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>int</code> as an object.
020: */
021: public final class Integer extends Number implements
022: Comparable<Integer> {
023:
024: public static final int MIN_VALUE = 0x80000000;
025: public static final int MAX_VALUE = 0x7fffffff;
026: public static final int SIZE = 32;
027:
028: // Box values according to JLS - between -128 and 127
029: private static Integer[] boxedValues = new Integer[256];
030:
031: public static int bitCount(int x) {
032: // Courtesy the University of Kentucky
033: // http://aggregate.org/MAGIC/#Population%20Count%20(Ones%20Count)
034: x -= ((x >> 1) & 0x55555555);
035: x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
036: x = (((x >> 4) + x) & 0x0f0f0f0f);
037: x += (x >> 8);
038: x += (x >> 16);
039: return x & 0x0000003f;
040: }
041:
042: public static Integer decode(String s) throws NumberFormatException {
043: return new Integer((int) __decodeAndValidateLong(s, MIN_VALUE,
044: MAX_VALUE));
045: }
046:
047: /**
048: * @skip
049: *
050: * Here for shared implementation with Arrays.hashCode
051: */
052: public static int hashCode(int i) {
053: return i;
054: }
055:
056: public static int highestOneBit(int i) {
057: if (i < 0) {
058: return MIN_VALUE;
059: } else if (i == 0) {
060: return 0;
061: } else {
062: int rtn;
063: for (rtn = 0x40000000; (rtn & i) == 0; rtn = rtn >> 1) {
064: // loop down until smaller
065: }
066: return rtn;
067: }
068: }
069:
070: public static int lowestOneBit(int i) {
071: if (i == 0) {
072: return 0;
073: } else if (i == Integer.MIN_VALUE) {
074: return 0x80000000;
075: } else {
076: int r = 1;
077: while ((r & i) == 0) {
078: r = r * 2;
079: }
080: return r;
081: }
082: }
083:
084: public static int numberOfLeadingZeros(int i) {
085: if (i < 0) {
086: return 0;
087: } else if (i == 0) {
088: return SIZE;
089: } else {
090: return SIZE - 1
091: - (int) Math.floor(Math.log(i) / Math.log(2.0d));
092: }
093: }
094:
095: public static int numberOfTrailingZeros(int i) {
096: if (i == 0) {
097: return 32;
098: } else {
099: int rtn = 0;
100: for (int r = 1; (r & i) == 0; r = r * 2) {
101: rtn++;
102: }
103: return rtn;
104: }
105: }
106:
107: public static int parseInt(String s) throws NumberFormatException {
108: return parseInt(s, 10);
109: }
110:
111: public static int parseInt(String s, int radix)
112: throws NumberFormatException {
113: return (int) __parseAndValidateLong(s, radix, MIN_VALUE,
114: MAX_VALUE);
115: }
116:
117: public static int reverse(int i) {
118: int ui = i & 0x7fffffff; // avoid sign extension
119: int acc = 0;
120: int front = 0x80000000;
121: int back = 1;
122: int swing = 31;
123: while (swing > 0) {
124: acc = acc | ((ui & front) >> swing)
125: | ((ui & back) << swing);
126: swing -= 2;
127: front = front >> 1;
128: back = back << 1;
129: }
130: if (i < 0) {
131: acc = acc | 0x1; // restore the real value of 0x80000000
132: }
133: return acc;
134: }
135:
136: public static int reverseBytes(int i) {
137: return ((i & 0xff) << 24) | ((i & 0xff00) << 8)
138: | ((i & 0xff0000) >> 8) | ((i & 0xff000000) >> 24);
139: }
140:
141: public static int rotateLeft(int i, int distance) {
142: while (distance-- > 0) {
143: i = i << 1 | ((i < 0) ? 1 : 0);
144: }
145: return i;
146: }
147:
148: public static int rotateRight(int i, int distance) {
149: int ui = i & 0x7fffffff; // avoid sign extension
150: int carry = (i < 0) ? 0x40000000 : 0; // 0x80000000 rightshifted 1
151: while (distance-- > 0) {
152: int nextcarry = ui & 1;
153: ui = carry | (ui >> 1);
154: carry = (nextcarry == 0) ? 0 : 0x40000000;
155: }
156: if (carry != 0) {
157: ui = ui | 0x80000000;
158: }
159: return ui;
160: }
161:
162: public static int signum(int i) {
163: if (i == 0) {
164: return 0;
165: } else if (i < 0) {
166: return -1;
167: } else {
168: return 1;
169: }
170: }
171:
172: public static String toBinaryString(int x) {
173: return Long.toBinaryString(x);
174: }
175:
176: public static String toHexString(int x) {
177: return Long.toHexString(x);
178: }
179:
180: public static String toString(int b) {
181: return String.valueOf(b);
182: }
183:
184: public static Integer valueOf(int i) {
185: if (i > -129 && i < 128) {
186: int rebase = i + 128;
187: if (boxedValues[rebase] == null) {
188: boxedValues[rebase] = new Integer(i);
189: }
190: return boxedValues[rebase];
191: }
192: return new Integer(i);
193: }
194:
195: public static Integer valueOf(String s)
196: throws NumberFormatException {
197: return new Integer(Integer.parseInt(s));
198: }
199:
200: public static Integer valueOf(String s, int radix)
201: throws NumberFormatException {
202: return new Integer(Integer.parseInt(s, radix));
203: }
204:
205: private final transient int value;
206:
207: public Integer(int value) {
208: this .value = value;
209: }
210:
211: public Integer(String s) {
212: value = parseInt(s);
213: }
214:
215: @Override
216: public byte byteValue() {
217: return (byte) value;
218: }
219:
220: public int compareTo(Integer b) {
221: if (value < b.value) {
222: return -1;
223: } else if (value > b.value) {
224: return 1;
225: } else {
226: return 0;
227: }
228: }
229:
230: @Override
231: public double doubleValue() {
232: return value;
233: }
234:
235: @Override
236: public boolean equals(Object o) {
237: return (o instanceof Integer) && (((Integer) o).value == value);
238: }
239:
240: @Override
241: public float floatValue() {
242: return value;
243: }
244:
245: @Override
246: public int hashCode() {
247: return hashCode(value);
248: }
249:
250: @Override
251: public int intValue() {
252: return value;
253: }
254:
255: @Override
256: public long longValue() {
257: return value;
258: }
259:
260: @Override
261: public short shortValue() {
262: return (short) value;
263: }
264:
265: @Override
266: public String toString() {
267: return toString(value);
268: }
269:
270: }
|