Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/amqp/UnsignedShort.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 package org.apache.qpid.proton.amqp;
23
24 public final class UnsignedShort extends Number implements Comparable<UnsignedShort>
25 {
26 private final short _underlying;
27 private static final UnsignedShort[] cachedValues = new UnsignedShort[256];
28
29 public static final UnsignedShort MAX_VALUE = new UnsignedShort((short) -1);
30
31 static
32 {
33 for(short i = 0; i < 256; i++)
34 {
35 cachedValues[i] = new UnsignedShort(i);
36 }
37 }
38
39 public UnsignedShort(short underlying)
40 {
41 _underlying = underlying;
42 }
43
44 public short shortValue()
45 {
46 return _underlying;
47 }
48
49 @Override
50 public int intValue()
51 {
52 return _underlying & 0xFFFF;
53 }
54
55 @Override
56 public long longValue()
57 {
58 return ((long) _underlying) & 0xFFFFl;
59 }
60
61 @Override
62 public float floatValue()
63 {
64 return (float) intValue();
65 }
66
67 @Override
68 public double doubleValue()
69 {
70 return (double) intValue();
71 }
72
73 @Override
74 public boolean equals(Object o)
75 {
76 if (this == o)
77 {
78 return true;
79 }
80 if (o == null || getClass() != o.getClass())
81 {
82 return false;
83 }
84
85 UnsignedShort that = (UnsignedShort) o;
86
87 if (_underlying != that._underlying)
88 {
89 return false;
90 }
91
92 return true;
93 }
94
95 public int compareTo(UnsignedShort o)
96 {
97 return Integer.signum(intValue() - o.intValue());
98 }
99
100 @Override
101 public int hashCode()
102 {
103 return _underlying;
104 }
105
106 @Override
107 public String toString()
108 {
109 return String.valueOf(longValue());
110 }
111
112 public static UnsignedShort valueOf(short underlying)
113 {
114 if((underlying & 0xFF00) == 0)
115 {
116 return cachedValues[underlying];
117 }
118 else
119 {
120 return new UnsignedShort(underlying);
121 }
122 }
123
124 public static UnsignedShort valueOf(final String value)
125 {
126 int intVal = Integer.parseInt(value);
127 if(intVal < 0 || intVal >= (1<<16))
128 {
129 throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0 + "-" + (1<<16) +").");
130 }
131 return valueOf((short)intVal);
132
133 }
134 }