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     * @author Mikhail Mazursky
030     */
031    public abstract class Join<T> extends Condition<T> {
032    
033      @VisibleForTesting final Collection<Condition<? super T>> conditions;
034    
035      /**
036       * Creates a new </code>{@link Join}</code>.
037       * @param conditions the conditions to join.
038       * @throws NullPointerException if the given array is {@code null}.
039       * @throws NullPointerException if any of the elements in the given array is {@code null}.
040       */
041      protected Join(Condition<? super T>...conditions) {
042        if (conditions == null) throw conditionsIsNull();
043        this.conditions = new ArrayList<Condition<? super T>>();
044        for (Condition<? super T> condition : conditions) this.conditions.add(notNull(condition));
045      }
046    
047      /**
048       * Creates a new </code>{@link Join}</code>.
049       * @param conditions the conditions to join.
050       * @throws NullPointerException if the given iterable is {@code null}.
051       * @throws NullPointerException if any of the elements in the given iterable is {@code null}.
052       */
053      protected Join(Iterable<? extends Condition<? super T>> conditions) {
054        if (conditions == null) throw conditionsIsNull();
055        this.conditions = new ArrayList<Condition<? super T>>();
056        for (Condition<? super T> condition : conditions) this.conditions.add(notNull(condition));
057      }
058    
059      private static NullPointerException conditionsIsNull() {
060        return new NullPointerException("The given conditions should not be null");
061      }
062    
063      private static <T> Condition<T> notNull(Condition<T> condition) {
064        if (condition == null) throw new NullPointerException("The given conditions should not have null entries");
065        return condition;
066      }
067    
068      /**
069       * Returns the conditions to join.
070       * @return the conditions to join.
071       */
072      protected final Collection<Condition<? super T>> conditions() {
073        return unmodifiableCollection(conditions);
074      }
075    }