Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/amqp/UnsignedByte.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 UnsignedByte extends Number implements Comparable<UnsignedByte>
25 {
26 private final byte _underlying;
27 private static final UnsignedByte[] cachedValues = new UnsignedByte[256];
28
29 static
30 {
31 for(int i = 0; i<256; i++)
32 {
33 cachedValues[i] = new UnsignedByte((byte)i);
34 }
35 }
36
37 public UnsignedByte(byte underlying)
38 {
39 _underlying = underlying;
40 }
41
42 @Override
43 public byte byteValue()
44 {
45 return _underlying;
46 }
47
48 @Override
49 public short shortValue()
50 {
51 return (short) intValue();
52 }
53
54 @Override
55 public int intValue()
56 {
57 return ((int)_underlying) & 0xFF;
58 }
59
60 @Override
61 public long longValue()
62 {
63 return ((long) _underlying) & 0xFFl;
64 }
65
66 @Override
67 public float floatValue()
68 {
69 return (float) longValue();
70 }
71
72 @Override
73 public double doubleValue()
74 {
75 return (double) longValue();
76 }
77
78 @Override
79 public boolean equals(Object o)
80 {
81 if (this == o)
82 {
83 return true;
84 }
85 if (o == null || getClass() != o.getClass())
86 {
87 return false;
88 }
89
90 UnsignedByte that = (UnsignedByte) o;
91
92 if (_underlying != that._underlying)
93 {
94 return false;
95 }
96
97 return true;
98 }
99
100 public int compareTo(UnsignedByte o)
101 {
102 return Integer.signum(intValue() - o.intValue());
103 }
104
105 @Override
106 public int hashCode()
107 {
108 return _underlying;
109 }
110
111 @Override
112 public String toString()
113 {
114 return String.valueOf(intValue());
115 }
116
117 public static UnsignedByte valueOf(byte underlying)
118 {
119 final int index = ((int) underlying) & 0xFF;
120 return cachedValues[index];
121 }
122
123 public static UnsignedByte valueOf(final String value)
124 throws NumberFormatException
125 {
126 int intVal = Integer.parseInt(value);
127 if(intVal < 0 || intVal >= (1<<8))
128 {
129 throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0 + "-" + (1<<8) +").");
130 }
131 return valueOf((byte)intVal);
132 }
133
134 }