001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 * 012 * $Id$ 013 */ 014 015package org.nuxeo.ecm.core.api.model.impl.primitives; 016 017import java.io.Serializable; 018 019import org.nuxeo.ecm.core.api.model.Property; 020import org.nuxeo.ecm.core.api.model.PropertyConversionException; 021import org.nuxeo.ecm.core.api.model.impl.ScalarProperty; 022import org.nuxeo.ecm.core.schema.types.Field; 023 024/** 025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 026 */ 027public class DoubleProperty extends ScalarProperty { 028 029 private static final long serialVersionUID = 3764033209483557763L; 030 031 public DoubleProperty(Property parent, Field field, int flags) { 032 super(parent, field, flags); 033 } 034 035 @Override 036 public boolean isNormalized(Object value) { 037 return value == null || value.getClass() == Double.class; 038 } 039 040 @Override 041 public Serializable normalize(Object value) throws PropertyConversionException { 042 if (isNormalized(value)) { 043 return (Serializable) value; 044 } 045 if (value.getClass() == String.class) { 046 String string = (String) value; 047 if (string.length() == 0) { 048 return null; 049 } 050 return Double.valueOf(value.toString()); 051 } 052 if (value instanceof Number) { 053 return ((Number) value).doubleValue(); 054 } 055 throw new PropertyConversionException(value.getClass(), Double.class); 056 } 057 058 @SuppressWarnings("unchecked") 059 @Override 060 public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException { 061 if (value == null || Double.class == toType) { 062 return (T) value; 063 } 064 Double v = (Double) value; 065 if (toType == Integer.class) { 066 return (T) new Integer(v.intValue()); 067 } 068 if (toType == String.class) { 069 return (T) v.toString(); 070 } 071 if (toType == Long.class) { 072 return (T) new Long(v.longValue()); 073 } 074 if (toType == Float.class) { 075 return (T) new Float(v.floatValue()); 076 } 077 if (toType == Short.class) { 078 return (T) new Short(v.shortValue()); 079 } 080 if (toType == Byte.class) { 081 return (T) new Byte(v.byteValue()); 082 } 083 throw new PropertyConversionException(value.getClass(), toType); 084 } 085 086 @Override 087 public Object newInstance() { 088 return (double) 0; 089 } 090 091}