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: import java.io.Serializable;
019:
020: /**
021: * Wraps a native <code>char</code> as an object.
022: */
023: public final class Character implements Comparable<Character>,
024: Serializable {
025:
026: public static final int MIN_RADIX = 2;
027: public static final int MAX_RADIX = 36;
028:
029: public static final char MIN_VALUE = '\u0000';
030: public static final char MAX_VALUE = '\uFFFF';
031:
032: // Box values according to JLS - from \u0000 to \u007f
033: private static Character[] boxedValues = new Character[128];
034:
035: public static int digit(char c, int radix) {
036: if (radix < MIN_RADIX || radix > MAX_RADIX) {
037: return -1;
038: }
039:
040: if (c >= '0' && c < '0' + Math.min(radix, 10)) {
041: return c - '0';
042: }
043:
044: // The offset by 10 is to re-base the alpha values
045: if (c >= 'a' && c < (radix + 'a' - 10)) {
046: return c - 'a' + 10;
047: }
048:
049: if (c >= 'A' && c < (radix + 'A' - 10)) {
050: return c - 'A' + 10;
051: }
052:
053: return -1;
054: }
055:
056: public static char forDigit(int digit, int radix) {
057: if (radix < MIN_RADIX || radix > MAX_RADIX) {
058: return 0;
059: }
060:
061: if (digit < 0 || digit >= radix) {
062: return 0;
063: }
064:
065: final int baseTenMax = 10;
066: if (digit < baseTenMax) {
067: return (char) ('0' + digit);
068: } else {
069: return (char) ('a' + digit - baseTenMax);
070: }
071: }
072:
073: /**
074: * @skip
075: *
076: * Here for shared implementation with Arrays.hashCode
077: */
078: public static int hashCode(char c) {
079: return c;
080: }
081:
082: public static native boolean isDigit(char c) /*-{
083: return (null != String.fromCharCode(c).match(/\d/));
084: }-*/;
085:
086: public static native boolean isLetter(char c) /*-{
087: return (null != String.fromCharCode(c).match(/[A-Z]/i));
088: }-*/;
089:
090: public static native boolean isLetterOrDigit(char c) /*-{
091: return (null != String.fromCharCode(c).match(/[A-Z\d]/i));
092: }-*/;
093:
094: public static boolean isLowerCase(char c) {
095: return toLowerCase(c) == c && isLetter(c);
096: }
097:
098: public static boolean isSpace(char c) {
099: switch (c) {
100: case ' ':
101: return true;
102: case '\n':
103: return true;
104: case '\t':
105: return true;
106: case '\f':
107: return true;
108: case '\r':
109: return true;
110: default:
111: return false;
112: }
113: }
114:
115: public static boolean isUpperCase(char c) {
116: return toUpperCase(c) == c && isLetter(c);
117: }
118:
119: public static native char toLowerCase(char c) /*-{
120: return String.fromCharCode(c).toLowerCase().charCodeAt(0);
121: }-*/;
122:
123: public static String toString(char x) {
124: return String.valueOf(x);
125: }
126:
127: public static native char toUpperCase(char c) /*-{
128: return String.fromCharCode(c).toUpperCase().charCodeAt(0);
129: }-*/;
130:
131: public static Character valueOf(char c) {
132: if (c < 128) {
133: if (boxedValues[c] == null) {
134: boxedValues[c] = new Character(c);
135: }
136: return boxedValues[c];
137: }
138: return new Character(c);
139: }
140:
141: private final transient char value;
142:
143: public Character(char value) {
144: this .value = value;
145: }
146:
147: public char charValue() {
148: return value;
149: }
150:
151: public int compareTo(Character c) {
152: if (value < c.value) {
153: return -1;
154: } else if (value > c.value) {
155: return 1;
156: } else {
157: return 0;
158: }
159: }
160:
161: @Override
162: public boolean equals(Object o) {
163: return (o instanceof Character)
164: && (((Character) o).value == value);
165: }
166:
167: @Override
168: public int hashCode() {
169: return hashCode(value);
170: }
171:
172: @Override
173: public String toString() {
174: return String.valueOf(value);
175: }
176: }
|