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