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