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 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api.model.impl;
016
017import java.io.Serializable;
018import java.util.Collection;
019import java.util.Iterator;
020
021import org.nuxeo.ecm.core.api.PropertyException;
022import org.nuxeo.ecm.core.api.model.Property;
023import org.nuxeo.ecm.core.api.model.PropertyVisitor;
024import org.nuxeo.ecm.core.schema.types.Field;
025import org.nuxeo.ecm.core.schema.types.Type;
026
027/**
028 * A scalar property that is linked to a schema field
029 *
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class ScalarProperty extends AbstractProperty {
033
034    private static final long serialVersionUID = 3078523648297014704L;
035
036    /**
037     * The corresponding field.
038     */
039    protected final Field field;
040
041    protected Serializable value;
042
043    public ScalarProperty(Property parent, Field field) {
044        super(parent);
045        this.field = field;
046    }
047
048    public ScalarProperty(Property parent, Field field, int flags) {
049        super(parent, flags);
050        this.field = field;
051    }
052
053    @Override
054    public void internalSetValue(Serializable value) throws PropertyException {
055        this.value = value;
056    }
057
058    @Override
059    public String getName() {
060        return field.getName().getPrefixedName();
061    }
062
063    @Override
064    public Type getType() {
065        return field.getType();
066    }
067
068    @Override
069    public Serializable internalGetValue() throws PropertyException {
070        return value;
071    }
072
073    @Override
074    public boolean isContainer() {
075        return false;
076    }
077
078    @Override
079    public Collection<Property> getChildren() {
080        throw new UnsupportedOperationException("Scalar properties don't have children");
081    }
082
083    @Override
084    public Property get(int index) {
085        throw new UnsupportedOperationException("Scalar properties don't have children");
086    }
087
088    @Override
089    public Property get(String name) {
090        throw new UnsupportedOperationException("Scalar properties don't have children");
091    }
092
093    @Override
094    public Property addValue(Object value) {
095        throw new UnsupportedOperationException("Scalar properties don't have children");
096    }
097
098    @Override
099    public Property addValue(int index, Object value) {
100        throw new UnsupportedOperationException("Scalar properties don't have children");
101    }
102
103    @Override
104    public Property addEmpty() {
105        throw new UnsupportedOperationException("add() operation not supported on map properties");
106    }
107
108    @Override
109    public Field getField() {
110        return field;
111    }
112
113    @Override
114    public final Object clone() throws CloneNotSupportedException {
115        ScalarProperty clone = (ScalarProperty) super.clone();
116        return clone;
117    }
118
119    @Override
120    public void accept(PropertyVisitor visitor, Object arg) throws PropertyException {
121        visitor.visit(this, arg);
122    }
123
124    @Override
125    public boolean isSameAs(Property property) throws PropertyException {
126        if (property == null) {
127            return false;
128        }
129        ScalarProperty sp = (ScalarProperty) property;
130        Object v1 = getValue();
131        Object v2 = sp.getValue();
132        if (v1 == null) {
133            return v2 == null;
134        }
135        return v1.equals(v2);
136    }
137
138    @Override
139    public Iterator<Property> getDirtyChildren() {
140        throw new UnsupportedOperationException("Cannot iterate over children of scalar properties");
141    }
142
143    @Override
144    public String toString() {
145        return getClass().getSimpleName() + '(' + getPath().substring(1) + (isDirty() ? "*" : "") + "="
146                + String.valueOf(value) + ')';
147    }
148
149}