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