ae.config.field.AbstractCollectionField.java Source code

Java tutorial

Introduction

Here is the source code for ae.config.field.AbstractCollectionField.java

Source

/*
 * Copyright 2007-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ae.config.field;

import org.apache.commons.lang.StringUtils;

import ae.config.ActiveEntity;
import ae.config.constraint.LengthBetweenMetaConstraint;
import ae.config.constraint.MaxLengthMetaConstraint;
import ae.config.constraint.MinLengthMetaConstraint;

public abstract class AbstractCollectionField<F extends AbstractCollectionField<F>>
        extends AbstractIndexableField<F> implements CollectionField<F> {
    private final String elementClass;

    AbstractCollectionField(final ActiveEntity ae, final String collectionElementClass) {
        super(ae);
        if (collectionElementClass == null) {
            throw new NullPointerException("collectionElementClass");
        }
        this.elementClass = collectionElementClass;
    }

    @Override
    public String sizeReaderMethod() {
        return "size";
    }

    @Override
    public String getElementClass() {
        return this.elementClass;
    }

    @Override
    public String getDatastoreType() {
        return getDomainType();
    }

    @Override
    public boolean isBoolean() {
        return false;
    }

    @Override
    public boolean isEnum() {
        return false;
    }

    @Override
    public boolean isCollection() {
        return true;
    }

    protected static abstract class CollectionBuilder<F extends AbstractCollectionField<F>, B extends CollectionBuilder<F, B>>
            extends IndexableFieldBuilder<F, B> {
        protected final String elementClassName;

        public CollectionBuilder(final ActiveEntity ae, final Class<?> elementClass) {
            super(ae);
            if (elementClass == null) {
                throw new NullPointerException("elementClass");
            }
            this.elementClassName = elementClass.getName();
        }

        public CollectionBuilder(final ActiveEntity ae, final String elementClassName) {
            super(ae);
            if (elementClassName == null) {
                throw new NullPointerException("elementClassName");
            }
            if (StringUtils.isBlank(elementClassName)) {
                throw new IllegalArgumentException("elementClassName is blank");
            }
            this.elementClassName = elementClassName;
        }

        public B minLength(final int value) {
            return addConstraint(new MinLengthMetaConstraint(value));
        }

        public B maxLength(final int value) {
            return addConstraint(new MaxLengthMetaConstraint(value));
        }

        public B lengthBetween(final int min, final int max) {
            return addConstraint(new LengthBetweenMetaConstraint(min, max));
        }
    }
}