358520018367c5a77db34bea09083b1d8f3c7e07.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package com.chinacreator.process.util;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.List;
  5. public final class Objects {
  6. private Objects() {
  7. throw new AssertionError("No java.util.Objects instances for you!");
  8. }
  9. /**
  10. * Returns {@code true} if the arguments are equal to each other
  11. * and {@code false} otherwise.
  12. * Consequently, if both arguments are {@code null}, {@code true}
  13. * is returned and if exactly one argument is {@code null}, {@code
  14. * false} is returned. Otherwise, equality is determined by using
  15. * the {@link Object#equals equals} method of the first
  16. * argument.
  17. *
  18. * @param a an object
  19. * @param b an object to be compared with {@code a} for equality
  20. * @return {@code true} if the arguments are equal to each other
  21. * and {@code false} otherwise
  22. * @see Object#equals(Object)
  23. */
  24. public static boolean equals(Object a, Object b) {
  25. return (a == b) || (a != null && a.equals(b));
  26. }
  27. /**
  28. * Returns {@code true} if the arguments are deeply equal to each other
  29. * and {@code false} otherwise.
  30. *
  31. * Two {@code null} values are deeply equal. If both arguments are
  32. * arrays, the algorithm in {@link Arrays#deepEquals(Object[],
  33. * Object[]) Arrays.deepEquals} is used to determine equality.
  34. * Otherwise, equality is determined by using the {@link
  35. * Object#equals equals} method of the first argument.
  36. *
  37. * @param a an object
  38. * @param b an object to be compared with {@code a} for deep equality
  39. * @return {@code true} if the arguments are deeply equal to each other
  40. * and {@code false} otherwise
  41. * @see Arrays#deepEquals(Object[], Object[])
  42. * @see Objects#equals(Object, Object)
  43. */
  44. // public static boolean deepEquals(Object a, Object b) {
  45. // if (a == b)
  46. // return true;
  47. // else if (a == null || b == null)
  48. // return false;
  49. // else
  50. // return Arrays.deepEquals0(a, b);
  51. // }
  52. /**
  53. * Returns the hash code of a non-{@code null} argument and 0 for
  54. * a {@code null} argument.
  55. *
  56. * @param o an object
  57. * @return the hash code of a non-{@code null} argument and 0 for
  58. * a {@code null} argument
  59. * @see Object#hashCode
  60. */
  61. public static int hashCode(Object o) {
  62. return o != null ? o.hashCode() : 0;
  63. }
  64. /**
  65. * Generates a hash code for a sequence of input values. The hash
  66. * code is generated as if all the input values were placed into an
  67. * array, and that array were hashed by calling {@link
  68. * Arrays#hashCode(Object[])}.
  69. *
  70. * <p>This method is useful for implementing {@link
  71. * Object#hashCode()} on objects containing multiple fields. For
  72. * example, if an object that has three fields, {@code x}, {@code
  73. * y}, and {@code z}, one could write:
  74. *
  75. * <blockquote><pre>
  76. * &#064;Override public int hashCode() {
  77. * return Objects.hash(x, y, z);
  78. * }
  79. * </pre></blockquote>
  80. *
  81. * <b>Warning: When a single object reference is supplied, the returned
  82. * value does not equal the hash code of that object reference.</b> This
  83. * value can be computed by calling {@link #hashCode(Object)}.
  84. *
  85. * @param values the values to be hashed
  86. * @return a hash value of the sequence of input values
  87. * @see Arrays#hashCode(Object[])
  88. * @see List#hashCode
  89. */
  90. public static int hash(Object... values) {
  91. return Arrays.hashCode(values);
  92. }
  93. /**
  94. * Returns the result of calling {@code toString} for a non-{@code
  95. * null} argument and {@code "null"} for a {@code null} argument.
  96. *
  97. * @param o an object
  98. * @return the result of calling {@code toString} for a non-{@code
  99. * null} argument and {@code "null"} for a {@code null} argument
  100. * @see Object#toString
  101. * @see String#valueOf(Object)
  102. */
  103. public static String toString(Object o) {
  104. return String.valueOf(o);
  105. }
  106. /**
  107. * Returns the result of calling {@code toString} on the first
  108. * argument if the first argument is not {@code null} and returns
  109. * the second argument otherwise.
  110. *
  111. * @param o an object
  112. * @param nullDefault string to return if the first argument is
  113. * {@code null}
  114. * @return the result of calling {@code toString} on the first
  115. * argument if it is not {@code null} and the second argument
  116. * otherwise.
  117. * @see Objects#toString(Object)
  118. */
  119. public static String toString(Object o, String nullDefault) {
  120. return (o != null) ? o.toString() : nullDefault;
  121. }
  122. /**
  123. * Returns 0 if the arguments are identical and {@code
  124. * c.compare(a, b)} otherwise.
  125. * Consequently, if both arguments are {@code null} 0
  126. * is returned.
  127. *
  128. * <p>Note that if one of the arguments is {@code null}, a {@code
  129. * NullPointerException} may or may not be thrown depending on
  130. * what ordering policy, if any, the {@link Comparator Comparator}
  131. * chooses to have for {@code null} values.
  132. *
  133. * @param <T> the type of the objects being compared
  134. * @param a an object
  135. * @param b an object to be compared with {@code a}
  136. * @param c the {@code Comparator} to compare the first two arguments
  137. * @return 0 if the arguments are identical and {@code
  138. * c.compare(a, b)} otherwise.
  139. * @see Comparable
  140. * @see Comparator
  141. */
  142. public static <T> int compare(T a, T b, Comparator<? super T> c) {
  143. return (a == b) ? 0 : c.compare(a, b);
  144. }
  145. /**
  146. * Checks that the specified object reference is not {@code null}. This
  147. * method is designed primarily for doing parameter validation in methods
  148. * and constructors, as demonstrated below:
  149. * <blockquote><pre>
  150. * public Foo(Bar bar) {
  151. * this.bar = Objects.requireNonNull(bar);
  152. * }
  153. * </pre></blockquote>
  154. *
  155. * @param obj the object reference to check for nullity
  156. * @param <T> the type of the reference
  157. * @return {@code obj} if not {@code null}
  158. * @throws NullPointerException if {@code obj} is {@code null}
  159. */
  160. public static <T> T requireNonNull(T obj) {
  161. if (obj == null)
  162. throw new NullPointerException();
  163. return obj;
  164. }
  165. /**
  166. * Checks that the specified object reference is not {@code null} and
  167. * throws a customized {@link NullPointerException} if it is. This method
  168. * is designed primarily for doing parameter validation in methods and
  169. * constructors with multiple parameters, as demonstrated below:
  170. * <blockquote><pre>
  171. * public Foo(Bar bar, Baz baz) {
  172. * this.bar = Objects.requireNonNull(bar, "bar must not be null");
  173. * this.baz = Objects.requireNonNull(baz, "baz must not be null");
  174. * }
  175. * </pre></blockquote>
  176. *
  177. * @param obj the object reference to check for nullity
  178. * @param message detail message to be used in the event that a {@code
  179. * NullPointerException} is thrown
  180. * @param <T> the type of the reference
  181. * @return {@code obj} if not {@code null}
  182. * @throws NullPointerException if {@code obj} is {@code null}
  183. */
  184. public static <T> T requireNonNull(T obj, String message) {
  185. if (obj == null)
  186. throw new NullPointerException(message);
  187. return obj;
  188. }
  189. }