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;
022
023import java.util.HashSet;
024
025import org.nuxeo.ecm.core.schema.types.resolver.ObjectResolver;
026
027/**
028 * Implementation of a simple type that is not primitive (and therefore has constraints).
029 */
030public class SimpleTypeImpl extends AbstractType implements SimpleType {
031
032    private static final long serialVersionUID = 1L;
033
034    private ObjectResolver resolver;
035
036    private PrimitiveType primitiveType;
037
038    public SimpleTypeImpl(SimpleType superType, String schema, String name) {
039        super(superType, schema, name);
040        // simple types must have a not null super type
041        // for example a primitive type or another simple type
042        assert superType != null;
043        constraints = new HashSet<>();
044    }
045
046    @Override
047    public boolean validate(Object object) throws TypeException {
048        if (object == null) {
049            return true;
050        }
051        if (validateConstraints(object)) {
052            return getSuperType().validate(object);
053        }
054        return false;
055    }
056
057    @Override
058    public PrimitiveType getPrimitiveType() {
059        if (primitiveType == null) {
060            primitiveType = ((SimpleType) getSuperType()).getPrimitiveType();
061        }
062        return primitiveType;
063    }
064
065    @Override
066    public boolean isPrimitive() {
067        return false;
068    }
069
070    @Override
071    public boolean isSimpleType() {
072        return true;
073    }
074
075    public void setResolver(ObjectResolver resolver) {
076        this.resolver = resolver;
077    }
078
079    @Override
080    public ObjectResolver getObjectResolver() {
081        return resolver;
082    }
083
084    @Override
085    public Object decode(String str) {
086        return getPrimitiveType().decode(str);
087    }
088
089    @Override
090    public String encode(Object value) {
091        return getPrimitiveType().encode(value);
092    }
093
094    @Override
095    public Object convert(Object value) throws TypeException {
096        return getPrimitiveType().convert(value);
097    }
098
099    @Override
100    public Object newInstance() {
101        // XXX AT: not sure that's what is supposed to be done
102        return getPrimitiveType().newInstance();
103    }
104
105}