001/*
002 * (C) Copyright 2006-2012 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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
020 */
021package org.nuxeo.ecm.core.schema.types.primitives;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.ecm.core.schema.types.PrimitiveType;
025import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
026import org.nuxeo.ecm.core.schema.types.constraints.EnumConstraint;
027import org.nuxeo.ecm.core.schema.types.constraints.NotNullConstraint;
028import org.nuxeo.ecm.core.schema.types.constraints.NumericIntervalConstraint;
029import org.nuxeo.ecm.core.schema.types.constraints.PatternConstraint;
030
031/**
032 * The double type.
033 */
034public final class DoubleType extends PrimitiveType {
035
036    private static final long serialVersionUID = 1L;
037
038    public static final String ID = "double";
039
040    public static final DoubleType INSTANCE = new DoubleType();
041
042    private DoubleType() {
043        super(ID);
044    }
045
046    @Override
047    public boolean validate(Object object) {
048        return object instanceof Number;
049    }
050
051    @Override
052    public Object convert(Object value) {
053        if (value instanceof Double) {
054            return value;
055        } else if (value instanceof Number) {
056            return Double.valueOf(((Number) value).longValue());
057        } else {
058            try {
059                return Double.valueOf((String) value);
060            } catch (NumberFormatException e) {
061                return null;
062            }
063        }
064    }
065
066    @Override
067    public Object decode(String str) {
068        if (StringUtils.isEmpty(str)) {
069            return null;
070        }
071        try {
072            return Double.valueOf(str);
073        } catch (NumberFormatException e) {
074            return Double.valueOf(0.0);
075        }
076    }
077
078    @Override
079    public String encode(Object object) {
080        if (object instanceof Double) {
081            return object.toString();
082        } else if (object instanceof Number) {
083            return object.toString();
084        } else {
085            return object != null ? (String) object : "";
086        }
087    }
088
089    protected Object readResolve() {
090        return INSTANCE;
091    }
092
093    @Override
094    public boolean support(Class<? extends Constraint> constraint) {
095        if (NotNullConstraint.class.equals(constraint)) {
096            return true;
097        }
098        if (EnumConstraint.class.equals(constraint)) {
099            return true;
100        }
101        if (PatternConstraint.class.equals(constraint)) {
102            return true;
103        }
104        if (NumericIntervalConstraint.class.equals(constraint)) {
105            return true;
106        }
107        return false;
108    }
109
110}