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