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