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.DeltaLong; 020import org.nuxeo.ecm.core.api.model.Property; 021import org.nuxeo.ecm.core.api.model.PropertyConversionException; 022import org.nuxeo.ecm.core.api.model.impl.ScalarProperty; 023import org.nuxeo.ecm.core.schema.types.Field; 024 025/** 026 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 027 */ 028public class LongProperty extends ScalarProperty { 029 030 private static final long serialVersionUID = 4071336664294418041L; 031 032 public LongProperty(Property parent, Field field, int flags) { 033 super(parent, field, flags); 034 } 035 036 @Override 037 public boolean isNormalized(Object value) { 038 return value == null || value instanceof Long || value instanceof DeltaLong; 039 } 040 041 @Override 042 public Serializable normalize(Object value) throws PropertyConversionException { 043 if (isNormalized(value)) { 044 return (Serializable) value; 045 } 046 if (value.getClass() == String.class) { 047 String string = (String) value; 048 if (string.length() == 0) { 049 return null; 050 } 051 return Long.valueOf(value.toString()); 052 } 053 if (value instanceof Number) { 054 return ((Number) value).longValue(); 055 } 056 throw new PropertyConversionException(value.getClass(), Long.class); 057 } 058 059 @SuppressWarnings("unchecked") 060 @Override 061 public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException { 062 if (value == null || Long.class == toType) { 063 return (T) value; 064 } 065 Long v = (Long) value; 066 if (toType == Integer.class) { 067 return (T) new Integer(v.intValue()); 068 } 069 if (toType == String.class) { 070 return (T) v.toString(); 071 } 072 if (toType == Double.class) { 073 return (T) new Double(v.doubleValue()); 074 } 075 if (toType == Float.class) { 076 return (T) new Float(v.floatValue()); 077 } 078 if (toType == Short.class) { 079 return (T) new Short(v.shortValue()); 080 } 081 if (toType == Byte.class) { 082 return (T) new Byte(v.byteValue()); 083 } 084 throw new PropertyConversionException(value.getClass(), toType); 085 } 086 087 @Override 088 public Object newInstance() { 089 return (long) 0; 090 } 091 092}