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