001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.graph.properties.basic;
023
024 import java.io.InputStream;
025 import java.io.Reader;
026 import java.math.BigDecimal;
027 import java.net.URI;
028 import java.util.Calendar;
029 import java.util.Date;
030 import java.util.UUID;
031 import net.jcip.annotations.Immutable;
032 import org.jboss.dna.common.text.TextDecoder;
033 import org.jboss.dna.graph.GraphI18n;
034 import org.jboss.dna.graph.properties.Binary;
035 import org.jboss.dna.graph.properties.DateTime;
036 import org.jboss.dna.graph.properties.IoException;
037 import org.jboss.dna.graph.properties.Name;
038 import org.jboss.dna.graph.properties.Path;
039 import org.jboss.dna.graph.properties.PropertyType;
040 import org.jboss.dna.graph.properties.Reference;
041 import org.jboss.dna.graph.properties.ValueFactory;
042 import org.jboss.dna.graph.properties.ValueFormatException;
043
044 /**
045 * The standard {@link ValueFactory} for {@link PropertyType#BOOLEAN} values.
046 *
047 * @author Randall Hauch
048 * @author John Verhaeg
049 */
050 @Immutable
051 public class BooleanValueFactory extends AbstractValueFactory<Boolean> {
052
053 public BooleanValueFactory( TextDecoder decoder,
054 ValueFactory<String> stringValueFactory ) {
055 super(PropertyType.BOOLEAN, decoder, stringValueFactory);
056 }
057
058 /**
059 * {@inheritDoc}
060 */
061 public Boolean create( String value ) {
062 if (value == null) return null;
063 return Boolean.valueOf(value.trim());
064 }
065
066 /**
067 * {@inheritDoc}
068 */
069 public Boolean create( String value,
070 TextDecoder decoder ) {
071 // this probably doesn't really need to call the decoder, but by doing so then we don't care at all what the decoder does
072 return create(getDecoder(decoder).decode(value));
073 }
074
075 /**
076 * {@inheritDoc}
077 */
078 public Boolean create( int value ) {
079 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
080 Integer.class.getSimpleName(),
081 value));
082 }
083
084 /**
085 * {@inheritDoc}
086 */
087 public Boolean create( long value ) {
088 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
089 Long.class.getSimpleName(),
090 value));
091 }
092
093 /**
094 * {@inheritDoc}
095 */
096 public Boolean create( boolean value ) {
097 return value;
098 }
099
100 /**
101 * {@inheritDoc}
102 */
103 public Boolean create( float value ) {
104 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
105 Float.class.getSimpleName(),
106 value));
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public Boolean create( double value ) {
113 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
114 Double.class.getSimpleName(),
115 value));
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 public Boolean create( BigDecimal value ) {
122 throw new ValueFormatException(value, getPropertyType(),
123 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
124 BigDecimal.class.getSimpleName(),
125 value));
126 }
127
128 /**
129 * {@inheritDoc}
130 */
131 public Boolean create( Calendar value ) {
132 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
133 Calendar.class.getSimpleName(),
134 value));
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 public Boolean create( Date value ) {
141 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
142 Date.class.getSimpleName(),
143 value));
144 }
145
146 /**
147 * {@inheritDoc}
148 *
149 * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.DateTime)
150 */
151 public Boolean create( DateTime value ) throws ValueFormatException {
152 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
153 DateTime.class.getSimpleName(),
154 value));
155 }
156
157 /**
158 * {@inheritDoc}
159 */
160 public Boolean create( Name value ) {
161 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
162 Name.class.getSimpleName(),
163 value));
164 }
165
166 /**
167 * {@inheritDoc}
168 */
169 public Boolean create( Path value ) {
170 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
171 Path.class.getSimpleName(),
172 value));
173 }
174
175 /**
176 * {@inheritDoc}
177 */
178 public Boolean create( Reference value ) {
179 throw new ValueFormatException(value, getPropertyType(),
180 GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
181 Reference.class.getSimpleName(),
182 value));
183 }
184
185 /**
186 * {@inheritDoc}
187 */
188 public Boolean create( URI value ) {
189 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
190 URI.class.getSimpleName(),
191 value));
192 }
193
194 /**
195 * {@inheritDoc}
196 *
197 * @see org.jboss.dna.graph.properties.ValueFactory#create(java.util.UUID)
198 */
199 public Boolean create( UUID value ) {
200 throw new ValueFormatException(value, getPropertyType(), GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
201 UUID.class.getSimpleName(),
202 value));
203 }
204
205 /**
206 * {@inheritDoc}
207 */
208 public Boolean create( byte[] value ) {
209 // First create a string and then create the boolean from the string value ...
210 return create(getStringValueFactory().create(value));
211 }
212
213 /**
214 * {@inheritDoc}
215 *
216 * @see org.jboss.dna.graph.properties.ValueFactory#create(org.jboss.dna.graph.properties.Binary)
217 */
218 public Boolean create( Binary value ) throws ValueFormatException, IoException {
219 // First create a string and then create the boolean from the string value ...
220 return create(getStringValueFactory().create(value));
221 }
222
223 /**
224 * {@inheritDoc}
225 */
226 public Boolean create( InputStream stream,
227 long approximateLength ) throws IoException {
228 // First create a string and then create the boolean from the string value ...
229 return create(getStringValueFactory().create(stream, approximateLength));
230 }
231
232 /**
233 * {@inheritDoc}
234 */
235 public Boolean create( Reader reader,
236 long approximateLength ) throws IoException {
237 // First create a string and then create the boolean from the string value ...
238 return create(getStringValueFactory().create(reader, approximateLength));
239 }
240
241 /**
242 * {@inheritDoc}
243 */
244 @Override
245 protected Boolean[] createEmptyArray( int length ) {
246 return new Boolean[length];
247 }
248
249 }