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