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