root/trunk/src/java/org/jcoderz/commons/util/ObjectUtil.java

Revision 1286, 5.1 kB (checked in by amandel, 3 years ago)

New utility methods.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 package org.jcoderz.commons.util;
34
35
36/**
37 * This class provides object related utility functions.
38 *
39 * @author Michael Griffel
40 */
41public final class ObjectUtil
42{
43   private ObjectUtil ()
44   {
45      // utility class - only static methods.
46   }
47
48   /**
49    *  Returns <tt>true</tt> if the two specified objects are
50    * <i>equal</i> to one another. Two objects <tt>a</tt>
51    * and <tt>b</tt> are considered <i>equal</i> if <tt>(a==null ? b == null
52    * : a.equals(b))</tt>. Also, two object references are considered
53    * equal if both are <tt>null</tt>.
54    *
55    * This method allow easy implementation of the <code>equals</code> method.
56    *
57    * @param a one object to be tested for equality.
58    * @param b the other object to be tested for equality.
59    * @return <tt>true</tt> if the two objects are equal; <tt>false</tt>
60    *       otherwise.
61    */
62   public static boolean equals (Object a, Object b)
63   {
64      return (a == null ? b == null : a.equals(b));
65   }
66
67   /**
68    * Returns <tt>true</tt> if the two values are <i>equal</i> to one another.
69    *
70    * This method allow easy implementation of the <code>equals</code> method.
71    *
72    * @param a value to be tested for equality.
73    * @param b the other value to be tested for equality.
74    * @return <tt>true</tt> if the two value are equal; <tt>false</tt>
75    *       otherwise.
76    */
77   public static boolean equals (long a, long b)
78   {
79      return a == b;
80   }
81
82   /**
83    * Returns <tt>true</tt> if the two values are <i>equal</i> to one another.
84    *
85    * This method allow easy implementation of the <code>equals</code> method.
86    *
87    * @param a value to be tested for equality.
88    * @param b the other value to be tested for equality.
89    * @return <tt>true</tt> if the two value are equal; <tt>false</tt>
90    *       otherwise.
91    */
92   public static boolean equals (int a, int b)
93   {
94      return a == b;
95   }
96
97   /**
98    * Returns <tt>true</tt> if the two values are <i>equal</i> to one another.
99    *
100    * This method allow easy implementation of the <code>equals</code> method.
101    *
102    * @param a value to be tested for equality.
103    * @param b the other value to be tested for equality.
104    * @return <tt>true</tt> if the two value are equal; <tt>false</tt>
105    *       otherwise.
106    */
107   public static boolean equals (char a, char b)
108   {
109      return a == b;
110   }
111
112   /**
113    * Returns <tt>true</tt> if the two values are <i>equal</i> to one another.
114    *
115    * This method allow easy implementation of the <code>equals</code> method.
116    *
117    * @param a value to be tested for equality.
118    * @param b the other value to be tested for equality.
119    * @return <tt>true</tt> if the two value are equal; <tt>false</tt>
120    *       otherwise.
121    */
122   public static boolean equals (boolean a, boolean b)
123   {
124      return a == b;
125   }
126
127   /**
128    * Returns the string representation of the object or
129    * <code>null</code> if the object is null.
130    * @param obj the object to be converted or <code>null</code>.
131    * @return the string representation of the object or
132    * <code>null</code> if the object is null.
133    */
134   public static String toString (Object obj)
135   {
136      return obj == null ? null : obj.toString();
137   }
138
139   /**
140    * Returns the string representation of the object or
141    * an empty string if the object is null.
142    * @param obj the object to be converted or <code>null</code>.
143    * @return the string representation of the object or
144    * an empty string if the object is null.
145    */
146   public static String toStringOrEmpty (Object obj)
147   {
148      return obj == null ? "" : obj.toString();
149   }
150}
Note: See TracBrowser for help on using the browser.