KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > Primitives


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 /**
25  * Primitive utilities.
26  *
27  * @version <tt>$Revision: 1958 $</tt>
28  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
29  */

30 public final class Primitives
31 {
32    /**
33     * Get a Boolean from a boolean, equivalent to the java 1.4 method Boolean.valueOf(boolean)
34     *
35     * @param value the boolean
36     * @return the Boolean equivalent
37     */

38    public static Boolean JavaDoc valueOf(boolean value)
39    {
40       if (value)
41          return Boolean.TRUE;
42       else
43          return Boolean.FALSE;
44    }
45
46    /**
47     * Test the equality of two doubles by converting their values into
48     * IEEE 754 floating-point "double format" long values.
49     *
50     * @param a Double to check equality with.
51     * @param b Double to check equality with.
52     * @return True if a equals b.
53     */

54    public static boolean equals(final double a, final double b) {
55       return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
56    }
57
58    /**
59     * Test the equality of two doubles by converting their values into
60     * IEEE 754 floating-point "single precision" bit layouts.
61     *
62     * @param a Float to check equality with.
63     * @param b Float to check equality with.
64     * @return True if a equals b.
65     */

66    public static boolean equals(final float a, final float b) {
67       return Float.floatToIntBits(a) == Float.floatToIntBits(b);
68    }
69
70    /**
71     * Test the equality of a given sub-section of two byte arrays.
72     *
73     * @param a The first byte array.
74     * @param abegin The begining index of the first byte array.
75     * @param b The second byte array.
76     * @param bbegin The begining index of the second byte array.
77     * @param length The length of the sub-section.
78     * @return True if sub-sections are equal.
79     */

80    public static boolean equals(final byte a[], final int abegin,
81                                 final byte b[], final int bbegin,
82                                 final int length)
83    {
84       try {
85          int i=length;
86          while (--i >= 0) {
87             if (a[abegin + i] != b[bbegin + i]) {
88                return false;
89             }
90          }
91       }
92       catch (ArrayIndexOutOfBoundsException JavaDoc e) {
93          return false;
94       }
95
96       return true;
97    }
98
99    /**
100     * Test the equality of two byte arrays.
101     *
102     * @param a The first byte array.
103     * @param b The second byte array.
104     * @return True if the byte arrays are equal.
105     */

106    public static boolean equals(final byte a[], final byte b[]) {
107       if (a == b) return true;
108       if (a == null || b == null) return false;
109       if (a.length != b.length) return false;
110
111       try {
112          for (int i=0; i<a.length; i++) {
113             if (a[i] != b[i]) {
114                return false;
115             }
116          }
117       }
118       catch (ArrayIndexOutOfBoundsException JavaDoc e) {
119          return false;
120       }
121
122       return true;
123    }
124
125 }
126
Popular Tags