等牛奶的咖啡

经营属于我们的咖啡屋

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  0 随笔 :: 8 文章 :: 0 评论 :: 0 Trackbacks
  1 /**
  2  * 提供程序设计的基础类
  3  */
  4 package java.lang;
  5 
  6 /**
  7  * 将一个基本类型 short 的值包装在一个 Short 对象中
  8  */
  9 public final class Short extends Number implements Comparable<Short> {
 10 
 11     /** 常量,表示 short 类型的最小值 * */
 12     public static final short MIN_VALUE = -32768;
 13 
 14     /** 常量,表示 short 类型的最大值 * */
 15     public static final short MAX_VALUE = 32767;
 16 
 17     /** 用于以二进制补码形式表示 short 值的位数 */
 18     public static final int SIZE = 16;
 19 
 20     /** 表示基本类型 short 的 Class 对象 */
 21     public static final Class<Short> TYPE = (Class<Short>) Class
 22             .getPrimitiveClass("short");
 23 
 24     /** 定义一个私有变量,类型为 short */
 25     private final short value;
 26 
 27     /** 表明类的不同版本间的兼容性 */
 28     private static final long serialVersionUID = 7515723908773894738L;
 29 
 30     /**
 31      * 构造器,参数为基本类型 short
 32      */
 33     public Short(short value) {
 34         this.value = value;
 35     }
 36 
 37     /**
 38      * 构造器,参数为 String 该字符串要存在 Short 类型的范围
 39      */
 40     public Short(String s) throws NumberFormatException {
 41         this.value = parseShort(s, 10);
 42     }
 43 
 44     /**
 45      * 将 Short 对象强制转换为基本类型 byte 输出
 46      * 覆盖了父类 Number 中的 byteValue() 方法
 47      */
 48     public byte byteValue() {
 49         return (byte) value;
 50     }
 51 
 52     /**
 53      * 将 Short 对象的值作为基本类型 short 输出
 54      * 覆盖了父类 Number 中的 shortValue() 方法
 55      */
 56     public short shortValue() {
 57         return value;
 58     }
 59 
 60     /**
 61      * 将 Short 对象强制转换为基本类型 int 输出
 62      * 定义了父类 Number 中的抽象方法 intValue()
 63      */
 64     public int intValue() {
 65         return (int) value;
 66     }
 67 
 68     /**
 69      * 将 Short 对象强制转换为基本类型 long 输出
 70      * 定义了父类 Number 中的抽象方法 longValue()
 71      */
 72     public long longValue() {
 73         return (long) value;
 74     }
 75 
 76     /**
 77      * 将 Short 对象强制转换为基本类型 float 输出
 78      * 定义了父类 Number 中的抽象方法 floatValue()
 79      */
 80     public float floatValue() {
 81         return (float) value;
 82     }
 83 
 84     /**
 85      * 将 Short 对象强制转换为基本类型 double 输出
 86      * 定义了父类 Number 中的抽象方法 doubleValue()
 87      */
 88     public double doubleValue() {
 89         return (double) value;
 90     }
 91 
 92     /**
 93      * 返回表示指定 short 的 String 对象,以基数为 10 计算
 94      */
 95     public static String toString(short s) {
 96         return Integer.toString((int) s, 10);
 97     }
 98 
 99     /**
100      * 返回表示此 Short 对象值的 String 对象
101      */
102     public String toString() {
103         return String.valueOf((int) value);
104     }
105 
106     /**
107      * 内部类 ShortCache 准备把256个 short 存在缓存里
108      */
109     private static class ShortCache {
110         private ShortCache() {
111         }
112 
113         static final Short cache[] = new Short[-(-128+ 127 + 1];
114 
115         static {
116             for (int i = 0; i < cache.length; i++)
117                 cache[i] = new Short((short) (i - 128));
118         }
119     }
120 
121 
122     /**
123      * 返回一个表示基本类型 short 值的 Short 对象
124      * 直接从内部类中取,比构造器效率高
125      */
126     public static Short valueOf(short s) {
127         final int offset = 128;
128         int sAsInt = s;
129         if (sAsInt >= -128 && sAsInt <= 127) {
130             return ShortCache.cache[sAsInt + offset];
131         }
132         return new Short(s);
133     }
134 
135     /**
136      * 将 String 对象解析为有符号的 10 进制的 Short 对象,第一个参数为 String ,第二个参数为基数,范围[2,36]
137      */
138     public static Short valueOf(String s, int radix)
139             throws NumberFormatException {
140         return new Short(parseShort(s, radix));
141     }
142 
143     /**
144      * 将 String 对象解析为有符号的 10 进制基本类型 short
145      */
146     public static Short valueOf(String s) throws NumberFormatException {
147         return valueOf(s, 10);
148     }
149 
150     /**
151      * 将 String 对象解析为有符号的 10 进制基本类型 short
152      */
153     public static short parseShort(String s) throws NumberFormatException {
154         return parseShort(s, 10);
155     }
156 
157     /**
158      * 将 String 对象解析为有符号的 10 进制基本类型 short 第一个参数为 String ,第二个参数为基数,范围[2,36]
159      * 调用的主要方法是 Integer.parseInt()
160      * 由第二个参数指定基数
161      */
162     public static short parseShort(String s, int radix)
163             throws NumberFormatException {
164         int i = Integer.parseInt(s, radix);
165         if (i < MIN_VALUE || i > MAX_VALUE)
166             throw new NumberFormatException("Value out of range. Value:\"" + s
167                     + "\" Radix:" + radix);
168         return (short) i;
169     }
170 
171     /**
172      * 将 String 对象解析为有符号的 10 进制基本类型 short ,String 对象前的 - 对应负数 0x 0X # 对应 16 进制 0
173      * 对应 8 进制
174      * 直接由字符串的前缀来判断该字符串的类型
175      * 最终还是调用 parseShort() 转到调用 Integer.parseInt()
176      */
177     public static Short decode(String nm) throws NumberFormatException {
178         /** 用于确定基数 **/
179         int radix = 10;
180         /** 用于定位数值部分开始的位置 **/
181         int index = 0;
182         /** 用于确定 正负 **/
183         boolean negative = false;
184         Short result;
185 
186         /** 定位数值部分开始的位置 **/
187         if (nm.startsWith("-")) {
188             negative = true;
189             index++;
190         }
191         if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
192             index += 2;
193             radix = 16;
194         } else if (nm.startsWith("#", index)) {
195             index++;
196             radix = 16;
197         } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
198             index++;
199             radix = 8;
200         }
201 
202         if (nm.startsWith("-", index))
203             throw new NumberFormatException("Negative sign in wrong position");
204 
205         /** 调用 valueOf()方法进行解析 **/
206         try {
207             result = Short.valueOf(nm.substring(index), radix);
208             result = negative ? new Short((short-result.shortValue())
209                     : result;
210         } catch (NumberFormatException e) {
211             String constant = negative ? new String("-" + nm.substring(index))
212                     : nm.substring(index);
213             result = Short.valueOf(constant, radix);
214         }
215         return result;
216     }
217 
218     /**
219      * 返回此 Short 的哈希码 即将 Short 对象的值强制转换成基本类型 int
220      */
221     public int hashCode() {
222         return (int) value;
223     }
224 
225     /**
226      * 比较两个 Short 对象是否相同 当且仅当参数是一个与此对象一样,都表示同一个 short 值的 Short 对象时,才返回 true
227      */
228     public boolean equals(Object obj) {
229         if (obj instanceof Short) {
230             return value == ((Short) obj).shortValue();
231         }
232         return false;
233     }
234 
235     /**
236      * 将此 Short 实例与其他 Short 实例进行比较,true 为 0 false 为 非0
237      */
238     public int compareTo(Short anotherShort) {
239         return this.value - anotherShort.value;
240     }
241 
242     /**
243      * 返回通过反转(或者交换,效果相同)指定 short 值中的字节而获得的值
244      * 按字节倒置  在网络传输处理时特有用
245      */
246     public static short reverseBytes(short i) {
247         return (short) (((i & 0xFF00>> 8| (i << 8));
248     }
249 }
250 
posted on 2009-10-30 20:29 等牛奶的咖啡 阅读(324) 评论(0)  编辑  收藏 所属分类: JDK源代码

只有注册用户登录后才能发表评论。


网站导航: