001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.core.api.model.impl.primitives; 023 024import java.io.Serializable; 025 026import org.nuxeo.ecm.core.api.model.DeltaLong; 027import org.nuxeo.ecm.core.api.model.Property; 028import org.nuxeo.ecm.core.api.model.PropertyConversionException; 029import org.nuxeo.ecm.core.api.model.impl.ScalarProperty; 030import org.nuxeo.ecm.core.schema.types.Field; 031 032/** 033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 034 */ 035public class LongProperty extends ScalarProperty { 036 037 private static final long serialVersionUID = 4071336664294418041L; 038 039 public LongProperty(Property parent, Field field, int flags) { 040 super(parent, field, flags); 041 } 042 043 @Override 044 public boolean isNormalized(Object value) { 045 return value == null || value instanceof Long || value instanceof DeltaLong; 046 } 047 048 @Override 049 public Serializable normalize(Object value) { 050 if (isNormalized(value)) { 051 return (Serializable) value; 052 } 053 if (value instanceof String) { 054 String string = (String) value; 055 if (string.length() == 0) { 056 return null; 057 } 058 try { 059 return Long.valueOf(string); 060 } catch (NumberFormatException e) { 061 throw new PropertyConversionException(value.getClass(), Long.class, e.getMessage()); 062 } 063 } 064 if (value instanceof Number) { 065 return ((Number) value).longValue(); 066 } 067 throw new PropertyConversionException(value.getClass(), Long.class); 068 } 069 070 @SuppressWarnings("unchecked") 071 @Override 072 public <T> T convertTo(Serializable value, Class<T> toType) { 073 if (value == null || Long.class == toType) { 074 return (T) value; 075 } 076 Long v = (Long) value; 077 if (toType == Integer.class) { 078 return (T) Integer.valueOf(v.intValue()); 079 } 080 if (toType == String.class) { 081 return (T) v.toString(); 082 } 083 if (toType == Double.class) { 084 return (T) Double.valueOf(v.doubleValue()); 085 } 086 if (toType == Float.class) { 087 return (T) Float.valueOf(v.floatValue()); 088 } 089 if (toType == Short.class) { 090 return (T) Short.valueOf(v.shortValue()); 091 } 092 if (toType == Byte.class) { 093 return (T) Byte.valueOf(v.byteValue()); 094 } 095 throw new PropertyConversionException(value.getClass(), toType); 096 } 097 098 @Override 099 public Object newInstance() { 100 return (long) 0; 101 } 102 103}