LoginSignup
5
2

More than 5 years have passed since last update.

[Java]複数OR条件判定

Last updated at Posted at 2017-12-27

そんなの

よく使う方法
Arrays.asList(expects...).contains(target)

でいいじゃん、というツッコミはさておき、

MultipleConditionMatcher.java
// 要lombok.jar

import java.util.Arrays;

import lombok.NonNull;

/**
 * 複数OR条件判定
 * @param <T> 判定対象の型
 */
public class MultipleConditionMatcher<T> {

    /** 判定対象 */
    @NonNull
    private T target;

    /**
     * コンストラクタ
     * @param target 判定対象
     */
    public MultipleConditionMatcher(@NonNull T target) {
        this.target = target;
    }

    /**
     * 複数OR条件判定
     * @param expects 判定条件
     * @return 判定条件のいずれかと一致した場合 {code true}
     */
    @SuppressWarnings("unchecked")
    public boolean in(@NonNull final T... expects) {
        return Arrays.stream(expects).anyMatch(target::equals);
    }

    /**
     * 複数OR条件判定
     * @param expects 判定条件
     * @return 判定条件のいずれかと一致した場合 {code true}
     */
    @SuppressWarnings("unchecked")
    public boolean or(@NonNull final T... expects) {
        return in(expects);
    }

}
サンプルコード
// 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));
実行結果
true
false

(2018/11/01 追記)

Predicate#Or() で繋ぐ版を作ってみました。

MultipleConditionMatcher.java
// 要lombok.jar

import java.util.function.Predicate;

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

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

    /**
     * 複数OR条件判定
     * @param <T> 判定対象の型
     * @param target 判定対象
     * @param expects 判定条件
     * @return 判定条件のいずれかと一致した場合 {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);
    }

    /**
     * 複数OR条件判定
     * @param <T> 判定対象の型
     * @param target 判定対象
     * @param expects 判定条件
     * @return 判定条件のいずれかと一致した場合 {code true}
     */
    @SuppressWarnings("unchecked")
    public static <T> boolean or(final T target, final T... expects) {
        return in(target, expects);
    }

}
サンプルコード
// String
System.out.println(MultipleConditionMatcher.in("Hoge", "Hoge", "Fuga"));
// Integer(boxed int)
System.out.println(MultipleConditionMatcher.or(1, 0, Integer.MAX_VALUE));
実行結果
true
false
5
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2