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