~brian-thomason/+junk/hamcrest

« back to all changes in this revision

Viewing changes to hamcrest-core/src/main/java/org/hamcrest/core/AnyOf.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:40:16 UTC
  • Revision ID: brian.thomason@canonical.com-20111220174016-g67bgjybqqhzuuxt
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hamcrest.core;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Arrays;
 
5
import java.util.List;
 
6
 
 
7
import org.hamcrest.Description;
 
8
import org.hamcrest.Factory;
 
9
import org.hamcrest.Matcher;
 
10
 
 
11
/**
 
12
 * Calculates the logical disjunction of multiple matchers. Evaluation is shortcut, so
 
13
 * subsequent matchers are not called if an earlier matcher returns <code>true</code>.
 
14
 */
 
15
public class AnyOf<T> extends ShortcutCombination<T> {
 
16
 
 
17
    public AnyOf(Iterable<Matcher<? super T>> matchers) {
 
18
        super(matchers);
 
19
    }
 
20
 
 
21
    @Override
 
22
    public boolean matches(Object o) {
 
23
        return matches(o, true);
 
24
    }
 
25
 
 
26
    @Override
 
27
    public void describeTo(Description description) {
 
28
        describeTo(description, "or");
 
29
    }
 
30
 
 
31
    /**
 
32
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
33
     */
 
34
    @Factory
 
35
    public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
 
36
        return new AnyOf<T>(matchers);
 
37
    }
 
38
    
 
39
    /**
 
40
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
41
     */
 
42
    @Factory
 
43
    public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
 
44
        return anyOf(Arrays.asList(matchers));
 
45
    }
 
46
 
 
47
    /**
 
48
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
49
     */
 
50
    @Factory
 
51
    public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second) {
 
52
        List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
 
53
        matchers.add(first);
 
54
        matchers.add(second);
 
55
        return anyOf(matchers);
 
56
    }
 
57
 
 
58
    /**
 
59
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
60
     */
 
61
    @Factory
 
62
    public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
 
63
        List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
 
64
        matchers.add(first);
 
65
        matchers.add(second);
 
66
        matchers.add(third);
 
67
        return anyOf(matchers);
 
68
    }
 
69
 
 
70
    /**
 
71
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
72
     */
 
73
    @Factory
 
74
    public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
 
75
        List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
 
76
        matchers.add(first);
 
77
        matchers.add(second);
 
78
        matchers.add(third);
 
79
        matchers.add(fourth);
 
80
        return anyOf(matchers);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
85
     */
 
86
    @Factory
 
87
    public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
 
88
        List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
 
89
        matchers.add(first);
 
90
        matchers.add(second);
 
91
        matchers.add(third);
 
92
        matchers.add(fourth);
 
93
        matchers.add(fifth);
 
94
        return anyOf(matchers);
 
95
    }
 
96
 
 
97
    /**
 
98
     * Evaluates to true if ANY of the passed in matchers evaluate to true.
 
99
     */
 
100
    @Factory
 
101
    public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
 
102
        List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
 
103
        matchers.add(first);
 
104
        matchers.add(second);
 
105
        matchers.add(third);
 
106
        matchers.add(fourth);
 
107
        matchers.add(fifth);
 
108
        matchers.add(sixth);
 
109
        return anyOf(matchers);
 
110
    }
 
111
}