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.Collection;
023
024import org.nuxeo.ecm.core.schema.Namespace;
025import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
026
027/**
028 * A complex type is tree-like structure of named elements which can be of any type.
029 * <p>
030 * Complex types can describe and validate java <code>Map objects</code>.
031 */
032public interface ComplexType extends Type {
033
034    /**
035     * Gets the namespace used by this complex type.
036     *
037     * @return the namespace or {@link Namespace#DEFAULT_NS} if none was specified
038     */
039    Namespace getNamespace();
040
041    /**
042     * Gets the field with the given name.
043     * <p>
044     * If the name is non-prefixed the first matching field is returned if any is found. If the name is prefixed then
045     * the right field is returned if any is found.
046     *
047     * @param name the field name
048     * @return the field
049     */
050    Field getField(String name);
051
052    /**
053     * Gets the field having the given name.
054     *
055     * @param name the name
056     * @return the field or null if no field with that name was found
057     */
058    Field getField(QName name);
059
060    /**
061     * Adds a field to this complex type.
062     * <p>
063     * If the given name is not prefixed it will be prefixed with the type prefix. If one was specified otherwise the
064     * default prefix will be used (e.g. "" - no prefix). If the given name is prefixed it will be stored as is (using
065     * the specified prefix).
066     *
067     * @param name the field name
068     * @param type the field type
069     * @param defaultValue an optional default value (null if none)
070     * @param flags optional flags
071     * @return the created field
072     */
073    Field addField(String name, Type type, String defaultValue, int flags, Collection<Constraint> constraints);
074
075    /**
076     * Tests whether this type defines the given field name.
077     * <p>
078     * The name is supposed to be non prefixed.
079     *
080     * @param name the field name
081     * @return true if the field exists, false otherwise
082     */
083    boolean hasField(String name);
084
085    /**
086     * Tests whether this type has any field defined.
087     * <p>
088     * If a complex type has no fields, it is considered as unstructured and it accepts any field with any type and
089     * name.
090     *
091     * @return true if the at least one field exists, false otherwise
092     */
093    boolean hasFields();
094
095    /**
096     * Gets all fields as a (field name, field type) map.
097     *
098     * @return the fields map
099     */
100    Collection<Field> getFields();
101
102    /**
103     * Gets the number of fields defined for this complex type.
104     *
105     * @return the fields count
106     */
107    int getFieldsCount();
108
109}