001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: /**
019: * Wraps a primitive <code>long</code> as an object.
020: */
021: public final class Long extends Number implements Comparable<Long> {
022: public static final long MIN_VALUE = 0x8000000000000000L;
023: public static final long MAX_VALUE = 0x7fffffffffffffffL;
024: public static final int SIZE = 64;
025:
026: // Box values according to JLS - between -128 and 127
027: private static Long[] boxedValues = new Long[256];
028:
029: public static int bitCount(long i) {
030: int cnt = 0;
031: for (long q = MIN_VALUE; q > 0; q = q >> 1) {
032: if ((q & i) != 0) {
033: cnt++;
034: }
035: }
036: return cnt;
037: }
038:
039: public static Long decode(String s) throws NumberFormatException {
040: return new Long(
041: __decodeAndValidateLong(s, MIN_VALUE, MAX_VALUE));
042: }
043:
044: /**
045: * @skip Here for shared implementation with Arrays.hashCode
046: */
047: public static int hashCode(long l) {
048: return (int) l;
049: }
050:
051: public static long highestOneBit(long i) {
052: if (i < 0) {
053: return MIN_VALUE;
054: } else {
055: long rtn;
056: for (rtn = 0x4000000000000000L; (rtn >> 1) > i; rtn = rtn >> 1) {
057: // loop down until smaller
058: }
059: return rtn;
060: }
061: }
062:
063: public static long lowestOneBit(long i) {
064: if (i == 0) {
065: return SIZE;
066: } else {
067: long r = 1;
068: while ((r & i) != 0) {
069: r = r << 1;
070: }
071: return r;
072: }
073: }
074:
075: public static int numberOfLeadingZeros(long i) {
076: if (i < 0) {
077: return 0;
078: } else if (i == 0) {
079: return SIZE;
080: } else {
081: return SIZE
082: - 1
083: - (int) Math.floor(Math.log((double) i)
084: / Math.log(2.0d));
085: }
086: }
087:
088: public static int numberOfTrailingZeros(long i) {
089: if (i < 0) {
090: return 0;
091: } else if (i == 0) {
092: return SIZE;
093: } else {
094: int rtn = 0;
095: for (int r = 1; (r & i) != 0; r = r * 2) {
096: rtn++;
097: }
098: return rtn;
099: }
100: }
101:
102: public static long parseLong(String s) throws NumberFormatException {
103: return parseLong(s, 10);
104: }
105:
106: public static long parseLong(String s, int radix)
107: throws NumberFormatException {
108: return __parseAndValidateLong(s, radix, MIN_VALUE, MAX_VALUE);
109: }
110:
111: public static long reverse(long i) {
112: long acc = 0;
113: long front = MIN_VALUE;
114: int back = 1;
115: int swing = SIZE - 1;
116: while (swing > 15) {
117: acc = acc | ((i & front) >> swing) | ((i & back) << swing);
118: swing--;
119: front = front >> 1;
120: back = back << 1;
121: }
122: return acc;
123: }
124:
125: public static long reverseBytes(long i) {
126: return ((i & 0xffL) << 56) | ((i & 0xff00L) << 40)
127: | ((i & 0xff0000L) << 24) | ((i & 0xff000000L) << 8)
128: | ((i & 0xff00000000L) >> 8)
129: | ((i & 0xff0000000000L) >> 24)
130: | ((i & 0xff000000000000L) >> 40)
131: | ((i & 0xff00000000000000L) >> 56);
132: }
133:
134: public static long rotateLeft(long i, int distance) {
135: while (distance-- > 0) {
136: i = i << 1 | ((i < 0) ? 1 : 0);
137: }
138: return i;
139: }
140:
141: public static long rotateRight(long i, int distance) {
142: while (distance-- > 0) {
143: i = ((i & 1) == 0 ? 0 : 0x80000000) | i >> 1;
144: }
145: return i;
146: }
147:
148: public static int signum(long i) {
149: if (i == 0) {
150: return 0;
151: } else if (i < 0) {
152: return -1;
153: } else {
154: return 1;
155: }
156: }
157:
158: public static String toBinaryString(long x) {
159: if (x == 0) {
160: return "0";
161: }
162: String binStr = "";
163: while (x != 0) {
164: int bit = (int) x & 0x1;
165: binStr = __hexDigits[bit] + binStr;
166: x = x >>> 1;
167: }
168: return binStr;
169: }
170:
171: public static String toHexString(long x) {
172: if (x == 0) {
173: return "0";
174: }
175: String hexStr = "";
176: while (x != 0) {
177: int nibble = (int) x & 0xF;
178: hexStr = __hexDigits[nibble] + hexStr;
179: x = x >>> 4;
180: }
181: return hexStr;
182: }
183:
184: public static String toString(long b) {
185: return String.valueOf(b);
186: }
187:
188: public static Long valueOf(long i) {
189: if (i > -129 && i < 128) {
190: int rebase = (int) i + 128;
191: if (boxedValues[rebase] == null) {
192: boxedValues[rebase] = new Long(i);
193: }
194: return boxedValues[rebase];
195: }
196: return new Long(i);
197: }
198:
199: public static Long valueOf(String s) throws NumberFormatException {
200: return new Long(Long.parseLong(s));
201: }
202:
203: public static Long valueOf(String s, int radix)
204: throws NumberFormatException {
205: return new Long(Long.parseLong(s, radix));
206: }
207:
208: private final transient long value;
209:
210: public Long(long value) {
211: this .value = value;
212: }
213:
214: public Long(String s) {
215: value = parseLong(s);
216: }
217:
218: @Override
219: public byte byteValue() {
220: return (byte) value;
221: }
222:
223: public int compareTo(Long b) {
224: if (value < b.value) {
225: return -1;
226: } else if (value > b.value) {
227: return 1;
228: } else {
229: return 0;
230: }
231: }
232:
233: @Override
234: public double doubleValue() {
235: return value;
236: }
237:
238: @Override
239: public boolean equals(Object o) {
240: return (o instanceof Long) && (((Long) o).value == value);
241: }
242:
243: @Override
244: public float floatValue() {
245: return value;
246: }
247:
248: @Override
249: public int hashCode() {
250: return hashCode(value);
251: }
252:
253: @Override
254: public int intValue() {
255: return (int) value;
256: }
257:
258: @Override
259: public long longValue() {
260: return value;
261: }
262:
263: @Override
264: public short shortValue() {
265: return (short) value;
266: }
267:
268: @Override
269: public String toString() {
270: return toString(value);
271: }
272:
273: }
|