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 org.nuxeo.ecm.core.schema.types.PrimitiveType;
024import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
025import org.nuxeo.ecm.core.schema.types.constraints.EnumConstraint;
026import org.nuxeo.ecm.core.schema.types.constraints.LengthConstraint;
027import org.nuxeo.ecm.core.schema.types.constraints.NotNullConstraint;
028import org.nuxeo.ecm.core.schema.types.constraints.PatternConstraint;
029
030/**
031 * The string type.
032 */
033public final class StringType extends PrimitiveType {
034
035    private static final long serialVersionUID = 1L;
036
037    public static final String ID = "string";
038
039    public static final StringType INSTANCE = new StringType();
040
041    private StringType() {
042        super(ID);
043    }
044
045    @Override
046    public boolean validate(Object object) {
047        return true;
048    }
049
050    @Override
051    public Object convert(Object value) {
052        return value.toString();
053    }
054
055    @Override
056    public Object decode(String str) {
057        return str;
058    }
059
060    @Override
061    public String encode(Object object) {
062        return object != null ? object.toString() : "";
063    }
064
065    @Override
066    public Object newInstance() {
067        return "";
068    }
069
070    protected Object readResolve() {
071        return INSTANCE;
072    }
073
074    @Override
075    public boolean support(Class<? extends Constraint> constraint) {
076        if (NotNullConstraint.class.equals(constraint)) {
077            return true;
078        }
079        if (EnumConstraint.class.equals(constraint)) {
080            return true;
081        }
082        if (PatternConstraint.class.equals(constraint)) {
083            return true;
084        }
085        if (LengthConstraint.class.equals(constraint)) {
086            return true;
087        }
088        return false;
089    }
090
091}