001    /*
002     * Created on Feb 6, 2011
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2011 the original author or authors.
014     */
015    package org.fest.assertions.condition;
016    
017    import static java.util.Collections.unmodifiableCollection;
018    
019    import java.util.*;
020    
021    import org.fest.assertions.core.Condition;
022    import org.fest.util.VisibleForTesting;
023    
024    /**
025     * Join of two or more <code>{@link Condition}</code>s.
026     * @param <T> the type of object this condition accepts.
027     *
028     * @author Yvonne Wang
029     */
030    public abstract class Join<T> extends Condition<T> {
031    
032      @VisibleForTesting final Collection<Condition<T>> conditions;
033    
034      /**
035       * Creates a new </code>{@link Join}</code>.
036       * @param conditions the conditions to join.
037       * @throws NullPointerException if the given array is {@code null}.
038       * @throws NullPointerException if any of the elements in the given array is {@code null}.
039       */
040      protected Join(Condition<T>...conditions) {
041        this.conditions = listWithoutNulls(conditions);
042      }
043    
044      private static <T> List<Condition<T>> listWithoutNulls(Condition<T>...conditions) {
045        if (conditions == null) throw conditionsIsNull();
046        List<Condition<T>> list = new ArrayList<Condition<T>>();
047        for (Condition<T> condition : conditions) list.add(notNull(condition));
048        return list;
049      }
050    
051      /**
052       * Creates a new </code>{@link Join}</code>.
053       * @param conditions the conditions to join.
054       * @throws NullPointerException if the given collection is {@code null}.
055       * @throws NullPointerException if any of the elements in the given collection is {@code null}.
056       */
057      protected Join(Collection<Condition<T>> conditions) {
058        this.conditions = listWithoutNulls(conditions);
059      }
060    
061      private static <T> List<Condition<T>> listWithoutNulls(Collection<Condition<T>> conditions) {
062        if (conditions == null) throw conditionsIsNull();
063        List<Condition<T>> list = new ArrayList<Condition<T>>();
064        for (Condition<T> condition : conditions) list.add(notNull(condition));
065        return list;
066      }
067    
068      private static NullPointerException conditionsIsNull() {
069        return new NullPointerException("The given conditions should not be null");
070      }
071    
072      private static <T> Condition<T> notNull(Condition<T> condition) {
073        if (condition == null) throw new NullPointerException("The given conditions should not have null entries");
074        return condition;
075      }
076    
077      /**
078       * Returns the conditions to join.
079       * @return the conditions to join.
080       */
081      protected final Collection<Condition<T>> conditions() {
082        return unmodifiableCollection(conditions);
083      }
084    }