001    /*
002     * Created on Feb 7, 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 org.fest.assertions.core.Condition;
018    
019    /**
020     * Returns {@code true} if all of the joined conditions is satisfied.
021     * @param <T> the type of object this condition accepts.
022     *
023     * @author Yvonne Wang
024     * @author Mikhail Mazursky
025     */
026    public class AllOf<T> extends Join<T> {
027    
028      /**
029       * Creates a new <code>{@link AllOf}</code>
030       * @param <T> the type of object the given condition accept.
031       * @param conditions the conditions to evaluate.
032       * @return the created {@code AnyOf}.
033       * @throws NullPointerException if the given array is {@code null}.
034       * @throws NullPointerException if any of the elements in the given array is {@code null}.
035       */
036      public static <T> Condition<T> allOf(Condition<? super T>...conditions) {
037        return new AllOf<T>(conditions);
038      }
039    
040      /**
041       * Creates a new <code>{@link AllOf}</code>
042       * @param <T> the type of object the given condition accept.
043       * @param conditions the conditions to evaluate.
044       * @return the created {@code AnyOf}.
045       * @throws NullPointerException if the given iterable is {@code null}.
046       * @throws NullPointerException if any of the elements in the given iterable is {@code null}.
047       */
048      public static <T> Condition<T> allOf(Iterable<? extends Condition<? super T>> conditions) {
049        return new AllOf<T>(conditions);
050      }
051    
052      private AllOf(Condition<? super T>...conditions) {
053        super(conditions);
054      }
055    
056      private AllOf(Iterable<? extends Condition<? super T>> conditions) {
057        super(conditions);
058      }
059    
060      /** {@inheritDoc} */
061      @Override public boolean matches(T value) {
062        for (Condition<? super T> condition : conditions)
063          if (!condition.matches(value)) return false;
064        return true;
065      }
066    
067      @Override public String toString() {
068        return String.format("all of:<%s>", conditions);
069      }
070    }