ModelUtil.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.workgroup.util;

  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.ListIterator;

  22. /**
  23.  * Utility methods frequently used by data classes and design-time
  24.  * classes.
  25.  */
  26. public final class ModelUtil {
  27.     private ModelUtil() {
  28.         //  Prevents instantiation.
  29.     }

  30.     /**
  31.      * This is a utility method that compares two objects when one or
  32.      * both of the objects might be <CODE>null</CODE>  The result of
  33.      * this method is determined as follows:
  34.      * <OL>
  35.      * <LI>If <CODE>o1</CODE> and <CODE>o2</CODE> are the same object
  36.      * according to the <CODE>==</CODE> operator, return
  37.      * <CODE>true</CODE>.
  38.      * <LI>Otherwise, if either <CODE>o1</CODE> or <CODE>o2</CODE> is
  39.      * <CODE>null</CODE>, return <CODE>false</CODE>.
  40.      * <LI>Otherwise, return <CODE>o1.equals(o2)</CODE>.
  41.      * </OL>
  42.      * <p/>
  43.      * This method produces the exact logically inverted result as the
  44.      * {@link #areDifferent(Object, Object)} method.<P>
  45.      * <p/>
  46.      * For array types, one of the <CODE>equals</CODE> methods in
  47.      * {@link java.util.Arrays} should be used instead of this method.
  48.      * Note that arrays with more than one dimension will require some
  49.      * custom code in order to implement <CODE>equals</CODE> properly.
  50.      */
  51.     public static final boolean areEqual(Object o1, Object o2) {
  52.         if (o1 == o2) {
  53.             return true;
  54.         }
  55.         else if (o1 == null || o2 == null) {
  56.             return false;
  57.         }
  58.         else {
  59.             return o1.equals(o2);
  60.         }
  61.     }

  62.     /**
  63.      * This is a utility method that compares two Booleans when one or
  64.      * both of the objects might be <CODE>null</CODE>  The result of
  65.      * this method is determined as follows:
  66.      * <OL>
  67.      * <LI>If <CODE>b1</CODE> and <CODE>b2</CODE> are both TRUE or
  68.      * neither <CODE>b1</CODE> nor <CODE>b2</CODE> is TRUE,
  69.      * return <CODE>true</CODE>.
  70.      * <LI>Otherwise, return <CODE>false</CODE>.
  71.      * </OL>
  72.      * <p/>
  73.      */
  74.     public static final boolean areBooleansEqual(Boolean b1, Boolean b2) {
  75.         // !jwetherb treat NULL the same as Boolean.FALSE
  76.         return (b1 == Boolean.TRUE && b2 == Boolean.TRUE) ||
  77.                 (b1 != Boolean.TRUE && b2 != Boolean.TRUE);
  78.     }

  79.     /**
  80.      * This is a utility method that compares two objects when one or
  81.      * both of the objects might be <CODE>null</CODE>.  The result
  82.      * returned by this method is determined as follows:
  83.      * <OL>
  84.      * <LI>If <CODE>o1</CODE> and <CODE>o2</CODE> are the same object
  85.      * according to the <CODE>==</CODE> operator, return
  86.      * <CODE>false</CODE>.
  87.      * <LI>Otherwise, if either <CODE>o1</CODE> or <CODE>o2</CODE> is
  88.      * <CODE>null</CODE>, return <CODE>true</CODE>.
  89.      * <LI>Otherwise, return <CODE>!o1.equals(o2)</CODE>.
  90.      * </OL>
  91.      * <p/>
  92.      * This method produces the exact logically inverted result as the
  93.      * {@link #areEqual(Object, Object)} method.<P>
  94.      * <p/>
  95.      * For array types, one of the <CODE>equals</CODE> methods in
  96.      * {@link java.util.Arrays} should be used instead of this method.
  97.      * Note that arrays with more than one dimension will require some
  98.      * custom code in order to implement <CODE>equals</CODE> properly.
  99.      */
  100.     public static final boolean areDifferent(Object o1, Object o2) {
  101.         return !areEqual(o1, o2);
  102.     }


  103.     /**
  104.      * This is a utility method that compares two Booleans when one or
  105.      * both of the objects might be <CODE>null</CODE>  The result of
  106.      * this method is determined as follows:
  107.      * <OL>
  108.      * <LI>If <CODE>b1</CODE> and <CODE>b2</CODE> are both TRUE or
  109.      * neither <CODE>b1</CODE> nor <CODE>b2</CODE> is TRUE,
  110.      * return <CODE>false</CODE>.
  111.      * <LI>Otherwise, return <CODE>true</CODE>.
  112.      * </OL>
  113.      * <p/>
  114.      * This method produces the exact logically inverted result as the
  115.      * {@link #areBooleansEqual(Boolean, Boolean)} method.<P>
  116.      */
  117.     public static final boolean areBooleansDifferent(Boolean b1, Boolean b2) {
  118.         return !areBooleansEqual(b1, b2);
  119.     }


  120.     /**
  121.      * Returns <CODE>true</CODE> if the specified array is not null
  122.      * and contains a non-null element.  Returns <CODE>false</CODE>
  123.      * if the array is null or if all the array elements are null.
  124.      */
  125.     public static final boolean hasNonNullElement(Object[] array) {
  126.         if (array != null) {
  127.             final int n = array.length;
  128.             for (int i = 0; i < n; i++) {
  129.                 if (array[i] != null) {
  130.                     return true;
  131.                 }
  132.             }
  133.         }
  134.         return false;
  135.     }

  136.     /**
  137.      * Returns a single string that is the concatenation of all the
  138.      * strings in the specified string array.  A single space is
  139.      * put between each string array element.  Null array elements
  140.      * are skipped.  If the array itself is null, the empty string
  141.      * is returned.  This method is guaranteed to return a non-null
  142.      * value, if no expections are thrown.
  143.      */
  144.     public static final String concat(String[] strs) {
  145.         return concat(strs, " ");  //NOTRANS
  146.     }

  147.     /**
  148.      * Returns a single string that is the concatenation of all the
  149.      * strings in the specified string array.  The strings are separated
  150.      * by the specified delimiter.  Null array elements are skipped.  If
  151.      * the array itself is null, the empty string is returned.  This
  152.      * method is guaranteed to return a non-null value, if no expections
  153.      * are thrown.
  154.      */
  155.     public static final String concat(String[] strs, String delim) {
  156.         if (strs != null) {
  157.             final StringBuilder buf = new StringBuilder();
  158.             final int n = strs.length;
  159.             for (int i = 0; i < n; i++) {
  160.                 final String str = strs[i];
  161.                 if (str != null) {
  162.                     buf.append(str).append(delim);
  163.                 }
  164.             }
  165.             final int length = buf.length();
  166.             if (length > 0) {
  167.                 //  Trim trailing space.
  168.                 buf.setLength(length - 1);
  169.             }
  170.             return buf.toString();
  171.         }
  172.         else {
  173.             return ""; // NOTRANS
  174.         }
  175.     }

  176.     /**
  177.      * Returns <CODE>true</CODE> if the specified {@link String} is not
  178.      * <CODE>null</CODE> and has a length greater than zero.  This is
  179.      * a very frequently occurring check.
  180.      */
  181.     public static final boolean hasLength(String s) {
  182.         return (s != null && s.length() > 0);
  183.     }


  184.     /**
  185.      * Returns <CODE>null</CODE> if the specified string is empty or
  186.      * <CODE>null</CODE>.  Otherwise the string itself is returned.
  187.      */
  188.     public static final String nullifyIfEmpty(String s) {
  189.         return ModelUtil.hasLength(s) ? s : null;
  190.     }

  191.     /**
  192.      * Returns <CODE>null</CODE> if the specified object is null
  193.      * or if its <CODE>toString()</CODE> representation is empty.
  194.      * Otherwise, the <CODE>toString()</CODE> representation of the
  195.      * object itself is returned.
  196.      */
  197.     public static final String nullifyingToString(Object o) {
  198.         return o != null ? nullifyIfEmpty(o.toString()) : null;
  199.     }

  200.     /**
  201.      * Determines if a string has been changed.
  202.      *
  203.      * @param oldString is the initial value of the String
  204.      * @param newString is the new value of the String
  205.      * @return true If both oldString and newString are null or if they are
  206.      *         both not null and equal to each other.  Otherwise returns false.
  207.      */
  208.     public static boolean hasStringChanged(String oldString, String newString) {
  209.         if (oldString == null && newString == null) {
  210.             return false;
  211.         }
  212.         else if ((oldString == null && newString != null)
  213.                 || (oldString != null && newString == null)) {
  214.             return true;
  215.         }
  216.         else {
  217.             return !oldString.equals(newString);
  218.         }
  219.     }

  220.     public static String getTimeFromLong(long diff) {
  221.         final String HOURS = "h";
  222.         final String MINUTES = "min";
  223.         final String SECONDS = "sec";

  224.         final long MS_IN_A_DAY = 1000 * 60 * 60 * 24;
  225.         final long MS_IN_AN_HOUR = 1000 * 60 * 60;
  226.         final long MS_IN_A_MINUTE = 1000 * 60;
  227.         final long MS_IN_A_SECOND = 1000;
  228.         diff = diff % MS_IN_A_DAY;
  229.         long numHours = diff / MS_IN_AN_HOUR;
  230.         diff = diff % MS_IN_AN_HOUR;
  231.         long numMinutes = diff / MS_IN_A_MINUTE;
  232.         diff = diff % MS_IN_A_MINUTE;
  233.         long numSeconds = diff / MS_IN_A_SECOND;
  234.         diff = diff % MS_IN_A_SECOND;

  235.         StringBuilder buf = new StringBuilder();
  236.         if (numHours > 0) {
  237.             buf.append(numHours + " " + HOURS + ", ");
  238.         }

  239.         if (numMinutes > 0) {
  240.             buf.append(numMinutes + " " + MINUTES + ", ");
  241.         }

  242.         buf.append(numSeconds + " " + SECONDS);

  243.         String result = buf.toString();
  244.         return result;
  245.     }


  246.     /**
  247.      * Build a List of all elements in an Iterator.
  248.      */
  249.     public static <T> List<T> iteratorAsList(Iterator<T> i) {
  250.         ArrayList<T> list = new ArrayList<T>(10);
  251.         while (i.hasNext()) {
  252.             list.add(i.next());
  253.         }
  254.         return list;
  255.     }

  256.     /**
  257.      * Creates an Iterator that is the reverse of a ListIterator.
  258.      */
  259.     public static <T> Iterator<T> reverseListIterator(ListIterator<T> i) {
  260.         return new ReverseListIterator<T>(i);
  261.     }
  262. }

  263. /**
  264.  * An Iterator that is the reverse of a ListIterator.
  265.  */
  266. class ReverseListIterator<T> implements Iterator<T> {
  267.     private ListIterator<T> _i;

  268.     ReverseListIterator(ListIterator<T> i) {
  269.         _i = i;
  270.         while (_i.hasNext())
  271.             _i.next();
  272.     }

  273.     public boolean hasNext() {
  274.         return _i.hasPrevious();
  275.     }

  276.     public T next() {
  277.         return _i.previous();
  278.     }

  279.     public void remove() {
  280.         _i.remove();
  281.     }

  282. }