Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / lang / Boolean.java
bloba30b669df1ab6c6e65028675cff9dcfaa423088e
1 /* Boolean.java -- object wrapper for boolean
2 Copyright (C) 1998, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.lang;
41 import java.io.Serializable;
43 /**
44 * Instances of class <code>Boolean</code> represent primitive
45 * <code>boolean</code> values.
47 * @author Paul Fisher
48 * @author Eric Blake (ebb9@email.byu.edu)
49 * @since 1.0
50 * @status updated to 1.4
52 public final class Boolean implements Serializable
54 /**
55 * Compatible with JDK 1.0.2+.
57 private static final long serialVersionUID = -3665804199014368530L;
59 /**
60 * This field is a <code>Boolean</code> object representing the
61 * primitive value <code>true</code>. This instance is returned
62 * by the static <code>valueOf()</code> methods if they return
63 * a <code>Boolean</code> representing <code>true</code>.
65 public static final Boolean TRUE = new Boolean(true);
67 /**
68 * This field is a <code>Boolean</code> object representing the
69 * primitive value <code>false</code>. This instance is returned
70 * by the static <code>valueOf()</code> methods if they return
71 * a <code>Boolean</code> representing <code>false</code>.
73 public static final Boolean FALSE = new Boolean(false);
75 /**
76 * The primitive type <code>boolean</code> is represented by this
77 * <code>Class</code> object.
79 * @since 1.1
81 public static final Class TYPE = VMClassLoader.getPrimitiveClass('Z');
83 /**
84 * The immutable value of this Boolean.
85 * @serial the wrapped value
87 private final boolean value;
89 /**
90 * Create a <code>Boolean</code> object representing the value of the
91 * argument <code>value</code>. In general the use of the static
92 * method <code>valueof(boolean)</code> is more efficient since it will
93 * not create a new object.
95 * @param value the primitive value of this <code>Boolean</code>
96 * @see #valueOf(boolean)
98 public Boolean(boolean value)
100 this.value = value;
104 * Creates a <code>Boolean</code> object representing the primitive
105 * <code>true</code> if and only if <code>s</code> matches
106 * the string "true" ignoring case, otherwise the object will represent
107 * the primitive <code>false</code>. In general the use of the static
108 * method <code>valueof(String)</code> is more efficient since it will
109 * not create a new object.
111 * @param s the <code>String</code> representation of <code>true</code>
112 * or false
114 public Boolean(String s)
116 value = "true".equalsIgnoreCase(s);
120 * Return the primitive <code>boolean</code> value of this
121 * <code>Boolean</code> object.
123 * @return true or false, depending on the value of this Boolean
125 public boolean booleanValue()
127 return value;
131 * Returns the Boolean <code>TRUE</code> if the given boolean is
132 * <code>true</code>, otherwise it will return the Boolean
133 * <code>FALSE</code>.
135 * @param b the boolean to wrap
136 * @return the wrapper object
137 * @see #TRUE
138 * @see #FALSE
139 * @since 1.4
141 public static Boolean valueOf(boolean b)
143 return b ? TRUE : FALSE;
147 * Returns the Boolean <code>TRUE</code> if and only if the given
148 * String is equal, ignoring case, to the the String "true", otherwise
149 * it will return the Boolean <code>FALSE</code>.
151 * @param s the string to convert
152 * @return a wrapped boolean from the string
154 public static Boolean valueOf(String s)
156 return "true".equalsIgnoreCase(s) ? TRUE : FALSE;
160 * Returns "true" if the value of the give boolean is <code>true</code> and
161 * returns "false" if the value of the given boolean is <code>false</code>.
163 * @param b the boolean to convert
164 * @return the string representation of the boolean
165 * @since 1.4
167 public static String toString(boolean b)
169 return b ? "true" : "false";
173 * Returns "true" if the value of this object is <code>true</code> and
174 * returns "false" if the value of this object is <code>false</code>.
176 * @return the string representation of this
178 public String toString()
180 return value ? "true" : "false";
184 * Returns the integer <code>1231</code> if this object represents
185 * the primitive <code>true</code> and the integer <code>1237</code>
186 * otherwise.
188 * @return the hash code
190 public int hashCode()
192 return value ? 1231 : 1237;
196 * If the <code>obj</code> is an instance of <code>Boolean</code> and
197 * has the same primitive value as this object then <code>true</code>
198 * is returned. In all other cases, including if the <code>obj</code>
199 * is <code>null</code>, <code>false</code> is returned.
201 * @param obj possibly an instance of any <code>Class</code>
202 * @return true if <code>obj</code> equals this
204 public boolean equals(Object obj)
206 return obj instanceof Boolean && value == ((Boolean) obj).value;
210 * If the value of the system property <code>name</code> matches
211 * "true" ignoring case then the function returns <code>true</code>.
213 * @param name the property name to look up
214 * @return true if the property resulted in "true"
215 * @throws SecurityException if accessing the system property is forbidden
216 * @see System#getProperty(String)
218 public static boolean getBoolean(String name)
220 if (name == null || "".equals(name))
221 return false;
222 return "true".equalsIgnoreCase(System.getProperty(name));