PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/wcs/base/util/ObjectUtils.java

https://github.com/YuanZhencai/tms
Java | 991 lines | 576 code | 56 blank | 359 comment | 222 complexity | 870ca2d065f5273cab43d809454bc11f MD5 | raw file
  1. /*
  2. * Copyright 2002-2009 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.wcs.base.util;
  17. import java.lang.reflect.Array;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. /**
  23. * Miscellaneous object utility methods.
  24. *
  25. * <p>
  26. * Mainly for internal use within the framework; consider <a
  27. * href="http://jakarta.apache.org/commons/lang/">Jakarta's Commons Lang</a> for
  28. * a more comprehensive suite of object utilities.
  29. *
  30. * <p>
  31. * Thanks to Alex Ruiz for contributing several enhancements to this class!
  32. *
  33. * @author Juergen Hoeller
  34. * @author Keith Donald
  35. * @author Rod Johnson
  36. * @author Rob Harrop
  37. * @since 19.03.2004
  38. * @see org.apache.commons.lang.ObjectUtils
  39. */
  40. public abstract class ObjectUtils extends org.apache.commons.lang.ObjectUtils {
  41. private static final int INITIAL_HASH = 7;
  42. private static final int MULTIPLIER = 31;
  43. private static final String EMPTY_STRING = "";
  44. private static final String NULL_STRING = "null";
  45. private static final String ARRAY_START = "{";
  46. private static final String ARRAY_END = "}";
  47. private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
  48. private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
  49. /**
  50. * Return whether the given throwable is a checked exception: that is,
  51. * neither a RuntimeException nor an Error.
  52. *
  53. * @param ex
  54. * the throwable to check
  55. * @return whether the throwable is a checked exception
  56. * @see java.lang.Exception
  57. * @see java.lang.RuntimeException
  58. * @see java.lang.Error
  59. */
  60. public static boolean isCheckedException(Throwable ex) {
  61. return !(ex instanceof RuntimeException || ex instanceof Error);
  62. }
  63. /**
  64. * Check whether the given exception is compatible with the exceptions
  65. * declared in a throws clause.
  66. *
  67. * @param ex
  68. * the exception to checked
  69. * @param declaredExceptions
  70. * the exceptions declared in the throws clause
  71. * @return whether the given exception is compatible
  72. */
  73. public static boolean isCompatibleWithThrowsClause(Throwable ex,
  74. Class[] declaredExceptions) {
  75. if (!isCheckedException(ex)) {
  76. return true;
  77. }
  78. if (declaredExceptions != null) {
  79. int i = 0;
  80. while (i < declaredExceptions.length) {
  81. if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {
  82. return true;
  83. }
  84. i++;
  85. }
  86. }
  87. return false;
  88. }
  89. /**
  90. * Determine whether the given object is an array: either an Object array or
  91. * a primitive array.
  92. *
  93. * @param obj
  94. * the object to check
  95. */
  96. public static boolean isArray(Object obj) {
  97. return (obj != null && obj.getClass().isArray());
  98. }
  99. /**
  100. * Determine whether the given array is empty: i.e. <code>null</code> or of
  101. * zero length.
  102. *
  103. * @param array
  104. * the array to check
  105. */
  106. public static boolean isEmpty(Object[] array) {
  107. return (array == null || array.length == 0);
  108. }
  109. /**
  110. * Check whether the given array contains the given element.
  111. *
  112. * @param array
  113. * the array to check (may be <code>null</code>, in which case
  114. * the return value will always be <code>false</code>)
  115. * @param element
  116. * the element to check for
  117. * @return whether the element has been found in the given array
  118. */
  119. public static boolean containsElement(Object[] array, Object element) {
  120. if (array == null) {
  121. return false;
  122. }
  123. for (Object arrayEle : array) {
  124. if (nullSafeEquals(arrayEle, element)) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * Append the given Object to the given array, returning a new array
  132. * consisting of the input array contents plus the given Object.
  133. *
  134. * @param array
  135. * the array to append to (can be <code>null</code>)
  136. * @param obj
  137. * the Object to append
  138. * @return the new array (of the same component type; never
  139. * <code>null</code>)
  140. */
  141. public static Object[] addObjectToArray(Object[] array, Object obj) {
  142. Class compType = Object.class;
  143. if (array != null) {
  144. compType = array.getClass().getComponentType();
  145. } else if (obj != null) {
  146. compType = obj.getClass();
  147. }
  148. int newArrLength = (array != null ? array.length + 1 : 1);
  149. Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
  150. if (array != null) {
  151. System.arraycopy(array, 0, newArr, 0, array.length);
  152. }
  153. newArr[newArr.length - 1] = obj;
  154. return newArr;
  155. }
  156. /**
  157. * Convert the given array (which may be a primitive array) to an object
  158. * array (if necessary of primitive wrapper objects).
  159. * <p>
  160. * A <code>null</code> source value will be converted to an empty Object
  161. * array.
  162. *
  163. * @param source
  164. * the (potentially primitive) array
  165. * @return the corresponding object array (never <code>null</code>)
  166. * @throws IllegalArgumentException
  167. * if the parameter is not an array
  168. */
  169. public static Object[] toObjectArray(Object source) {
  170. if (source instanceof Object[]) {
  171. return (Object[]) source;
  172. }
  173. if (source == null) {
  174. return new Object[0];
  175. }
  176. if (!source.getClass().isArray()) {
  177. throw new IllegalArgumentException("Source is not an array: "
  178. + source);
  179. }
  180. int length = Array.getLength(source);
  181. if (length == 0) {
  182. return new Object[0];
  183. }
  184. Class wrapperType = Array.get(source, 0).getClass();
  185. Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
  186. for (int i = 0; i < length; i++) {
  187. newArray[i] = Array.get(source, i);
  188. }
  189. return newArray;
  190. }
  191. // ---------------------------------------------------------------------
  192. // Convenience methods for content-based equality/hash-code handling
  193. // ---------------------------------------------------------------------
  194. /**
  195. * Determine if the given objects are equal, returning <code>true</code> if
  196. * both are <code>null</code> or <code>false</code> if only one is
  197. * <code>null</code>.
  198. * <p>
  199. * Compares arrays with <code>Arrays.equals</code>, performing an equality
  200. * check based on the array elements rather than the array reference.
  201. *
  202. * @param o1
  203. * first Object to compare
  204. * @param o2
  205. * second Object to compare
  206. * @return whether the given objects are equal
  207. * @see java.util.Arrays#equals
  208. */
  209. public static boolean nullSafeEquals(Object o1, Object o2) {
  210. if (o1 == o2) {
  211. return true;
  212. }
  213. if (o1 == null || o2 == null) {
  214. return false;
  215. }
  216. if (o1.equals(o2)) {
  217. return true;
  218. }
  219. if (o1.getClass().isArray() && o2.getClass().isArray()) {
  220. if (o1 instanceof Object[] && o2 instanceof Object[]) {
  221. return Arrays.equals((Object[]) o1, (Object[]) o2);
  222. }
  223. if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
  224. return Arrays.equals((boolean[]) o1, (boolean[]) o2);
  225. }
  226. if (o1 instanceof byte[] && o2 instanceof byte[]) {
  227. return Arrays.equals((byte[]) o1, (byte[]) o2);
  228. }
  229. if (o1 instanceof char[] && o2 instanceof char[]) {
  230. return Arrays.equals((char[]) o1, (char[]) o2);
  231. }
  232. if (o1 instanceof double[] && o2 instanceof double[]) {
  233. return Arrays.equals((double[]) o1, (double[]) o2);
  234. }
  235. if (o1 instanceof float[] && o2 instanceof float[]) {
  236. return Arrays.equals((float[]) o1, (float[]) o2);
  237. }
  238. if (o1 instanceof int[] && o2 instanceof int[]) {
  239. return Arrays.equals((int[]) o1, (int[]) o2);
  240. }
  241. if (o1 instanceof long[] && o2 instanceof long[]) {
  242. return Arrays.equals((long[]) o1, (long[]) o2);
  243. }
  244. if (o1 instanceof short[] && o2 instanceof short[]) {
  245. return Arrays.equals((short[]) o1, (short[]) o2);
  246. }
  247. }
  248. return false;
  249. }
  250. /**
  251. * Return as hash code for the given object; typically the value of
  252. * <code>{@link Object#hashCode()}</code>. If the object is an array, this
  253. * method will delegate to any of the <code>nullSafeHashCode</code> methods
  254. * for arrays in this class. If the object is <code>null</code>, this method
  255. * returns 0.
  256. *
  257. * @see #nullSafeHashCode(Object[])
  258. * @see #nullSafeHashCode(boolean[])
  259. * @see #nullSafeHashCode(byte[])
  260. * @see #nullSafeHashCode(char[])
  261. * @see #nullSafeHashCode(double[])
  262. * @see #nullSafeHashCode(float[])
  263. * @see #nullSafeHashCode(int[])
  264. * @see #nullSafeHashCode(long[])
  265. * @see #nullSafeHashCode(short[])
  266. */
  267. public static int nullSafeHashCode(Object obj) {
  268. if (obj == null) {
  269. return 0;
  270. }
  271. if (obj.getClass().isArray()) {
  272. if (obj instanceof Object[]) {
  273. return nullSafeHashCode((Object[]) obj);
  274. }
  275. if (obj instanceof boolean[]) {
  276. return nullSafeHashCode((boolean[]) obj);
  277. }
  278. if (obj instanceof byte[]) {
  279. return nullSafeHashCode((byte[]) obj);
  280. }
  281. if (obj instanceof char[]) {
  282. return nullSafeHashCode((char[]) obj);
  283. }
  284. if (obj instanceof double[]) {
  285. return nullSafeHashCode((double[]) obj);
  286. }
  287. if (obj instanceof float[]) {
  288. return nullSafeHashCode((float[]) obj);
  289. }
  290. if (obj instanceof int[]) {
  291. return nullSafeHashCode((int[]) obj);
  292. }
  293. if (obj instanceof long[]) {
  294. return nullSafeHashCode((long[]) obj);
  295. }
  296. if (obj instanceof short[]) {
  297. return nullSafeHashCode((short[]) obj);
  298. }
  299. }
  300. return obj.hashCode();
  301. }
  302. /**
  303. * Return a hash code based on the contents of the specified array. If
  304. * <code>array</code> is <code>null</code>, this method returns 0.
  305. */
  306. public static int nullSafeHashCode(Object[] array) {
  307. if (array == null) {
  308. return 0;
  309. }
  310. int hash = INITIAL_HASH;
  311. int arraySize = array.length;
  312. for (int i = 0; i < arraySize; i++) {
  313. hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
  314. }
  315. return hash;
  316. }
  317. /**
  318. * Return a hash code based on the contents of the specified array. If
  319. * <code>array</code> is <code>null</code>, this method returns 0.
  320. */
  321. public static int nullSafeHashCode(boolean[] array) {
  322. if (array == null) {
  323. return 0;
  324. }
  325. int hash = INITIAL_HASH;
  326. int arraySize = array.length;
  327. for (int i = 0; i < arraySize; i++) {
  328. hash = MULTIPLIER * hash + hashCode(array[i]);
  329. }
  330. return hash;
  331. }
  332. /**
  333. * Return a hash code based on the contents of the specified array. If
  334. * <code>array</code> is <code>null</code>, this method returns 0.
  335. */
  336. public static int nullSafeHashCode(byte[] array) {
  337. if (array == null) {
  338. return 0;
  339. }
  340. int hash = INITIAL_HASH;
  341. int arraySize = array.length;
  342. for (int i = 0; i < arraySize; i++) {
  343. hash = MULTIPLIER * hash + array[i];
  344. }
  345. return hash;
  346. }
  347. /**
  348. * Return a hash code based on the contents of the specified array. If
  349. * <code>array</code> is <code>null</code>, this method returns 0.
  350. */
  351. public static int nullSafeHashCode(char[] array) {
  352. if (array == null) {
  353. return 0;
  354. }
  355. int hash = INITIAL_HASH;
  356. int arraySize = array.length;
  357. for (int i = 0; i < arraySize; i++) {
  358. hash = MULTIPLIER * hash + array[i];
  359. }
  360. return hash;
  361. }
  362. /**
  363. * Return a hash code based on the contents of the specified array. If
  364. * <code>array</code> is <code>null</code>, this method returns 0.
  365. */
  366. public static int nullSafeHashCode(double[] array) {
  367. if (array == null) {
  368. return 0;
  369. }
  370. int hash = INITIAL_HASH;
  371. int arraySize = array.length;
  372. for (int i = 0; i < arraySize; i++) {
  373. hash = MULTIPLIER * hash + hashCode(array[i]);
  374. }
  375. return hash;
  376. }
  377. /**
  378. * Return a hash code based on the contents of the specified array. If
  379. * <code>array</code> is <code>null</code>, this method returns 0.
  380. */
  381. public static int nullSafeHashCode(float[] array) {
  382. if (array == null) {
  383. return 0;
  384. }
  385. int hash = INITIAL_HASH;
  386. int arraySize = array.length;
  387. for (int i = 0; i < arraySize; i++) {
  388. hash = MULTIPLIER * hash + hashCode(array[i]);
  389. }
  390. return hash;
  391. }
  392. /**
  393. * Return a hash code based on the contents of the specified array. If
  394. * <code>array</code> is <code>null</code>, this method returns 0.
  395. */
  396. public static int nullSafeHashCode(int[] array) {
  397. if (array == null) {
  398. return 0;
  399. }
  400. int hash = INITIAL_HASH;
  401. int arraySize = array.length;
  402. for (int i = 0; i < arraySize; i++) {
  403. hash = MULTIPLIER * hash + array[i];
  404. }
  405. return hash;
  406. }
  407. /**
  408. * Return a hash code based on the contents of the specified array. If
  409. * <code>array</code> is <code>null</code>, this method returns 0.
  410. */
  411. public static int nullSafeHashCode(long[] array) {
  412. if (array == null) {
  413. return 0;
  414. }
  415. int hash = INITIAL_HASH;
  416. int arraySize = array.length;
  417. for (int i = 0; i < arraySize; i++) {
  418. hash = MULTIPLIER * hash + hashCode(array[i]);
  419. }
  420. return hash;
  421. }
  422. /**
  423. * Return a hash code based on the contents of the specified array. If
  424. * <code>array</code> is <code>null</code>, this method returns 0.
  425. */
  426. public static int nullSafeHashCode(short[] array) {
  427. if (array == null) {
  428. return 0;
  429. }
  430. int hash = INITIAL_HASH;
  431. int arraySize = array.length;
  432. for (int i = 0; i < arraySize; i++) {
  433. hash = MULTIPLIER * hash + array[i];
  434. }
  435. return hash;
  436. }
  437. /**
  438. * Return the same value as <code>{@link Boolean#hashCode()}</code>.
  439. *
  440. * @see Boolean#hashCode()
  441. */
  442. public static int hashCode(boolean bool) {
  443. return bool ? 1231 : 1237;
  444. }
  445. /**
  446. * Return the same value as <code>{@link Double#hashCode()}</code>.
  447. *
  448. * @see Double#hashCode()
  449. */
  450. public static int hashCode(double dbl) {
  451. long bits = Double.doubleToLongBits(dbl);
  452. return hashCode(bits);
  453. }
  454. /**
  455. * Return the same value as <code>{@link Float#hashCode()}</code>.
  456. *
  457. * @see Float#hashCode()
  458. */
  459. public static int hashCode(float flt) {
  460. return Float.floatToIntBits(flt);
  461. }
  462. /**
  463. * Return the same value as <code>{@link Long#hashCode()}</code>.
  464. *
  465. * @see Long#hashCode()
  466. */
  467. public static int hashCode(long lng) {
  468. return (int) (lng ^ (lng >>> 32));
  469. }
  470. // ---------------------------------------------------------------------
  471. // Convenience methods for toString output
  472. // ---------------------------------------------------------------------
  473. /**
  474. * Return a String representation of an object's overall identity.
  475. *
  476. * @param obj
  477. * the object (may be <code>null</code>)
  478. * @return the object's identity as String representation, or an empty
  479. * String if the object was <code>null</code>
  480. */
  481. public static String identityToString(Object obj) {
  482. if (obj == null) {
  483. return EMPTY_STRING;
  484. }
  485. return obj.getClass().getName() + "@" + getIdentityHexString(obj);
  486. }
  487. /**
  488. * Return a hex String form of an object's identity hash code.
  489. *
  490. * @param obj
  491. * the object
  492. * @return the object's identity code in hex notation
  493. */
  494. public static String getIdentityHexString(Object obj) {
  495. return Integer.toHexString(System.identityHashCode(obj));
  496. }
  497. /**
  498. * Return a content-based String representation if <code>obj</code> is not
  499. * <code>null</code>; otherwise returns an empty String.
  500. * <p>
  501. * Differs from {@link #nullSafeToString(Object)} in that it returns an
  502. * empty String rather than "null" for a <code>null</code> value.
  503. *
  504. * @param obj
  505. * the object to build a display String for
  506. * @return a display String representation of <code>obj</code>
  507. * @see #nullSafeToString(Object)
  508. */
  509. public static String getDisplayString(Object obj) {
  510. if (obj == null) {
  511. return EMPTY_STRING;
  512. }
  513. return nullSafeToString(obj);
  514. }
  515. /**
  516. * Determine the class name for the given object.
  517. * <p>
  518. * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
  519. *
  520. * @param obj
  521. * the object to introspect (may be <code>null</code>)
  522. * @return the corresponding class name
  523. */
  524. public static String nullSafeClassName(Object obj) {
  525. return (obj != null ? obj.getClass().getName() : NULL_STRING);
  526. }
  527. /**
  528. * Return a String representation of the specified Object.
  529. * <p>
  530. * Builds a String representation of the contents in case of an array.
  531. * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
  532. *
  533. * @param obj
  534. * the object to build a String representation for
  535. * @return a String representation of <code>obj</code>
  536. */
  537. public static String nullSafeToString(Object obj) {
  538. if (obj == null) {
  539. return NULL_STRING;
  540. }
  541. if (obj instanceof String) {
  542. return (String) obj;
  543. }
  544. if (obj instanceof Object[]) {
  545. return nullSafeToString((Object[]) obj);
  546. }
  547. if (obj instanceof boolean[]) {
  548. return nullSafeToString((boolean[]) obj);
  549. }
  550. if (obj instanceof byte[]) {
  551. return nullSafeToString((byte[]) obj);
  552. }
  553. if (obj instanceof char[]) {
  554. return nullSafeToString((char[]) obj);
  555. }
  556. if (obj instanceof double[]) {
  557. return nullSafeToString((double[]) obj);
  558. }
  559. if (obj instanceof float[]) {
  560. return nullSafeToString((float[]) obj);
  561. }
  562. if (obj instanceof int[]) {
  563. return nullSafeToString((int[]) obj);
  564. }
  565. if (obj instanceof long[]) {
  566. return nullSafeToString((long[]) obj);
  567. }
  568. if (obj instanceof short[]) {
  569. return nullSafeToString((short[]) obj);
  570. }
  571. String str = obj.toString();
  572. return (str != null ? str : EMPTY_STRING);
  573. }
  574. /**
  575. * Return a String representation of the contents of the specified array.
  576. * <p>
  577. * The String representation consists of a list of the array's elements,
  578. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  579. * separated by the characters <code>", "</code> (a comma followed by a
  580. * space). Returns <code>"null"</code> if <code>array</code> is
  581. * <code>null</code>.
  582. *
  583. * @param array
  584. * the array to build a String representation for
  585. * @return a String representation of <code>array</code>
  586. */
  587. public static String nullSafeToString(Object[] array) {
  588. if (array == null) {
  589. return NULL_STRING;
  590. }
  591. int length = array.length;
  592. if (length == 0) {
  593. return EMPTY_ARRAY;
  594. }
  595. StringBuilder sb = new StringBuilder();
  596. for (int i = 0; i < length; i++) {
  597. if (i == 0) {
  598. sb.append(ARRAY_START);
  599. } else {
  600. sb.append(ARRAY_ELEMENT_SEPARATOR);
  601. }
  602. sb.append(String.valueOf(array[i]));
  603. }
  604. sb.append(ARRAY_END);
  605. return sb.toString();
  606. }
  607. /**
  608. * Return a String representation of the contents of the specified array.
  609. * <p>
  610. * The String representation consists of a list of the array's elements,
  611. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  612. * separated by the characters <code>", "</code> (a comma followed by a
  613. * space). Returns <code>"null"</code> if <code>array</code> is
  614. * <code>null</code>.
  615. *
  616. * @param array
  617. * the array to build a String representation for
  618. * @return a String representation of <code>array</code>
  619. */
  620. public static String nullSafeToString(boolean[] array) {
  621. if (array == null) {
  622. return NULL_STRING;
  623. }
  624. int length = array.length;
  625. if (length == 0) {
  626. return EMPTY_ARRAY;
  627. }
  628. StringBuilder sb = new StringBuilder();
  629. for (int i = 0; i < length; i++) {
  630. if (i == 0) {
  631. sb.append(ARRAY_START);
  632. } else {
  633. sb.append(ARRAY_ELEMENT_SEPARATOR);
  634. }
  635. sb.append(array[i]);
  636. }
  637. sb.append(ARRAY_END);
  638. return sb.toString();
  639. }
  640. /**
  641. * Return a String representation of the contents of the specified array.
  642. * <p>
  643. * The String representation consists of a list of the array's elements,
  644. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  645. * separated by the characters <code>", "</code> (a comma followed by a
  646. * space). Returns <code>"null"</code> if <code>array</code> is
  647. * <code>null</code>.
  648. *
  649. * @param array
  650. * the array to build a String representation for
  651. * @return a String representation of <code>array</code>
  652. */
  653. public static String nullSafeToString(byte[] array) {
  654. if (array == null) {
  655. return NULL_STRING;
  656. }
  657. int length = array.length;
  658. if (length == 0) {
  659. return EMPTY_ARRAY;
  660. }
  661. StringBuilder sb = new StringBuilder();
  662. for (int i = 0; i < length; i++) {
  663. if (i == 0) {
  664. sb.append(ARRAY_START);
  665. } else {
  666. sb.append(ARRAY_ELEMENT_SEPARATOR);
  667. }
  668. sb.append(array[i]);
  669. }
  670. sb.append(ARRAY_END);
  671. return sb.toString();
  672. }
  673. /**
  674. * Return a String representation of the contents of the specified array.
  675. * <p>
  676. * The String representation consists of a list of the array's elements,
  677. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  678. * separated by the characters <code>", "</code> (a comma followed by a
  679. * space). Returns <code>"null"</code> if <code>array</code> is
  680. * <code>null</code>.
  681. *
  682. * @param array
  683. * the array to build a String representation for
  684. * @return a String representation of <code>array</code>
  685. */
  686. public static String nullSafeToString(char[] array) {
  687. if (array == null) {
  688. return NULL_STRING;
  689. }
  690. int length = array.length;
  691. if (length == 0) {
  692. return EMPTY_ARRAY;
  693. }
  694. StringBuilder sb = new StringBuilder();
  695. for (int i = 0; i < length; i++) {
  696. if (i == 0) {
  697. sb.append(ARRAY_START);
  698. } else {
  699. sb.append(ARRAY_ELEMENT_SEPARATOR);
  700. }
  701. sb.append("'").append(array[i]).append("'");
  702. }
  703. sb.append(ARRAY_END);
  704. return sb.toString();
  705. }
  706. /**
  707. * Return a String representation of the contents of the specified array.
  708. * <p>
  709. * The String representation consists of a list of the array's elements,
  710. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  711. * separated by the characters <code>", "</code> (a comma followed by a
  712. * space). Returns <code>"null"</code> if <code>array</code> is
  713. * <code>null</code>.
  714. *
  715. * @param array
  716. * the array to build a String representation for
  717. * @return a String representation of <code>array</code>
  718. */
  719. public static String nullSafeToString(double[] array) {
  720. if (array == null) {
  721. return NULL_STRING;
  722. }
  723. int length = array.length;
  724. if (length == 0) {
  725. return EMPTY_ARRAY;
  726. }
  727. StringBuilder sb = new StringBuilder();
  728. for (int i = 0; i < length; i++) {
  729. if (i == 0) {
  730. sb.append(ARRAY_START);
  731. } else {
  732. sb.append(ARRAY_ELEMENT_SEPARATOR);
  733. }
  734. sb.append(array[i]);
  735. }
  736. sb.append(ARRAY_END);
  737. return sb.toString();
  738. }
  739. /**
  740. * Return a String representation of the contents of the specified array.
  741. * <p>
  742. * The String representation consists of a list of the array's elements,
  743. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  744. * separated by the characters <code>", "</code> (a comma followed by a
  745. * space). Returns <code>"null"</code> if <code>array</code> is
  746. * <code>null</code>.
  747. *
  748. * @param array
  749. * the array to build a String representation for
  750. * @return a String representation of <code>array</code>
  751. */
  752. public static String nullSafeToString(float[] array) {
  753. if (array == null) {
  754. return NULL_STRING;
  755. }
  756. int length = array.length;
  757. if (length == 0) {
  758. return EMPTY_ARRAY;
  759. }
  760. StringBuilder sb = new StringBuilder();
  761. for (int i = 0; i < length; i++) {
  762. if (i == 0) {
  763. sb.append(ARRAY_START);
  764. } else {
  765. sb.append(ARRAY_ELEMENT_SEPARATOR);
  766. }
  767. sb.append(array[i]);
  768. }
  769. sb.append(ARRAY_END);
  770. return sb.toString();
  771. }
  772. /**
  773. * Return a String representation of the contents of the specified array.
  774. * <p>
  775. * The String representation consists of a list of the array's elements,
  776. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  777. * separated by the characters <code>", "</code> (a comma followed by a
  778. * space). Returns <code>"null"</code> if <code>array</code> is
  779. * <code>null</code>.
  780. *
  781. * @param array
  782. * the array to build a String representation for
  783. * @return a String representation of <code>array</code>
  784. */
  785. public static String nullSafeToString(int[] array) {
  786. if (array == null) {
  787. return NULL_STRING;
  788. }
  789. int length = array.length;
  790. if (length == 0) {
  791. return EMPTY_ARRAY;
  792. }
  793. StringBuilder sb = new StringBuilder();
  794. for (int i = 0; i < length; i++) {
  795. if (i == 0) {
  796. sb.append(ARRAY_START);
  797. } else {
  798. sb.append(ARRAY_ELEMENT_SEPARATOR);
  799. }
  800. sb.append(array[i]);
  801. }
  802. sb.append(ARRAY_END);
  803. return sb.toString();
  804. }
  805. /**
  806. * Return a String representation of the contents of the specified array.
  807. * <p>
  808. * The String representation consists of a list of the array's elements,
  809. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  810. * separated by the characters <code>", "</code> (a comma followed by a
  811. * space). Returns <code>"null"</code> if <code>array</code> is
  812. * <code>null</code>.
  813. *
  814. * @param array
  815. * the array to build a String representation for
  816. * @return a String representation of <code>array</code>
  817. */
  818. public static String nullSafeToString(long[] array) {
  819. if (array == null) {
  820. return NULL_STRING;
  821. }
  822. int length = array.length;
  823. if (length == 0) {
  824. return EMPTY_ARRAY;
  825. }
  826. StringBuilder sb = new StringBuilder();
  827. for (int i = 0; i < length; i++) {
  828. if (i == 0) {
  829. sb.append(ARRAY_START);
  830. } else {
  831. sb.append(ARRAY_ELEMENT_SEPARATOR);
  832. }
  833. sb.append(array[i]);
  834. }
  835. sb.append(ARRAY_END);
  836. return sb.toString();
  837. }
  838. /**
  839. * Return a String representation of the contents of the specified array.
  840. * <p>
  841. * The String representation consists of a list of the array's elements,
  842. * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are
  843. * separated by the characters <code>", "</code> (a comma followed by a
  844. * space). Returns <code>"null"</code> if <code>array</code> is
  845. * <code>null</code>.
  846. *
  847. * @param array
  848. * the array to build a String representation for
  849. * @return a String representation of <code>array</code>
  850. */
  851. public static String nullSafeToString(short[] array) {
  852. if (array == null) {
  853. return NULL_STRING;
  854. }
  855. int length = array.length;
  856. if (length == 0) {
  857. return EMPTY_ARRAY;
  858. }
  859. StringBuilder sb = new StringBuilder();
  860. for (int i = 0; i < length; i++) {
  861. if (i == 0) {
  862. sb.append(ARRAY_START);
  863. } else {
  864. sb.append(ARRAY_ELEMENT_SEPARATOR);
  865. }
  866. sb.append(array[i]);
  867. }
  868. sb.append(ARRAY_END);
  869. return sb.toString();
  870. }
  871. public static boolean isValidCollection(Collection collection) {
  872. if (null != collection && 0 != collection.size()) {
  873. return true;
  874. } else {
  875. return false;
  876. }
  877. }
  878. public static boolean isEqualsSize(Collection res, Collection des) {
  879. if (res.size() == des.size()) {
  880. return true;
  881. } else {
  882. return false;
  883. }
  884. }
  885. public static boolean isNullString(String str) {
  886. if (null != str && !"".equals(str.trim())) {
  887. return false;
  888. }
  889. return true;
  890. }
  891. public static boolean isValidNumber(Integer number) {
  892. if (null != number && 0 < number) {
  893. return true;
  894. } else {
  895. return false;
  896. }
  897. }
  898. public static boolean isValidNumber(Double number) {
  899. if (null != number && 0 < number) {
  900. return true;
  901. } else {
  902. return false;
  903. }
  904. }
  905. public static boolean isValidNumber(Long number) {
  906. if (null != number && 0 < number) {
  907. return true;
  908. } else {
  909. return false;
  910. }
  911. }
  912. public static boolean isValidNumber(Short number) {
  913. if (null != number && 0 < number) {
  914. return true;
  915. } else {
  916. return false;
  917. }
  918. }
  919. public static boolean isValidNumber(String number) {
  920. if (null != number && !"".equals(number)) {
  921. Pattern pattern = Pattern.compile("[0-9]{1,}(\\.[0-9]{1,})?");
  922. Matcher m = pattern.matcher(number);
  923. return m.matches();
  924. } else {
  925. return false;
  926. }
  927. }
  928. public static boolean isValidStr(String str) {
  929. if (null != str && !"".equalsIgnoreCase(str) && !"null".equals(str)) {
  930. return true;
  931. } else {
  932. return false;
  933. }
  934. }
  935. }