src/share/classes/java/lang/Long.java

Print this page




 789      * Compares this object to the specified object.  The result is
 790      * {@code true} if and only if the argument is not
 791      * {@code null} and is a {@code Long} object that
 792      * contains the same {@code long} value as this object.
 793      *
 794      * @param   obj   the object to compare with.
 795      * @return  {@code true} if the objects are the same;
 796      *          {@code false} otherwise.
 797      */
 798     public boolean equals(Object obj) {
 799         if (obj instanceof Long) {
 800             return value == ((Long)obj).longValue();
 801         }
 802         return false;
 803     }
 804 
 805     /**
 806      * Determines the {@code long} value of the system property
 807      * with the specified name.
 808      *
 809      * <p>The first argument is treated as the name of a system property.
 810      * System properties are accessible through the {@link
 811      * java.lang.System#getProperty(java.lang.String)} method. The
 812      * string value of this property is then interpreted as a
 813      * {@code long} value and a {@code Long} object
 814      * representing this value is returned.  Details of possible
 815      * numeric formats can be found with the definition of
 816      * {@code getProperty}.
 817      *
 818      * <p>If there is no property with the specified name, if the
 819      * specified name is empty or {@code null}, or if the
 820      * property does not have the correct numeric format, then
 821      * {@code null} is returned.
 822      *
 823      * <p>In other words, this method returns a {@code Long} object equal to
 824      * the value of:
 825      *
 826      * <blockquote>
 827      *  {@code getLong(nm, null)}
 828      * </blockquote>
 829      *
 830      * @param   nm   property name.
 831      * @return  the {@code Long} value of the property.
 832      * @see     java.lang.System#getProperty(java.lang.String)
 833      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 834      */
 835     public static Long getLong(String nm) {
 836         return getLong(nm, null);
 837     }
 838 
 839     /**
 840      * Determines the {@code long} value of the system property
 841      * with the specified name.
 842      *
 843      * <p>The first argument is treated as the name of a system property.
 844      * System properties are accessible through the {@link
 845      * java.lang.System#getProperty(java.lang.String)} method. The
 846      * string value of this property is then interpreted as a
 847      * {@code long} value and a {@code Long} object
 848      * representing this value is returned.  Details of possible
 849      * numeric formats can be found with the definition of
 850      * {@code getProperty}.
 851      *
 852      * <p>The second argument is the default value. A {@code Long} object
 853      * that represents the value of the second argument is returned if there
 854      * is no property of the specified name, if the property does not have
 855      * the correct numeric format, or if the specified name is empty or null.
 856      *
 857      * <p>In other words, this method returns a {@code Long} object equal
 858      * to the value of:
 859      *
 860      * <blockquote>
 861      *  {@code getLong(nm, new Long(val))}
 862      * </blockquote>
 863      *
 864      * but in practice it may be implemented in a manner such as:
 865      *
 866      * <blockquote><pre>
 867      * Long result = getLong(nm, null);
 868      * return (result == null) ? new Long(val) : result;
 869      * </pre></blockquote>
 870      *


 872      * the default value is not needed.
 873      *
 874      * @param   nm    property name.
 875      * @param   val   default value.
 876      * @return  the {@code Long} value of the property.
 877      * @see     java.lang.System#getProperty(java.lang.String)
 878      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 879      */
 880     public static Long getLong(String nm, long val) {
 881         Long result = Long.getLong(nm, null);
 882         return (result == null) ? Long.valueOf(val) : result;
 883     }
 884 
 885     /**
 886      * Returns the {@code long} value of the system property with
 887      * the specified name.  The first argument is treated as the name
 888      * of a system property.  System properties are accessible through
 889      * the {@link java.lang.System#getProperty(java.lang.String)}
 890      * method. The string value of this property is then interpreted
 891      * as a {@code long} value, as per the
 892      * {@code Long.decode} method, and a {@code Long} object
 893      * representing this value is returned.
 894      *
 895      * <ul>
 896      * <li>If the property value begins with the two ASCII characters
 897      * {@code 0x} or the ASCII character {@code #}, not followed by
 898      * a minus sign, then the rest of it is parsed as a hexadecimal integer
 899      * exactly as for the method {@link #valueOf(java.lang.String, int)}
 900      * with radix 16.
 901      * <li>If the property value begins with the ASCII character
 902      * {@code 0} followed by another character, it is parsed as
 903      * an octal integer exactly as by the method {@link
 904      * #valueOf(java.lang.String, int)} with radix 8.
 905      * <li>Otherwise the property value is parsed as a decimal
 906      * integer exactly as by the method
 907      * {@link #valueOf(java.lang.String, int)} with radix 10.
 908      * </ul>
 909      *
 910      * <p>Note that, in every case, neither {@code L}
 911      * (<code>'&#92;u004C'</code>) nor {@code l}
 912      * (<code>'&#92;u006C'</code>) is permitted to appear at the end
 913      * of the property value as a type indicator, as would be
 914      * permitted in Java programming language source code.
 915      *
 916      * <p>The second argument is the default value. The default value is
 917      * returned if there is no property of the specified name, if the
 918      * property does not have the correct numeric format, or if the
 919      * specified name is empty or {@code null}.
 920      *
 921      * @param   nm   property name.
 922      * @param   val   default value.
 923      * @return  the {@code Long} value of the property.
 924      * @see     java.lang.System#getProperty(java.lang.String)
 925      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
 926      * @see java.lang.Long#decode
 927      */
 928     public static Long getLong(String nm, Long val) {
 929         String v = null;
 930         try {
 931             v = System.getProperty(nm);
 932         } catch (IllegalArgumentException e) {
 933         } catch (NullPointerException e) {
 934         }
 935         if (v != null) {
 936             try {
 937                 return Long.decode(v);
 938             } catch (NumberFormatException e) {
 939             }
 940         }
 941         return val;
 942     }
 943 
 944     /**
 945      * Compares two {@code Long} objects numerically.
 946      *
 947      * @param   anotherLong   the {@code Long} to be compared.
 948      * @return  the value {@code 0} if this {@code Long} is
 949      *          equal to the argument {@code Long}; a value less than
 950      *          {@code 0} if this {@code Long} is numerically less
 951      *          than the argument {@code Long}; and a value greater
 952      *          than {@code 0} if this {@code Long} is numerically
 953      *           greater than the argument {@code Long} (signed




 789      * Compares this object to the specified object.  The result is
 790      * {@code true} if and only if the argument is not
 791      * {@code null} and is a {@code Long} object that
 792      * contains the same {@code long} value as this object.
 793      *
 794      * @param   obj   the object to compare with.
 795      * @return  {@code true} if the objects are the same;
 796      *          {@code false} otherwise.
 797      */
 798     public boolean equals(Object obj) {
 799         if (obj instanceof Long) {
 800             return value == ((Long)obj).longValue();
 801         }
 802         return false;
 803     }
 804 
 805     /**
 806      * Determines the {@code long} value of the system property
 807      * with the specified name.
 808      *
 809      * <p>The first argument is treated as the name of a system
 810      * property.  System properties are accessible through the {@link
 811      * java.lang.System#getProperty(java.lang.String)} method. The
 812      * string value of this property is then interpreted as a {@code
 813      * long} value using the grammar supported by {@link Long#decode decode}
 814      * and a {@code Long} object representing this value is returned.


 815      *
 816      * <p>If there is no property with the specified name, if the
 817      * specified name is empty or {@code null}, or if the property
 818      * does not have the correct numeric format, then {@code null} is
 819      * returned.
 820      *
 821      * <p>In other words, this method returns a {@code Long} object
 822      * equal to the value of:
 823      *
 824      * <blockquote>
 825      *  {@code getLong(nm, null)}
 826      * </blockquote>
 827      *
 828      * @param   nm   property name.
 829      * @return  the {@code Long} value of the property.
 830      * @see     java.lang.System#getProperty(java.lang.String)
 831      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 832      */
 833     public static Long getLong(String nm) {
 834         return getLong(nm, null);
 835     }
 836 
 837     /**
 838      * Determines the {@code long} value of the system property
 839      * with the specified name.
 840      *
 841      * <p>The first argument is treated as the name of a system
 842      * property.  System properties are accessible through the {@link
 843      * java.lang.System#getProperty(java.lang.String)} method. The
 844      * string value of this property is then interpreted as a {@code
 845      * long} value using the grammar supported by {@link Long#decode decode}
 846      * and a {@code Long} object representing this value is returned.


 847      *
 848      * <p>The second argument is the default value. A {@code Long} object
 849      * that represents the value of the second argument is returned if there
 850      * is no property of the specified name, if the property does not have
 851      * the correct numeric format, or if the specified name is empty or null.
 852      *
 853      * <p>In other words, this method returns a {@code Long} object equal
 854      * to the value of:
 855      *
 856      * <blockquote>
 857      *  {@code getLong(nm, new Long(val))}
 858      * </blockquote>
 859      *
 860      * but in practice it may be implemented in a manner such as:
 861      *
 862      * <blockquote><pre>
 863      * Long result = getLong(nm, null);
 864      * return (result == null) ? new Long(val) : result;
 865      * </pre></blockquote>
 866      *


 868      * the default value is not needed.
 869      *
 870      * @param   nm    property name.
 871      * @param   val   default value.
 872      * @return  the {@code Long} value of the property.
 873      * @see     java.lang.System#getProperty(java.lang.String)
 874      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 875      */
 876     public static Long getLong(String nm, long val) {
 877         Long result = Long.getLong(nm, null);
 878         return (result == null) ? Long.valueOf(val) : result;
 879     }
 880 
 881     /**
 882      * Returns the {@code long} value of the system property with
 883      * the specified name.  The first argument is treated as the name
 884      * of a system property.  System properties are accessible through
 885      * the {@link java.lang.System#getProperty(java.lang.String)}
 886      * method. The string value of this property is then interpreted
 887      * as a {@code long} value, as per the
 888      * {@link Long#decode decode} method, and a {@code Long} object
 889      * representing this value is returned; in summary:
 890      *
 891      * <ul>
 892      * <li>If the property value begins with the two ASCII characters
 893      * {@code 0x} or the ASCII character {@code #}, not followed by
 894      * a minus sign, then the rest of it is parsed as a hexadecimal integer
 895      * exactly as for the method {@link #valueOf(java.lang.String, int)}
 896      * with radix 16.
 897      * <li>If the property value begins with the ASCII character
 898      * {@code 0} followed by another character, it is parsed as
 899      * an octal integer exactly as by the method {@link
 900      * #valueOf(java.lang.String, int)} with radix 8.
 901      * <li>Otherwise the property value is parsed as a decimal
 902      * integer exactly as by the method
 903      * {@link #valueOf(java.lang.String, int)} with radix 10.
 904      * </ul>
 905      *
 906      * <p>Note that, in every case, neither {@code L}
 907      * (<code>'&#92;u004C'</code>) nor {@code l}
 908      * (<code>'&#92;u006C'</code>) is permitted to appear at the end
 909      * of the property value as a type indicator, as would be
 910      * permitted in Java programming language source code.
 911      *
 912      * <p>The second argument is the default value. The default value is
 913      * returned if there is no property of the specified name, if the
 914      * property does not have the correct numeric format, or if the
 915      * specified name is empty or {@code null}.
 916      *
 917      * @param   nm   property name.
 918      * @param   val   default value.
 919      * @return  the {@code Long} value of the property.
 920      * @see     System#getProperty(java.lang.String)
 921      * @see     System#getProperty(java.lang.String, java.lang.String)

 922      */
 923     public static Long getLong(String nm, Long val) {
 924         String v = null;
 925         try {
 926             v = System.getProperty(nm);
 927         } catch (IllegalArgumentException | NullPointerException e) {

 928         }
 929         if (v != null) {
 930             try {
 931                 return Long.decode(v);
 932             } catch (NumberFormatException e) {
 933             }
 934         }
 935         return val;
 936     }
 937 
 938     /**
 939      * Compares two {@code Long} objects numerically.
 940      *
 941      * @param   anotherLong   the {@code Long} to be compared.
 942      * @return  the value {@code 0} if this {@code Long} is
 943      *          equal to the argument {@code Long}; a value less than
 944      *          {@code 0} if this {@code Long} is numerically less
 945      *          than the argument {@code Long}; and a value greater
 946      *          than {@code 0} if this {@code Long} is numerically
 947      *           greater than the argument {@code Long} (signed