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.UuidFactory;
044    import org.jboss.dna.graph.property.ValueFactory;
045    import org.jboss.dna.graph.property.ValueFormatException;
046    
047    /**
048     * The standard {@link ValueFactory} for {@link PropertyType#URI} values.
049     * 
050     * @author Randall Hauch
051     * @author John Verhaeg
052     */
053    @Immutable
054    public class UuidValueFactory extends AbstractValueFactory<UUID> implements UuidFactory {
055    
056        public UuidValueFactory( TextDecoder decoder,
057                                 ValueFactory<String> stringValueFactory ) {
058            super(PropertyType.UUID, decoder, stringValueFactory);
059        }
060    
061        /**
062         * {@inheritDoc}
063         * 
064         * @see org.jboss.dna.graph.property.UuidFactory#create()
065         */
066        public UUID create() {
067            return UUID.randomUUID();
068        }
069    
070        /**
071         * {@inheritDoc}
072         */
073        public UUID create( String value ) {
074            if (value == null) return null;
075            value = value.trim();
076            try {
077                return UUID.fromString(value);
078            } catch (IllegalArgumentException err) {
079                throw new ValueFormatException(value, PropertyType.UUID,
080                                               GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
081                                                                                URI.class.getSimpleName(),
082                                                                                value), err);
083            }
084        }
085    
086        /**
087         * {@inheritDoc}
088         */
089        public UUID create( String value,
090                            TextDecoder decoder ) {
091            // 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
092            return create(getDecoder(decoder).decode(value));
093        }
094    
095        /**
096         * {@inheritDoc}
097         */
098        public UUID create( int value ) {
099            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
100                                                                                                      Integer.class.getSimpleName(),
101                                                                                                      value));
102        }
103    
104        /**
105         * {@inheritDoc}
106         */
107        public UUID create( long value ) {
108            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
109                                                                                                      Long.class.getSimpleName(),
110                                                                                                      value));
111        }
112    
113        /**
114         * {@inheritDoc}
115         */
116        public UUID create( boolean value ) {
117            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
118                                                                                                      Boolean.class.getSimpleName(),
119                                                                                                      value));
120        }
121    
122        /**
123         * {@inheritDoc}
124         */
125        public UUID create( float value ) {
126            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
127                                                                                                      Float.class.getSimpleName(),
128                                                                                                      value));
129        }
130    
131        /**
132         * {@inheritDoc}
133         */
134        public UUID create( double value ) {
135            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
136                                                                                                      Double.class.getSimpleName(),
137                                                                                                      value));
138        }
139    
140        /**
141         * {@inheritDoc}
142         */
143        public UUID create( BigDecimal value ) {
144            throw new ValueFormatException(value, PropertyType.UUID,
145                                           GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
146                                                                            BigDecimal.class.getSimpleName(),
147                                                                            value));
148        }
149    
150        /**
151         * {@inheritDoc}
152         */
153        public UUID create( Calendar value ) {
154            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
155                                                                                                      Calendar.class.getSimpleName(),
156                                                                                                      value));
157        }
158    
159        /**
160         * {@inheritDoc}
161         */
162        public UUID create( Date value ) {
163            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
164                                                                                                      Date.class.getSimpleName(),
165                                                                                                      value));
166        }
167    
168        /**
169         * {@inheritDoc}
170         * 
171         * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.DateTime)
172         */
173        public UUID create( DateTime value ) throws ValueFormatException {
174            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
175                                                                                                      DateTime.class.getSimpleName(),
176                                                                                                      value));
177        }
178    
179        /**
180         * {@inheritDoc}
181         */
182        public UUID create( Name value ) {
183            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
184                                                                                                      Name.class.getSimpleName(),
185                                                                                                      value));
186        }
187    
188        /**
189         * {@inheritDoc}
190         */
191        public UUID create( Path value ) {
192            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
193                                                                                                      Path.class.getSimpleName(),
194                                                                                                      value));
195        }
196    
197        /**
198         * {@inheritDoc}
199         */
200        public UUID create( Reference value ) {
201            if (value instanceof UuidReference) {
202                UuidReference ref = (UuidReference)value;
203                return ref.getUuid();
204            }
205            throw new ValueFormatException(value, PropertyType.UUID,
206                                           GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
207                                                                            Reference.class.getSimpleName(),
208                                                                            value));
209        }
210    
211        /**
212         * {@inheritDoc}
213         */
214        public UUID create( URI value ) {
215            throw new ValueFormatException(value, PropertyType.UUID, GraphI18n.unableToCreateValue.text(getPropertyType().getName(),
216                                                                                                      URI.class.getSimpleName(),
217                                                                                                      value));
218        }
219    
220        /**
221         * {@inheritDoc}
222         * 
223         * @see org.jboss.dna.graph.property.ValueFactory#create(java.util.UUID)
224         */
225        public UUID create( UUID value ) {
226            return value;
227        }
228    
229        /**
230         * {@inheritDoc}
231         */
232        public UUID create( byte[] value ) {
233            // First attempt to create a string from the value, then a long from the string ...
234            return create(getStringValueFactory().create(value));
235        }
236    
237        /**
238         * {@inheritDoc}
239         * 
240         * @see org.jboss.dna.graph.property.ValueFactory#create(org.jboss.dna.graph.property.Binary)
241         */
242        public UUID create( Binary value ) throws ValueFormatException, IoException {
243            // First create a string and then create the boolean from the string value ...
244            return create(getStringValueFactory().create(value));
245        }
246    
247        /**
248         * {@inheritDoc}
249         */
250        public UUID create( InputStream stream,
251                            long approximateLength ) throws IoException {
252            // First attempt to create a string from the value, then a double from the string ...
253            return create(getStringValueFactory().create(stream, approximateLength));
254        }
255    
256        /**
257         * {@inheritDoc}
258         */
259        public UUID create( Reader reader,
260                            long approximateLength ) throws IoException {
261            // First attempt to create a string from the value, then a double from the string ...
262            return create(getStringValueFactory().create(reader, approximateLength));
263        }
264    
265        /**
266         * {@inheritDoc}
267         */
268        @Override
269        protected UUID[] createEmptyArray( int length ) {
270            return new UUID[length];
271        }
272    
273    }