内容
複数項目の比較に便利なSQLのin句。
例えば曜日が火木土のどれかと一致すれば良いことを比較する場合、
select * from m_calendar where week in ('火','木','土')
などで一致するデータを取得出来ます。
これをJavaで再現してみます。
サンプルコード
package gr.java_conf.tb.tbpg_util.lang; /** * 言語拡張ユーティリティ。 * */ public class LanguageUtil { /** * 複数項目とのor比較を行う。 * * <pre> * StringとString配列でor比較を行う。 * 指定順に比較を行うため、前の要素で一致するほど高速。 * SQLのinと堂宇等の機能 * </pre> * * @param target * 対象文字列 * @param expects * 比較文字列配列(可変長) * @return 一致有無 * @throws IllegalArgumentException * 引数不正。targetもしくはexpectsがnullの場合 */ public static <T> boolean in(String target, String... expects) { if (target == null || expects == null) { throw new IllegalArgumentException(); } for (String expect : expects) { if (expect == null) { throw new IllegalArgumentException(); } if (target.equals(expect)) { return true; } } return false; } }
テストコード
package gr.java_conf.tb.tbpg_util.lang; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.util.Arrays; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; /** * 言語拡張ユーティリティのテスト。 * */ @RunWith(Enclosed.class) public class LanguageUtilTest { /** * Inメソッドの正常系テスト。 * */ @RunWith(Theories.class) public static class InNormal { /** * テストパターン。 * * <pre> * ■入力 * ・複数データ、かつ一致無し * ・複数データ、かつ一致有り * ・単一データ、かつ一致無し * ・単一データ、かつ一致有り * ■期待値 * ・false * ・true * ・false * ・true * </pre> * * @return */ @DataPoints public static Fixture[] getFixture() { Fixture[] fixture = { new Fixture("ほげ", false, "はげ,ひげ,ふご".split(",")), new Fixture("ほげ", true, "はげ,ほげ,ふご".split(",")), new Fixture("ほげ", false, "はげ".split(",")), new Fixture("ほげ", true, "ほげ".split(",")), }; return fixture; }; @Theory public void testIn(Fixture fixture) { boolean actual = LanguageUtil.in(fixture.input, fixture.expects); assertThat(fixture.toString(), actual, is(fixture.result)); } static class Fixture { String input; boolean result; String[] expects; Fixture(String input, boolean result, String... expects) { this.input = input; this.result = result; this.expects = expects; } public String toString() { return Arrays.deepToString(expects); } } } /** * Inメソッドの異常系テスト。 * */ @RunWith(Theories.class) public static class InException { /** * テストパターン。 * * <pre> * ■入力 * ・入力データのみnull * ・比較データのみnull * ■期待値 * ・IllegalArgumentException * ・IllegalArgumentException * </pre> * * @return */ @DataPoints public static Fixture[] getFixture() { Fixture[] fixture = { new Fixture(null, "はげ,ひげ,ふご".split(",")), new Fixture("ほげ", (String[]) null), }; return fixture; }; @Theory @Test(expected = IllegalArgumentException.class) public void testIn(Fixture fixture) { LanguageUtil.in(fixture.input, fixture.expects); System.out.println("hjoge"); } static class Fixture { String input; String[] expects; Fixture(String input, String... expects) { this.input = input; this.expects = expects; } public String toString() { return Arrays.deepToString(expects); } } } }