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 */
020package org.nuxeo.ecm.core.schema.types;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.HashSet;
026import java.util.Set;
027
028import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
029import org.nuxeo.ecm.core.schema.types.constraints.ConstraintUtils;
030import org.nuxeo.ecm.core.schema.types.constraints.NotNullConstraint;
031
032/**
033 * The implementation for a field, which is the association of a type, a name, and default values.
034 */
035public class FieldImpl implements Field {
036
037    private static final long serialVersionUID = 1L;
038
039    private QName name;
040
041    private Type type;
042
043    private Type declaringType;
044
045    private int flags;
046
047    private int maxOccurs = 1;
048
049    private int minOccurs = 1;
050
051    private String defaultValue;
052
053    private int maxLength = -1;
054
055    private Set<Constraint> constraints;
056
057    public FieldImpl(QName name, Type declaringType, Type type, String defaultValue, int flags,
058            Collection<Constraint> constraints) {
059        this.name = name;
060        this.type = type;
061        this.declaringType = declaringType;
062        this.defaultValue = defaultValue;
063        this.flags = flags;
064        this.constraints = new HashSet<>();
065        if (constraints != null) {
066            this.constraints.addAll(constraints);
067        }
068    }
069
070    public FieldImpl(QName name, Type declaringType, Type type) {
071        this(name, declaringType, type, null, 0, new ArrayList<Constraint>());
072    }
073
074    public FieldImpl(QName name, Type declaringType, Type type, String defaultValue, int flags) {
075        this(name, declaringType, type, defaultValue, flags, new ArrayList<Constraint>());
076    }
077
078    @Override
079    public Type getDeclaringType() {
080        return declaringType;
081    }
082
083    @Override
084    public QName getName() {
085        return name;
086    }
087
088    @Override
089    public Type getType() {
090        return type;
091    }
092
093    @Override
094    public Object getDefaultValue() {
095        return type.decode(defaultValue);
096    }
097
098    @Override
099    public boolean isNillable() {
100        return ConstraintUtils.getConstraint(constraints, NotNullConstraint.class) == null;
101    }
102
103    @Override
104    public boolean isConstant() {
105        return (flags & CONSTANT) != 0;
106    }
107
108    @Override
109    public void setDefaultValue(String value) {
110        defaultValue = value;
111    }
112
113    @Override
114    public void setNillable(boolean isNillable) {
115        if (isNillable) {
116            flags |= NILLABLE;
117        } else {
118            flags &= ~NILLABLE;
119        }
120    }
121
122    @Override
123    public void setConstant(boolean isConstant) {
124        if (isConstant) {
125            flags |= CONSTANT;
126        } else {
127            flags &= ~CONSTANT;
128        }
129    }
130
131    @Override
132    public int getMaxOccurs() {
133        return maxOccurs;
134    }
135
136    @Override
137    public int getMinOccurs() {
138        return minOccurs;
139    }
140
141    @Override
142    public void setMaxOccurs(int max) {
143        maxOccurs = max;
144    }
145
146    @Override
147    public void setMinOccurs(int min) {
148        minOccurs = min;
149    }
150
151    @Override
152    public int getMaxLength() {
153        return maxLength;
154    }
155
156    @Override
157    public void setMaxLength(int length) {
158        maxLength = length;
159    }
160
161    @Override
162    public String toString() {
163        return name + " [" + type.getName() + ']';
164    }
165
166    @Override
167    public Set<Constraint> getConstraints() {
168        return Collections.unmodifiableSet(constraints);
169    }
170
171}