1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 | package agents.org.apache.commons.lang.mutable;
|
---|
19 |
|
---|
20 | import java.io.Serializable;
|
---|
21 |
|
---|
22 | import agents.org.apache.commons.lang.BooleanUtils;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A mutable <code>boolean</code> wrapper.
|
---|
26 | *
|
---|
27 | * @see Boolean
|
---|
28 | * @since 2.2
|
---|
29 | * @author Apache Software Foundation
|
---|
30 | * @version $Id: MutableBoolean.java 905707 2010-02-02 16:59:59Z niallp $
|
---|
31 | */
|
---|
32 | public class MutableBoolean implements Mutable, Serializable, Comparable {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Required for serialization support.
|
---|
36 | *
|
---|
37 | * @see java.io.Serializable
|
---|
38 | */
|
---|
39 | private static final long serialVersionUID = -4830728138360036487L;
|
---|
40 |
|
---|
41 | /** The mutable value. */
|
---|
42 | private boolean value;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructs a new MutableBoolean with the default value of false.
|
---|
46 | */
|
---|
47 | public MutableBoolean() {
|
---|
48 | super();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Constructs a new MutableBoolean with the specified value.
|
---|
53 | *
|
---|
54 | * @param value the initial value to store
|
---|
55 | */
|
---|
56 | public MutableBoolean(boolean value) {
|
---|
57 | super();
|
---|
58 | this.value = value;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructs a new MutableBoolean with the specified value.
|
---|
63 | *
|
---|
64 | * @param value the initial value to store, not null
|
---|
65 | * @throws NullPointerException if the object is null
|
---|
66 | */
|
---|
67 | public MutableBoolean(Boolean value) {
|
---|
68 | super();
|
---|
69 | this.value = value.booleanValue();
|
---|
70 | }
|
---|
71 |
|
---|
72 | //-----------------------------------------------------------------------
|
---|
73 | /**
|
---|
74 | * Gets the value as a Boolean instance.
|
---|
75 | *
|
---|
76 | * @return the value as a Boolean, never null
|
---|
77 | */
|
---|
78 | public Object getValue() {
|
---|
79 | return BooleanUtils.toBooleanObject(this.value);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Sets the value.
|
---|
84 | *
|
---|
85 | * @param value the value to set
|
---|
86 | */
|
---|
87 | public void setValue(boolean value) {
|
---|
88 | this.value = value;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Sets the value from any Boolean instance.
|
---|
93 | *
|
---|
94 | * @param value the value to set, not null
|
---|
95 | * @throws NullPointerException if the object is null
|
---|
96 | */
|
---|
97 | public void setValue(Object value) {
|
---|
98 | setValue(((Boolean) value).booleanValue());
|
---|
99 | }
|
---|
100 |
|
---|
101 | //-----------------------------------------------------------------------
|
---|
102 | /**
|
---|
103 | * Checks if the current value is <code>true</code>.
|
---|
104 | *
|
---|
105 | * @return <code>true</code> if the current value is <code>true</code>
|
---|
106 | * @since 2.5
|
---|
107 | */
|
---|
108 | public boolean isTrue() {
|
---|
109 | return value == true;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Checks if the current value is <code>false</code>.
|
---|
114 | *
|
---|
115 | * @return <code>true</code> if the current value is <code>false</code>
|
---|
116 | * @since 2.5
|
---|
117 | */
|
---|
118 | public boolean isFalse() {
|
---|
119 | return value == false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | //-----------------------------------------------------------------------
|
---|
123 | /**
|
---|
124 | * Returns the value of this MutableBoolean as a boolean.
|
---|
125 | *
|
---|
126 | * @return the boolean value represented by this object.
|
---|
127 | */
|
---|
128 | public boolean booleanValue() {
|
---|
129 | return value;
|
---|
130 | }
|
---|
131 |
|
---|
132 | //-----------------------------------------------------------------------
|
---|
133 | /**
|
---|
134 | * Gets this mutable as an instance of Boolean.
|
---|
135 | *
|
---|
136 | * @return a Boolean instance containing the value from this mutable, never null
|
---|
137 | * @since 2.5
|
---|
138 | */
|
---|
139 | public Boolean toBoolean() {
|
---|
140 | return BooleanUtils.toBooleanObject(this.value);
|
---|
141 | }
|
---|
142 |
|
---|
143 | //-----------------------------------------------------------------------
|
---|
144 | /**
|
---|
145 | * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
|
---|
146 | * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
|
---|
147 | * <code>boolean</code> value as this object.
|
---|
148 | *
|
---|
149 | * @param obj the object to compare with, null returns false
|
---|
150 | * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
|
---|
151 | */
|
---|
152 | public boolean equals(Object obj) {
|
---|
153 | if (obj instanceof MutableBoolean) {
|
---|
154 | return value == ((MutableBoolean) obj).booleanValue();
|
---|
155 | }
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Returns a suitable hash code for this mutable.
|
---|
161 | *
|
---|
162 | * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
|
---|
163 | */
|
---|
164 | public int hashCode() {
|
---|
165 | return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
|
---|
166 | }
|
---|
167 |
|
---|
168 | //-----------------------------------------------------------------------
|
---|
169 | /**
|
---|
170 | * Compares this mutable to another in ascending order.
|
---|
171 | *
|
---|
172 | * @param obj the other mutable to compare to, not null
|
---|
173 | * @return negative if this is less, zero if equal, positive if greater
|
---|
174 | * where false is less than true
|
---|
175 | */
|
---|
176 | public int compareTo(Object obj) {
|
---|
177 | MutableBoolean other = (MutableBoolean) obj;
|
---|
178 | boolean anotherVal = other.value;
|
---|
179 | return value == anotherVal ? 0 : (value ? 1 : -1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | //-----------------------------------------------------------------------
|
---|
183 | /**
|
---|
184 | * Returns the String value of this mutable.
|
---|
185 | *
|
---|
186 | * @return the mutable value as a string
|
---|
187 | */
|
---|
188 | public String toString() {
|
---|
189 | return String.valueOf(value);
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|