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 java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.ecm.core.schema.types.PrimitiveType;
028import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
029import org.nuxeo.ecm.core.schema.types.constraints.NotNullConstraint;
030
031/**
032 * The Boolean type.
033 */
034public final class BooleanType extends PrimitiveType {
035
036    private static final long serialVersionUID = 1L;
037
038    public static final String ID = "boolean";
039
040    public static final BooleanType INSTANCE = new BooleanType();
041
042    private BooleanType() {
043        super(ID);
044    }
045
046    @Override
047    public boolean validate(Object object) {
048        return object instanceof Boolean;
049    }
050
051    @Override
052    public Object convert(Object value) {
053        if (value instanceof Boolean) {
054            return value;
055        } else if (value instanceof Number) {
056            return ((Number) value).intValue() != 0 ? Boolean.TRUE : Boolean.FALSE;
057        } else {
058            return Boolean.valueOf((String) value);
059        }
060    }
061
062    @Override
063    public Object decode(String str) {
064        if (StringUtils.isEmpty(str)) {
065            return null;
066        }
067        return Boolean.valueOf(str);
068    }
069
070    @Override
071    public String encode(Object value) {
072
073        if (value instanceof Boolean) {
074            return value.toString();
075        } else if (value instanceof Number) {
076            return ((Number) value).intValue() != 0 ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
077        } else {
078            return value != null ? (String) value : "";
079        }
080    }
081
082    @Override
083    public Object newInstance() {
084        return Boolean.FALSE;
085    }
086
087    protected Object readResolve() {
088        return INSTANCE;
089    }
090
091    public List<Class<? extends Constraint>> getRelevantConstraints() {
092        List<Class<? extends Constraint>> classes = new ArrayList<Class<? extends Constraint>>();
093        classes.add(NotNullConstraint.class);
094        return classes;
095    }
096
097    @Override
098    public boolean support(Class<? extends Constraint> constraint) {
099        if (NotNullConstraint.class.equals(constraint)) {
100            return true;
101        }
102        return false;
103    }
104
105}