/[classpath]/classpath/java/lang/Long.java
ViewVC logotype

Diff of /classpath/java/lang/Long.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.13 by mark, Tue Jan 22 22:27:00 2002 UTC revision 1.14 by ericb, Mon Feb 25 01:36:03 2002 UTC
# Line 1  Line 1 
1  /* java.lang.Long  /* Long.java -- object wrapper for long
2     Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.     Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 7  GNU Classpath is free software; you can Line 7  GNU Classpath is free software; you can
7  it under the terms of the GNU General Public License as published by  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)  the Free Software Foundation; either version 2, or (at your option)
9  any later version.  any later version.
10    
11  GNU Classpath is distributed in the hope that it will be useful, but  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Line 39  exception statement from your version. * Line 39  exception statement from your version. *
39  package java.lang;  package java.lang;
40    
41  /**  /**
42   * Instances of class <code>Double</code> represent primitive   * Instances of class <code>Long</code> represent primitive
43   * <code>double</code> values.   * <code>long</code> values.
44   *   *
45   * Additionally, this class provides various helper functions and variables   * Additionally, this class provides various helper functions and variables
46   * related to longs.   * related to longs.
# Line 48  package java.lang; Line 48  package java.lang;
48   * @author Paul Fisher   * @author Paul Fisher
49   * @author John Keiser   * @author John Keiser
50   * @author Warren Levy   * @author Warren Levy
51   * @since JDK 1.0   * @author Eric Blake <ebb9@email.byu.edu>
52     * @since 1.0
53     * @status updated to 1.4
54   */   */
55  public final class Long extends Number implements Comparable  public final class Long extends Number implements Comparable
56  {  {
57    // compatible with JDK 1.0.2+    /**
58    static final long serialVersionUID = 4290774380558885855L;     * Compatible with JDK 1.0.2+.
59       */
60      private static final long serialVersionUID = 4290774380558885855L;
61    
62    /**    /**
63     * The minimum value a <code>long</code> can represent is     * The minimum value a <code>long</code> can represent is
64     * -9223372036854775808.     * -9223372036854775808L (or -2<sup>63</sup>).
65     */     */
66    public static final long MIN_VALUE = 0x8000000000000000L;    public static final long MIN_VALUE = 0x8000000000000000L;
67    
68    /**    /**
69     * The maximum value a <code>long</code> can represent is     * The maximum value a <code>long</code> can represent is
70     * 9223372036854775807.     * 9223372036854775807 (or 2<sup>63</sup> - 1).
71     */     */
72    public static final long MAX_VALUE = 0x7fffffffffffffffL;    public static final long MAX_VALUE = 0x7fffffffffffffffL;
73    
74    /**    /**
75     * The primitive type <code>long</code> is represented by this     * The primitive type <code>long</code> is represented by this
76     * <code>Class</code> object.     * <code>Class</code> object.
77       * @since 1.1
78     */     */
79    public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J');    public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J');
80    
81    /**    /**
82     * The immutable value of this Long.     * The immutable value of this Long.
83       *
84       * @serial the wrapped long
85     */     */
86    private final long value;    private final long value;
87    
88    /**    /**
89     * Create a <code>Long</code> object representing the value of the     * Create a <code>Long</code> object representing the value of the
90     * <code>long</code> argument.     * <code>long</code> argument.
91     *     *
92     * @param value the value to use     * @param value the value to use
# Line 90  public final class Long extends Number i Line 97  public final class Long extends Number i
97    }    }
98    
99    /**    /**
100     * Create a <code>Long</code> object representing the value of the     * Create a <code>Long</code> object representing the value of the
101     * argument after conversion to a <code>long</code>.     * argument after conversion to a <code>long</code>.
102     *     *
103     * @param s the string to convert.     * @param s the string to convert
104       * @throws NumberFormatException if the String does not contain a long
105       * @see #valueOf(String)
106     */     */
107    public Long(String s) throws NumberFormatException    public Long(String s)
108    {    {
109      value = parseLong(s, 10);      value = parseLong(s, 10);
110    }    }
111    
112    /**    /**
113     * If the <code>Object</code> is not <code>null</code>, is an     * Converts the <code>long</code> to a <code>String</code> using
114     * <code>instanceof</code> <code>Long</code>, and represents     * the specified radix (base). If the radix exceeds
115     * the same primitive <code>long</code> value return     * <code>Character.MIN_RADIX</code> or <code>Character.MAX_RADIX</code>, 10
116     * <code>true</code>.  Otherwise <code>false</code> is returned.     * is used instead. If the result is negative, the leading character is
117     */     * '-' ('\\u002D'). The remaining characters come from
118    public boolean equals(Object obj)     * <code>Character.forDigit(digit, radix)</code> ('0'-'9','a'-'z').
   {  
     return obj instanceof Long && ((Long)obj).value == value;  
   }  
   
   /**  
    * Return a hashcode representing this Object.  
    *  
    * <code>Long</code>'s hash code is calculated by simply returning its  
    * value.  
    *  
    * @return this Object's hash code.  
    */  
   public int hashCode()  
   {  
     return (int)(value^(value>>>32));  
   }  
   
   /**  
    * Get the specified system property as a <code>Long</code>.  
119     *     *
120     * A method similar to <code>Integer</code>'s <code>decode()</code> will be     * @param num the <code>long</code> to convert to <code>String</code>
121     * used to interpret the value of the property.     * @param radix the radix (base) to use in the conversion
122     *     * @return the <code>String</code> representation of the argument
    * @param nm the name of the system property  
    * @return the system property as an <code>Long</code>, or  
    *         <code>null</code> if the property is not found or cannot be  
    *         decoded as a <code>Long</code>.  
    * @see java.lang.System#getProperty(java.lang.String)  
    * @see java.lang.Integer#decode(int)  
123     */     */
124    public static Long getLong(String nm)    public static String toString(long num, int radix)
125    {    {
126      return getLong(nm, null);      // Use optimized method for the typical case.
127    }      if (radix == 10 ||
128            radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
129          return toString(num);
130    
131    /**      // Use the Integer toString for efficiency if possible.
132     * Get the specified system property as an <code>Long</code>, or use a      if ((int) num == num)
133     * default <code>long</code> value if the property is not found or is not        return Integer.toString((int) num, radix);
    * decodable.  
    *  
    * A method similar to <code>Integer</code>'s <code>decode()</code> will be  
    * used to interpret the value of the property.  
    *  
    * @param nm the name of the system property  
    * @param val the default value to use if the property is not found or not  
    *        a number.  
    * @return the system property as a <code>Long</code>, or the default  
    *         value if the property is not found or cannot be decoded as a  
    *         <code>Long</code>.  
    * @see java.lang.System#getProperty(java.lang.String)  
    * @see java.lang.Integer#decode(int)  
    * @see #getLong(java.lang.String,java.lang.Long)  
    */  
   public static Long getLong(String nm, long val)  
   {  
     Long result = getLong(nm, null);  
     return (result == null) ? new Long(val) : result;  
   }  
134    
135    /**      // For negative numbers, print out the absolute value w/ a leading '-'.
136     * Get the specified system property as an <code>Long</code>, or use a      // Use an array large enough for a binary number.
137     * default <code>Long</code> value if the property is not found or is      char[] buffer = new char[65];
138     * not decodable.      int i = 65;
139     *      boolean isNeg;
140     * The <code>decode()</code> method will be used to interpret the value of      if (num < 0)
    * the property.  
    *  
    * @param nm the name of the system property  
    * @param val the default value to use if the property is not found or not  
    *        a number.  
    * @return the system property as an <code>Long</code>, or the default  
    *         value if the property is not found or cannot be decoded as an  
    *         <code>Long</code>.  
    * @see java.lang.System#getProperty(java.lang.String)  
    * @see java.lang.Integer#decode(int)  
    * @see #getLong(java.lang.String,long)  
    */  
   public static Long getLong(String nm, Long def)  
   {  
     String val = System.getProperty(nm);  
     if (val == null)  
       return def;  
     try  
       {  
         return decode(nm);  
       }  
     catch (NumberFormatException e)  
141        {        {
142          return def;          isNeg = true;
143            num = -num;
144    
145            // When the value is MIN_VALUE, it overflows when made positive
146            if (num < 0)
147              {
148                buffer[--i] = Character.forDigit((int) (-(num + radix) % radix),
149                                                 radix);
150                num = -(num / radix);
151              }
152        }        }
153    }      else
154          isNeg = false;
155    
   private static String toUnsignedString(long num, int exp)  
   {  
     // Use an array large enough for a binary number.  
     int radix = 1 << exp;  
     int mask = radix - 1;  
     char[] buffer = new char[64];  
     int i = 64;  
156      do      do
157        {        {
158          buffer[--i] = Character.forDigit((int) num & mask, radix);          buffer[--i] = Character.forDigit((int) (num % radix), radix);
159          num = num >>> exp;          num /= radix;
160        }        }
161      while (num != 0);      while (num > 0);
162    
163      return String.valueOf(buffer, i, 64-i);      if (isNeg)
164          buffer[--i] = '-';
165    
166        return String.valueOf(buffer, i, 65 - i);
167    }    }
168    
169    /**    /**
170     * Converts the <code>long</code> to a <code>String</code> assuming it is     * Converts the <code>long</code> to a <code>String</code> assuming it is
171     * unsigned in base 16.     * unsigned in base 16.
172     * @param i the <code>long</code> to convert to <code>String</code>     *
173     * @return the <code>String</code> representation of the argument.     * @param l the <code>long</code> to convert to <code>String</code>
174       * @return the <code>String</code> representation of the argument
175     */     */
176    public static String toHexString(long i)    public static String toHexString(long l)
177    {    {
178      return toUnsignedString(i, 4);      return toUnsignedString(l, 4);
179    }    }
180    
181    /**    /**
182     * Converts the <code>long</code> to a <code>String</code> assuming it is     * Converts the <code>long</code> to a <code>String</code> assuming it is
183     * unsigned in base 8.     * unsigned in base 8.
184     * @param i the <code>long</code> to convert to <code>String</code>     *
185     * @return the <code>String</code> representation of the argument.     * @param l the <code>long</code> to convert to <code>String</code>
186       * @return the <code>String</code> representation of the argument
187     */     */
188    public static String toOctalString(long i)    public static String toOctalString(long l)
189    {    {
190      return toUnsignedString(i, 3);      return toUnsignedString(l, 3);
191    }    }
192    
193    /**    /**
194     * Converts the <code>long</code> to a <code>String</code> assuming it is     * Converts the <code>long</code> to a <code>String</code> assuming it is
195     * unsigned in base 2.     * unsigned in base 2.
196     * @param i the <code>long</code> to convert to <code>String</code>     *
197     * @return the <code>String</code> representation of the argument.     * @param l the <code>long</code> to convert to <code>String</code>
198       * @return the <code>String</code> representation of the argument
199     */     */
200    public static String toBinaryString(long i) {    public static String toBinaryString(long l)
201      return toUnsignedString(i, 1);    {
202        return toUnsignedString(l, 1);
203    }    }
204    
205    /**    /**
206     * Converts the <code>long</code> to a <code>String</code> and assumes     * Converts the <code>long</code> to a <code>String</code> and assumes
207     * a radix of 10.     * a radix of 10.
208       *
209     * @param num the <code>long</code> to convert to <code>String</code>     * @param num the <code>long</code> to convert to <code>String</code>
210     * @return the <code>String</code> representation of the argument.     * @return the <code>String</code> representation of the argument
211     */         * @see #toString(long, int)
212       */
213    public static String toString(long num)    public static String toString(long num)
214    {    {
215      // Use the Integer toString for efficiency if possible.      // Use the Integer toString for efficiency if possible.
216      if (num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE)      if ((int) num ==  num)
217        return Integer.toString((int) num);        return String.valueOf((int) num);
218    
219      // Use an array large enough for "-9223372036854775808"; i.e. 20 chars.      // Use an array large enough for "-9223372036854775808"; i.e. 20 chars.
220      char[] buffer = new char[20];      char[] buffer = new char[20];
# Line 288  public final class Long extends Number i Line 244  public final class Long extends Number i
244      if (isNeg)      if (isNeg)
245        buffer[--i] = '-';        buffer[--i] = '-';
246    
247      return String.valueOf(buffer, i, 20-i);      return String.valueOf(buffer, i, 20 - i);
248    }    }
249    
250    /**    /**
251     * Converts the <code>Long</code> value to a <code>String</code> and     * Converts the specified <code>String</code> into an <code>int</code>
252     * assumes a radix of 10.     * using the specified radix (base). The string must not be <code>null</code>
253     * @return the <code>String</code> representation of this <code>Long</code>.     * or empty. It may begin with an optional '-', which will negate the answer,
254     */         * provided that there are also valid digits. Each digit is parsed as if by
255    public String toString()     * <code>Character.digit(d, radix)</code>, and must be in the range
256    {     * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
257      return toString(value);     * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
258    }     * Unlike Double.parseDouble, you may not have a leading '+'; and 'l' or
259         * 'L' as the last character is only valid in radices 22 or greater, where
260    /**     * it is a digit and not a type indicator.
261     * Converts the <code>long</code> to a <code>String</code> using     *
262     * the specified radix (base).     * @param s the <code>String</code> to convert
263     * @param num the <code>long</code> to convert to <code>String</code>.     * @param radix the radix (base) to use in the conversion
264     * @param radix the radix (base) to use in the conversion.     * @return the <code>String</code> argument converted to </code>long</code>
265     * @return the <code>String</code> representation of the argument.     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
266       *         <code>long</code>
267     */     */
268    public static String toString(long num, int radix)    public static long parseLong(String str, int radix)
269    {    {
270      // Use optimized method for the typical case.      final int len;
     if (radix == 10 ||  
         radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)  
       return toString(num);  
   
     // Use the Integer toString for efficiency if possible.  
     if (num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE)  
       return Integer.toString((int) num, radix);  
   
     // For negative numbers, print out the absolute value w/ a leading '-'.  
     // Use an array large enough for a binary number.  
     char[] buffer = new char[65];  
     int i = 65;  
     boolean isNeg;  
     if (num < 0)  
       {  
         isNeg = true;  
         num = -(num);  
   
         // When the value is MIN_VALUE, it overflows when made positive  
         if (num < 0)  
           {  
             buffer[--i] = Character.forDigit((int) (-(num + radix) % radix),  
                                                 radix);  
             num = -(num / radix);  
           }  
       }  
     else  
       isNeg = false;  
271    
272      do      if ((len = str.length()) == 0 || radix < Character.MIN_RADIX
273        {           || radix > Character.MAX_RADIX)
274          buffer[--i] = Character.forDigit((int) (num % radix), radix);        throw new NumberFormatException();
         num /= radix;  
       }  
     while (num > 0);  
275    
276      if (isNeg)      boolean isNeg = false;
277        buffer[--i] = '-';      int index = 0;
278        if (str.charAt(index) == '-')
279          if (len > 1)
280            {
281              isNeg = true;
282              index++;
283            }
284          else
285            throw new NumberFormatException();
286    
287      return String.valueOf(buffer, i, 65-i);      return parseLong(str, index, len, isNeg, radix);
288    }    }
289        
290    /**    /**
291     * Creates a new <code>Long</code> object using the <code>String</code>,     * Converts the specified <code>String</code> into a <code>long</code>.
292     * assuming a radix of 10.     * This function assumes a radix of 10.
293     * @param s the <code>String</code> to convert.     *
294     * @return the new <code>Long</code>.     * @param s the <code>String</code> to convert
295     * @see #Long(java.lang.String)     * @return the <code>int</code> value of <code>s</code>
296     * @see #parseLong(java.lang.String)     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
297     * @exception NumberFormatException thrown if the <code>String</code>     *         <code>long</code>
298     * cannot be parsed as a <code>long</code>.     * @see #parseLong(String, int)
299     */     */
300    public static Long valueOf(String s) throws NumberFormatException    public static long parseLong(String s)
301    {    {
302      return new Long(parseLong(s));      return parseLong(s, 10);
303    }    }
304    
305    /**    /**
306     * Creates a new <code>Long</code> object using the <code>String</code>     * Creates a new <code>Long</code> object using the <code>String</code>
307     * and specified radix (base).     * and specified radix (base).
308     * @param s the <code>String</code> to convert.     *
309     * @param radix the radix (base) to convert with.     * @param s the <code>String</code> to convert
310     * @return the new <code>Long</code>.     * @param radix the radix (base) to convert with
311     * @see #parseLong(java.lang.String,int)     * @return the new <code>Long</code>
312     * @exception NumberFormatException thrown if the <code>String</code>     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
313     * cannot be parsed as a <code>long</code>.     *         <code>long</code>
314       * @see #parseLong(String, int)
315     */     */
316    public static Long valueOf(String s, int radix) throws NumberFormatException    public static Long valueOf(String s, int radix)
317    {    {
318      return new Long(parseLong(s, radix));      return new Long(parseLong(s, radix));
319    }    }
320    
321    /**    /**
322     * Converts the specified <code>String</code> into a <code>long</code>.     * Creates a new <code>Long</code> object using the <code>String</code>,
323     * This function assumes a radix of 10.     * assuming a radix of 10.
324     *     *
325     * @param s the <code>String</code> to convert     * @param s the <code>String</code> to convert
326     * @return the <code>long</code> value of the <code>String</code>     * @return the new <code>Long</code>
327     *         argument.     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
328     * @exception NumberFormatException thrown if the <code>String</code>     *         <code>long</code>
329     * cannot be parsed as a <code>long</code>.     * @see #Long(String)
330       * @see #parseLong(String)
331     */     */
332    public static long parseLong(String s) throws NumberFormatException    public static Long valueOf(String s)
333    {    {
334      return parseLong(s, 10);      return new Long(parseLong(s, 10));
335    }    }
336    
337    /**    /**
338     * Converts the specified <code>String</code> into a <code>long</code>     * Convert the specified <code>String</code> into a <code>Long</code>.
339     * using the specified radix (base).     * The <code>String</code> may represent decimal, hexadecimal, or
340       * octal numbers.
341     *     *
342     * @param s the <code>String</code> to convert     * <p>The extended BNF grammar is as follows:<br>
343     * @param radix the radix (base) to use in the conversion     * <pre>
344     * @return the <code>String</code> argument converted to </code>long</code>.     * <em>DecodableString</em>:
345     * @exception NumberFormatException thrown if the <code>String</code>     *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
346     * cannot be parsed as a <code>long</code>.         *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
347       *              | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
348       *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
349       * <em>DecimalNumber</em>:
350       *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
351       * <em>DecimalDigit</em>:
352       *        <em>Character.digit(d, 10) has value 0 to 9</em>
353       * <em>OctalDigit</em>:
354       *        <em>Character.digit(d, 8) has value 0 to 7</em>
355       * <em>DecimalDigit</em>:
356       *        <em>Character.digit(d, 16) has value 0 to 15</em>
357       * </pre>
358       * Note that you cannot decode MIN_VALUE, as the specification requires
359       * that the digits be parsed before negating the result, but
360       * 9223372036854775808 will not fit in a long. Also, you may not use a
361       * trailing 'l' or 'L', unlike in Java source code.
362       *
363       * @param s the <code>String</code> to interpret
364       * @return the value of the String as a <code>Long</code>
365       * @throws NumberFormatException if <code>s</code> cannot be parsed as a
366       *         <code>long</code>
367       * @throws NullPointerException if s is null
368       * @since 1.2
369     */     */
370    public static long parseLong(String str, int radix)    public static Long decode(String str)
     throws NumberFormatException  
   {  
     final int len;  
   
     if ((len = str.length()) == 0 || radix < Character.MIN_RADIX  
          || radix > Character.MAX_RADIX)  
       throw new NumberFormatException();  
   
     boolean isNeg = false;  
     int index = 0;  
     if (str.charAt(index) == '-')  
       if (len > 1)  
         {  
           isNeg = true;  
           index++;  
         }  
       else  
         throw new NumberFormatException();  
   
     return parseLong(str, index, len, isNeg, radix);  
   }  
   
   public static Long decode(String str) throws NumberFormatException  
371    {    {
372      boolean isNeg = false;      boolean isNeg = false;
373      int index = 0;      int index = 0;
# Line 475  public final class Long extends Number i Line 411  public final class Long extends Number i
411      return new Long(parseLong(str, index, len, isNeg, radix));      return new Long(parseLong(str, index, len, isNeg, radix));
412    }    }
413    
414    private static long parseLong(String str, int index, int len, boolean isNeg,    /**
415                                  int radix) throws NumberFormatException     * Return the value of this <code>Long</code> as a <code>byte</code>.
416    {     *
417      long val = 0;     * @return the byte value
418      int digval;     */
   
     long max = MAX_VALUE / radix;  
     // We can't directly write `max = (MAX_VALUE + 1) / radix'.  
     // So instead we fake it.  
     if (isNeg && MAX_VALUE % radix == radix - 1)  
       ++max;  
   
     for ( ; index < len; index++)  
       {  
         if (val < 0 || val > max)  
           throw new NumberFormatException();  
   
         if ((digval = Character.digit(str.charAt(index), radix)) < 0)  
           throw new NumberFormatException();  
   
         // Throw an exception for overflow if result is negative.  
         // However, we special-case the most negative value.  
         val = val * radix + digval;  
         if (val < 0 && (! isNeg || val != MIN_VALUE))  
           throw new NumberFormatException();  
       }  
   
     return isNeg ? -(val) : val;  
   }  
   
   /** Return the value of this <code>Long</code> as an <code>short</code>.  
    ** @return the value of this <code>Long</code> as an <code>short</code>.  
    **/  
419    public byte byteValue()    public byte byteValue()
420    {    {
421      return (byte) value;      return (byte) value;
422    }    }
423    
424    /** Return the value of this <code>Long</code> as an <code>short</code>.    /**
425     ** @return the value of this <code>Long</code> as an <code>short</code>.     * Return the value of this <code>Long</code> as a <code>short</code>.
426     **/     *
427       * @return the short value
428       */
429    public short shortValue()    public short shortValue()
430    {    {
431      return (short) value;      return (short) value;
432    }    }
433    
434    /** Return the value of this <code>Long</code> as an <code>int</code>.    /**
435     ** @return the value of this <code>Long</code> as an <code>int</code>.     * Return the value of this <code>Long</code> as an <code>int</code>.
436     **/     *
437       * @return the int value
438       */
439    public int intValue()    public int intValue()
440    {    {
441      return (int) value;      return (int) value;
442    }    }
443    
444    /** Return the value of this <code>Long</code> as a <code>long</code>.    /**
445     ** @return the value of this <code>Long</code> as a <code>long</code>.     * Return the value of this <code>Long</code>.
446     **/     *
447       * @return the long value
448       */
449    public long longValue()    public long longValue()
450    {    {
451      return value;      return value;
452    }    }
453    
454    /** Return the value of this <code>Long</code> as a <code>float</code>.    /**
455     ** @return the value of this <code>Long</code> as a <code>float</code>.     * Return the value of this <code>Long</code> as a <code>float</code>.
456     **/     *
457       * @return the float value
458       */
459    public float floatValue()    public float floatValue()
460    {    {
461      return value;      return value;
462    }    }
463    
464    /** Return the value of this <code>Long</code> as a <code>double</code>.    /**
465     ** @return the value of this <code>Long</code> as a <code>double</code>.     * Return the value of this <code>Long</code> as a <code>double</code>.
466     **/     *
467       * @return the double value
468       */
469    public double doubleValue()    public double doubleValue()
470    {    {
471      return value;      return value;
472    }    }
473    
474    /**    /**
475     * Compare two Longs numerically by comparing their     * Converts the <code>Long</code> value to a <code>String</code> and
476     * <code>long</code> values.     * assumes a radix of 10.
477     * @return a positive value if this <code>Long</code> is greater     *
478     * in value than the argument <code>Long</code>; a negative value     * @return the <code>String</code> representation
479     * if this <code>Long</code> is smaller in value than the argument     */
480     * <code>Long</code>; and <code>0</code>, zero, if this    public String toString()
481     * <code>Long</code> is equal in value to the argument    {
482     * <code>Long</code>.        return toString(value, 10);
483      }
484    
485      /**
486       * Return a hashcode representing this Object. <code>Long</code>'s hash
487       * code is calculated by <code>(int) (value ^ (value &gt;&gt; 32))</code>.
488       *
489       * @return this Object's hash code
490       */
491      public int hashCode()
492      {
493        return (int) (value ^ (value >>> 32));
494      }
495    
496      /**
497       * Returns <code>true</code> if <code>obj</code> is an instance of
498       * <code>Long</code> and represents the same long value.
499       *
500       * @param obj the object to compare
501       * @return whether these Objects are semantically equal
502       */
503      public boolean equals(Object obj)
504      {
505        return obj instanceof Long && value == ((Long) obj).value;
506      }
507    
508      /**
509       * Get the specified system property as a <code>Long</code>. The
510       * <code>decode()</code> method will be used to interpret the value of
511       * the property.
512       *
513       * @param nm the name of the system property
514       * @return the system property as a <code>Long</code>, or null if the
515       *         property is not found or cannot be decoded
516       * @throws SecurityException if accessing the system property is forbidden
517       * @see System#getProperty(String)
518       * @see #decode(String)
519       */
520      public static Long getLong(String nm)
521      {
522        return getLong(nm, null);
523      }
524    
525      /**
526       * Get the specified system property as a <code>Long</code>, or use a
527       * default <code>long</code> value if the property is not found or is not
528       * decodable. The <code>decode()</code> method will be used to interpret
529       * the value of the property.
530       *
531       * @param nm the name of the system property
532       * @param val the default value
533       * @return the value of the system property, or the default
534       * @throws SecurityException if accessing the system property is forbidden
535       * @see System#getProperty(String)
536       * @see #decode(String)
537       */
538      public static Long getLong(String nm, long val)
539      {
540        Long result = getLong(nm, null);
541        return result == null ? new Long(val) : result;
542      }
543    
544      /**
545       * Get the specified system property as a <code>Long</code>, or use a
546       * default <code>Long</code> value if the property is not found or is
547       * not decodable. The <code>decode()</code> method will be used to
548       * interpret the value of the property.
549     *     *
550       * @param nm the name of the system property
551       * @param val the default value
552       * @return the value of the system property, or the default
553       * @throws SecurityException if accessing the system property is forbidden
554       * @see System#getProperty(String)
555       * @see #decode(String)
556       */
557      public static Long getLong(String nm, Long def)
558      {
559        String val = System.getProperty(nm);
560        if (val == null)
561          return def;
562        try
563          {
564            return decode(nm);
565          }
566        catch (NumberFormatException e)
567          {
568            return def;
569          }
570      }
571    
572      /**
573       * Compare two Longs numerically by comparing their <code>long</code>
574       * values. The result is positive if the first is greater, negative if the
575       * second is greater, and 0 if the two are equal.
576       *
577       * @param l the Long to compare
578       * @return the comparison
579     * @since 1.2     * @since 1.2
580     */     */
581    public int compareTo(Long l)    public int compareTo(Long l)
582    {    {
583      if (this.value == l.value)      if (value == l.value)
584        return 0;        return 0;
   
585      // Returns just -1 or 1 on inequality; doing math might overflow the long.      // Returns just -1 or 1 on inequality; doing math might overflow the long.
586      if (this.value > l.value)      return value > i.value ? 1 : -1;
       return 1;  
   
     return -1;  
587    }    }
588        
589    /**    /**
590     * Behaves like <code>compareTo(java.lang.Long)</code> unless the Object     * Behaves like <code>compareTo(Long)</code> unless the Object
591     * is not a <code>Long</code>.  Then it throws a     * is not a <code>Long</code>.
    * <code>ClassCastException</code>.  
    * @exception ClassCastException if the argument is not a  
    * <code>Long</code>.  
592     *     *
593       * @param o the object to compare
594       * @return the comparison
595       * @throws ClassCastException if the argument is not a <code>Long</code>
596       * @see #compareTo(Long)
597       * @see Comparable
598     * @since 1.2     * @since 1.2
599     */     */
600    public int compareTo(Object o)    public int compareTo(Object o)
601    {    {
602      return compareTo((Long)o);      return compareTo((Long) o);
603      }
604    
605      /**
606       * Helper for converting unsigned numbers to String.
607       *
608       * @param num the number
609       * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex)
610       */
611      private static String toUnsignedString(long num, int exp)
612      {
613        // Use an array large enough for a binary number.
614        int radix = 1 << exp;
615        int mask = radix - 1;
616        char[] buffer = new char[64];
617        int i = 64;
618        do
619          {
620            buffer[--i] = Character.forDigit((int) num & mask, radix);
621            num = num >>> exp;
622          }
623        while (num != 0);
624    
625        return String.valueOf(buffer, i, 64 - i);
626      }
627    
628      /**
629       * Helper for parsing longs.
630       *
631       * @param str the string to parse
632       * @param index the index to start at
633       * @param len the string length
634       * @param isNeg if the result should be negated
635       * @param radix the radix to use
636       * @return the parsed long value
637       * @throws NumberFormatException if there is an error
638       */
639      private static long parseLong(String str, int index, int len, boolean isNeg,
640                                    int radix)
641      {
642        long val = 0;
643        int digval;
644    
645        long max = MAX_VALUE / radix;
646        // We can't directly write `max = (MAX_VALUE + 1) / radix'.
647        // So instead we fake it.
648        if (isNeg && MAX_VALUE % radix == radix - 1)
649          ++max;
650    
651        for ( ; index < len; index++)
652          {
653            if (val < 0 || val > max)
654              throw new NumberFormatException();
655    
656            if ((digval = Character.digit(str.charAt(index), radix)) < 0)
657              throw new NumberFormatException();
658    
659            // Throw an exception for overflow if result is negative.
660            // However, we special-case the most negative value.
661            val = val * radix + digval;
662            if (val < 0 && (! isNeg || val != MIN_VALUE))
663              throw new NumberFormatException();
664          }
665    
666        return isNeg ? -val : val;
667    }    }
668  }  }

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.14

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26