001/*
002 * Copyright (c) 2006-2011 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 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.model.impl.primitives;
016
017import java.io.Serializable;
018
019import org.nuxeo.ecm.core.api.model.Property;
020import org.nuxeo.ecm.core.api.model.PropertyConversionException;
021import org.nuxeo.ecm.core.api.model.impl.ScalarProperty;
022import org.nuxeo.ecm.core.schema.types.Field;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class StringProperty extends ScalarProperty {
028
029    private static final long serialVersionUID = -3586917845281930368L;
030
031    public StringProperty(Property parent, Field field, int flags) {
032        super(parent, field, flags);
033    }
034
035    @Override
036    public boolean isNormalized(Object value) {
037        return value == null || value.getClass() == String.class;
038    }
039
040    @Override
041    public Serializable normalize(Object value) throws PropertyConversionException {
042        if (isNormalized(value)) {
043            return (Serializable) value;
044        }
045        throw new PropertyConversionException(value.getClass(), String.class);
046    }
047
048    @SuppressWarnings("unchecked")
049    @Override
050    public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException {
051        if (toType == String.class) {
052            return (T) value;
053        }
054        throw new PropertyConversionException(value.getClass(), toType);
055    }
056
057    @Override
058    public Object newInstance() {
059        return "";
060    }
061
062}