01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package java.lang;
17:
18: import java.io.Serializable;
19:
20: /**
21: * Wraps native <code>boolean</code> as an object.
22: */
23: public final class Boolean implements Comparable<Boolean>, Serializable {
24:
25: // CHECKSTYLE_OFF: These have to be created somewhere.
26: public static Boolean FALSE = new Boolean(false);
27: public static Boolean TRUE = new Boolean(true);
28:
29: // CHECKSTYLE_ON
30:
31: public static Boolean parseString(String s) {
32: if (s != null && s.equalsIgnoreCase("true")) {
33: return TRUE;
34: }
35: return FALSE;
36: }
37:
38: public static String toString(boolean x) {
39: return String.valueOf(x);
40: }
41:
42: public static Boolean valueOf(boolean b) {
43: return b ? TRUE : FALSE;
44: }
45:
46: public static Boolean valueOf(String s) {
47: if (s != null && s.equalsIgnoreCase("true")) {
48: return TRUE;
49: } else {
50: return FALSE;
51: }
52: }
53:
54: private final transient boolean value;
55:
56: public Boolean(boolean value) {
57: this .value = value;
58: }
59:
60: public Boolean(String s) {
61: this ((s != null) && s.equalsIgnoreCase("true"));
62: }
63:
64: public boolean booleanValue() {
65: return value;
66: }
67:
68: public int compareTo(Boolean other) {
69: if (!value) {
70: return other.value ? -1 : 0;
71: } else {
72: return other.value ? 0 : 1;
73: }
74: }
75:
76: @Override
77: public boolean equals(Object o) {
78: return (o instanceof Boolean) && (((Boolean) o).value == value);
79: }
80:
81: @Override
82: public int hashCode() {
83: // The Java API doc defines these magic numbers.
84: final int hashCodeForTrue = 1231;
85: final int hashCodeForFalse = 1237;
86: return value ? hashCodeForTrue : hashCodeForFalse;
87: }
88:
89: @Override
90: public String toString() {
91: return value ? "true" : "false";
92: }
93: }
|