[Java] Multiple OR condition judgment

such a

Frequently used method


Arrays.asList(expects...).contains(target)

Aside from the tsukkomi that it's okay

MultipleConditionMatcher.java


//Lombok required.jar

import java.util.Arrays;

import lombok.NonNull;

/**
 *Multiple OR condition judgment
 * @param <T>Judgment target type
 */
public class MultipleConditionMatcher<T> {

    /**Judgment target*/
    @NonNull
    private T target;

    /**
     *constructor
     * @param target Judgment target
     */
    public MultipleConditionMatcher(@NonNull T target) {
        this.target = target;
    }

    /**
     *Multiple OR condition judgment
     * @param expects Judgment conditions
     * @return If any of the judgment conditions are met{code true}
     */
    @SuppressWarnings("unchecked")
    public boolean in(@NonNull final T... expects) {
        return Arrays.stream(expects).anyMatch(target::equals);
    }

    /**
     *Multiple OR condition judgment
     * @param expects Judgment conditions
     * @return If any of the judgment conditions are met{code true}
     */
    @SuppressWarnings("unchecked")
    public boolean or(@NonNull final T... expects) {
        return in(expects);
    }

}

Sample code


// String
System.out.println(new MultipleConditionMatcher<String>("Hoge").in("Hoge", "Fuga"));
// Integer
System.out.println(new MultipleConditionMatcher<Integer>(Integer.valueOf(1)).or(Integer.valueOf(0), Integer.valueOf(Integer.MAX_VALUE));

Execution result


true
false

** (Added on 2018/11/01) **

I made a version that connects with Predicate # Or ().

MultipleConditionMatcher.java


//Lombok required.jar

import java.util.function.Predicate;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MultipleConditionMatcher {

    /**
     *Multiple OR condition judgment
     * @param <T>Judgment target type
     * @param target Judgment target
     * @param expects Judgment conditions
     * @return If any of the judgment conditions are met{code true}
     */
    @SuppressWarnings("unchecked")
    public static <T> boolean in(final T target, final T... expects) {
        if (expects.length == 0) {
            return false;
        }
        Predicate<T> chainedExpect = null;
        for (final T expect : expects) {
            if (chainedExpect == null) {
                chainedExpect = expect::equals;
            } else {
                chainedExpect.or(expect::equals);
            }
        }
        return chainedExpect.test(target);
    }

    /**
     *Multiple OR condition judgment
     * @param <T>Judgment target type
     * @param target Judgment target
     * @param expects Judgment conditions
     * @return If any of the judgment conditions are met{code true}
     */
    @SuppressWarnings("unchecked")
    public static <T> boolean or(final T target, final T... expects) {
        return in(target, expects);
    }

}

Sample code


// String
System.out.println(MultipleConditionMatcher.in("Hoge", "Hoge", "Fuga"));
// Integer(boxed int)
System.out.println(MultipleConditionMatcher.or(1, 0, Integer.MAX_VALUE));

Execution result


true
false

Recommended Posts

[Java] Multiple OR condition judgment
Java string multiple replacement
[Java] Holiday judgment sample
[Java] Combine multiple Lists (Collections)
Operation to connect multiple Streams @Java
Java Unit Test Library-Artery-Current Date Judgment
[Java] Judgment of identity and equivalence
Catch multiple exceptions together in java