001/*
002 * (C) Copyright 2006-2018 Nuxeo (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.lang3.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 long type.
033 */
034public final class LongType extends PrimitiveType {
035
036    private static final long serialVersionUID = 1L;
037
038    public static final String ID = "long";
039
040    public static final LongType INSTANCE = new LongType();
041
042    private LongType() {
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 Long) {
054            return value;
055        } else if (value instanceof Number) {
056            return Long.valueOf(((Number) value).longValue());
057        } else {
058            try {
059                return Long.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 Long.valueOf(str);
073        } catch (NumberFormatException e) {
074            return Long.valueOf(0);
075        }
076    }
077
078    @Override
079    public String encode(Object object) {
080
081        if (object instanceof Long) {
082            return object.toString();
083        } else if (object instanceof Number) {
084            return object.toString();
085        } else {
086            return object != null ? (String) object : "";
087        }
088    }
089
090    protected Object readResolve() {
091        return INSTANCE;
092    }
093
094    @Override
095    public boolean support(Class<? extends Constraint> constraint) {
096        if (NotNullConstraint.class.equals(constraint)) {
097            return true;
098        }
099        if (EnumConstraint.class.equals(constraint)) {
100            return true;
101        }
102        if (PatternConstraint.class.equals(constraint)) {
103            return true;
104        }
105        if (NumericIntervalConstraint.class.equals(constraint)) {
106            return true;
107        }
108        return false;
109    }
110
111}