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) throws PropertyConversionException {
050        if (isNormalized(value)) {
051            return (Serializable) value;
052        }
053        if (value.getClass() == String.class) {
054            String string = (String) value;
055            if (string.length() == 0) {
056                return null;
057            }
058            return Long.valueOf(value.toString());
059        }
060        if (value instanceof Number) {
061            return ((Number) value).longValue();
062        }
063        throw new PropertyConversionException(value.getClass(), Long.class);
064    }
065
066    @SuppressWarnings("unchecked")
067    @Override
068    public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException {
069        if (value == null || Long.class == toType) {
070            return (T) value;
071        }
072        Long v = (Long) value;
073        if (toType == Integer.class) {
074            return (T) new Integer(v.intValue());
075        }
076        if (toType == String.class) {
077            return (T) v.toString();
078        }
079        if (toType == Double.class) {
080            return (T) new Double(v.doubleValue());
081        }
082        if (toType == Float.class) {
083            return (T) new Float(v.floatValue());
084        }
085        if (toType == Short.class) {
086            return (T) new Short(v.shortValue());
087        }
088        if (toType == Byte.class) {
089            return (T) new Byte(v.byteValue());
090        }
091        throw new PropertyConversionException(value.getClass(), toType);
092    }
093
094    @Override
095    public Object newInstance() {
096        return (long) 0;
097    }
098
099}