Advertisement
mbpaster

Utilities

Mar 15th, 2012
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.49 KB | None | 0 0
  1. /*
  2.  * This program is free software. It comes without any warranty, to
  3.  * the extent permitted by applicable law. You can redistribute it
  4.  * and/or modify it under the terms of the Do What The Fuck You Want
  5.  * To Public License, Version 2, as published by Sam Hocevar. See
  6.  * http://sam.zoy.org/wtfpl/COPYING for more details.
  7.  */
  8.  
  9. import java.util.List;
  10. import java.util.Set;
  11.  
  12. /**
  13.  * Contains only a bunch of <code>static</code> utility methods.
  14.  *
  15.  * @author mb - somethingididnotknow.wordpress.com
  16.  */
  17. public final class Utilities {
  18.  
  19.     /**
  20.      * Checks whether <strong>all</strong> the provided objects are
  21.      * <code>null</code>.
  22.      *
  23.      * @param objects
  24.      *            a number of objects of any kind that are to be checked
  25.      *            against <code>null</code>
  26.      * @return <code>true</code> in case <strong>all</strong> the argument
  27.      *         objects are <code>null</code>, <code>false</code> otherwise.
  28.      */
  29.     public static boolean areAllNull(Object... objects) {
  30.         for (Object o : objects) {
  31.             if (o != null)
  32.                 return false;
  33.         }
  34.         return true;
  35.     }
  36.  
  37.     /**
  38.      * Checks whether <strong>any</strong> of the argument objects is
  39.      * <code>null</code>.
  40.      *
  41.      * @param objects
  42.      *            a number of objects of any kind that are to be checked
  43.      *            against <code>null</code>.
  44.      * @return <code>true</code> if at least one of the arguments is
  45.      *         <code>null</code>.
  46.      */
  47.     public static boolean isAnyNull(Object... objects) {
  48.         for (Object o : objects) {
  49.             if (o == null)
  50.                 return true;
  51.         }
  52.         return false;
  53.     }
  54.  
  55.     /**
  56.      * Checks whether the two arguments are equal using a <em>null-safe</em>
  57.      * comparison.
  58.      *
  59.      * In case only one of the two objects is <code>null</code>,
  60.      * <code>false</code> is returned. In case both are not <code>null</code>
  61.      * {@link Object#equals(Object)} is called on the first object using the
  62.      * second as argument.
  63.      *
  64.      * @param first
  65.      *            the first object to be checked.
  66.      * @param second
  67.      *            the second object to be checked.
  68.      * @return <code>true</code> in case {@link Object#equals(Object)} returns
  69.      *         <code>true</code> or the objects are both <code>null</code>,
  70.      *         <code>false</code> otherwise.
  71.      */
  72.     public static boolean nsEquals(Object first, Object second) {
  73.         if (first == null)
  74.             return second == null;
  75.         if (second == null)
  76.             return false;
  77.         return first.equals(second);
  78.     }
  79.  
  80.     /**
  81.      * Returns a String that is empty in case the argument <tt>string</tt> is
  82.      * <code>null</code>, the unmodified <tt>string</tt> otherwise.
  83.      *
  84.      * @param string
  85.      *            the string to be checked against <code>null</code>
  86.      * @return the empty String if <tt>string</tt> is <code>null</code>, the
  87.      *         argument <tt>string</tt> unmodified otherwise
  88.      */
  89.     public static String nonNull(final String string) {
  90.         return string == null ? "" : string;
  91.     }
  92.  
  93.     /**
  94.      * An equivalent of Python's <code>str.join()</code> function on lists: it
  95.      * returns a String which is the concatenation of the strings in the
  96.      * argument array. The separator between elements is the argument
  97.      * <tt>toJoin</tt> string. The separator is only inserted between
  98.      * elements: there's no separator before the first element or after the
  99.      * last.
  100.      *
  101.      * @param toJoin
  102.      *            the separator, if <code>null</code> the empty String is used
  103.      * @param list
  104.      *            a list of <code>Object</code>s on which
  105.      *            {@link Object#toString()} will be called
  106.      * @return the concatenation of String representations of the objects in
  107.      *         the list
  108.      */
  109.     public static String join(String toJoin, Object[] list) {
  110.         if (list == null || list.length == 0)
  111.             return "";
  112.         StringBuilder builder = new StringBuilder();
  113.         String delimiter = nonNull(toJoin);
  114.         int i = 0;
  115.         for (; i < (list.length - 1); i++) {
  116.             if (list[i] != null)
  117.                 builder.append(list[i]);
  118.             builder.append(delimiter);
  119.         }
  120.         builder.append(list[i]);
  121.         return builder.toString();
  122.     }
  123.  
  124.     /**
  125.      * An equivalent of Python's <code>str.join()</code> function on lists: it
  126.      * returns a String which is the concatenation of the strings in the
  127.      * argument list. The separator between elements is the string providing
  128.      * this method. The separator is only inserted between elements: there's
  129.      * no separator before the first element or after the last.
  130.      *
  131.      * @param toJoin
  132.      *            the separator, if <code>null</code> the empty String is used
  133.      * @param list
  134.      *            a list of <code>Object</code>s on which
  135.      *            {@link Object#toString()} will be called
  136.      * @return the concatenation of String representations of the objects in
  137.      *         the list
  138.      */
  139.     public static String join(String toJoin, List<?> list) {
  140.         if (list == null || list.isEmpty())
  141.             return "";
  142.         StringBuilder builder = new StringBuilder();
  143.         String delimiter = nonNull(toJoin);
  144.         int i = 0;
  145.         for (; i < list.size() - 1; i++) {
  146.             if (list.get(i) != null)
  147.                 builder.append(list.get(i));
  148.             builder.append(delimiter);
  149.         }
  150.         builder.append(list.get(i));
  151.         return builder.toString();
  152.     }
  153.  
  154.     /**
  155.      * An equivalent of Python's <code>str.join()</code> function on lists: it
  156.      * returns a String which is the concatenation of the strings in the
  157.      * argument list. The separator between elements is the string providing
  158.      * this method. The separator is only inserted between elements: there's
  159.      * no separator before the first element or after the last.
  160.      *
  161.      * @param toJoin
  162.      *            the separator, if <code>null</code> the empty String is used
  163.      * @param set
  164.      *            a set of <code>Object</code>s on which
  165.      *            {@link Object#toString()} will be called
  166.      * @return the concatenation of String representations of the objects in
  167.      *         the set
  168.      */
  169.     public static String join(String toJoin, Set<?> set) {
  170.         return join(toJoin, set.toArray());
  171.     }
  172.     /**
  173.      * Checks whether the argument <tt>array</tt> contains at least a
  174.      * <code>null</code> value.
  175.      *
  176.      * @param array
  177.      *            the array to be checked.
  178.      * @return <code>true</code> in case <em>at least</em> one of the values
  179.      *         stored in the argument <tt>array</tt> is <code>null</code>, or
  180.      *         in case the <tt>array</tt> itself is <code>null</code>.
  181.      */
  182.     public static boolean containsNull(Object[] array) {
  183.         if (array == null)
  184.             return true;
  185.         for (Object o : array) {
  186.             if (o == null)
  187.                 return true;
  188.         }
  189.         return false;
  190.     }
  191.  
  192.     /**
  193.      * Checks whether the argument <tt>list</tt> contains at least a
  194.      * <code>null</code> value.
  195.      *
  196.      * @param list
  197.      *            the list to be checked
  198.      * @return <code>true</code> in case <em>at least</em> one of the values
  199.      *         stored in the argument <tt>array</tt> is <code>null</code>, or
  200.      *         in case the <tt>list</tt> itself is <code>null</code>
  201.      */
  202.     public static boolean containsNull(List<?> list) {
  203.         if (list == null)
  204.             return true;
  205.         for (Object o : list) {
  206.             if (o == null)
  207.                 return true;
  208.         }
  209.         return false;
  210.     }
  211.  
  212.     /**
  213.      * Checks whether the argument <tt>string</tt> is <code>null</code> or
  214.      * empty. Please note that the <tt>string</tt> is
  215.      * <strong>trimmed</strong>, so that a check on a string containing
  216.      * white spaces only will always return <code>true</code>.
  217.      *
  218.      * @param string
  219.      *            the string to be checked
  220.      * @return <code>true</code> in case the argument <tt>string</tt> is
  221.      *         <code>null</code>, empty ({@link String#length()} returns 0) or
  222.      *         contains only white spaces (
  223.      *         <tt>{@link String#trim()}.length()</tt> returns 0)
  224.      */
  225.     public static boolean isNullOrEmpty(String string) {
  226.         return string == null || string.trim().length() == 0;
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement