KEIS BLOGは株式会社ケイズ・ソフトウェアが運営しています。

KEIS BLOG

Java Tips -=-= Library =-=- Apache Commons Lang Part 2


前回に引き続き【Apache Commons Lang】をご紹介いたします。
今回は、

BitField

class

BooleanUtils

class

CharSetUtils

class
のサンプルをご紹介します。

Apache Commons Langとは
Apache Commons Lang(アパッチ・コモンズ・ラング)は、ApacheのトッププロジェクトであるApache Commonsにある、Javaのjava.langパッケージを拡張するライブラリである。

– License:
Apache 2.0

– Home Page:
http://commons.apache.org/proper/commons-lang/

– Maven repository:
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

– Maven dependency

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.6</version>
</dependency>

– Java doc:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/package-summary.html

BitField

このクラスはBitFieldに対する操作を提供します。このクラスのインスタンスは、int、short、またはbyteの中にフラグまたはデータを格納するために使用できます。
各BitFieldはマスク値で構成されます。
そのフィールドのデータを格納および検索するために使用されるビットを示します。たとえば、マスク0xFFは、最下位バイトをデータの格納に使用する必要があることを示します。

– setValue()
ビットを新しい値に置き換えます。

– getValue()
適切に右にシフトされたBitFieldの値を取得します

	/**
	 * Test for 
	 * 		{@link BitField#setValue(int, int)}
	 * 		{@link BitField#getValue(int)}
	 */
	@Test
	public void testBitField() {
		BitField blue = new BitField(0xFF);
		BitField green = new BitField(0xFF00);
		BitField red = new BitField(0xFF0000);

		// setValue()
		int color = 0;
		color = blue.setValue(color, 1);
		color = green.setValue(color, 2);
		color = red.setValue(color, 3);
		
		assertThat(color, is(0x030201));

		// getValue()
		assertThat(red.getValue(color), is(3));
		assertThat(green.getValue(color), is(2));
		assertThat(blue.getValue(color), is(1));
	}

BooleanUtils

BooleanプリミティブとBooleanオブジェクトに対する操作を行います。
このクラスはnull入力を正常に処理します。 null入力に対しては例外はスローされません。各メソッドは、その動作をより詳細に記録します。
#ThreadSafe#

– and()
Boolean配列全体のandを返します。

	/**
	 * Test for {@link BooleanUtils#and(boolean...)}
	 */
	@Test
	public void testAnd() {
		assertThat(BooleanUtils.and(false, false), is(true));
		assertThat(BooleanUtils.and(true, true), is(true));
		assertThat(BooleanUtils.and(false, false), is(false));
		assertThat(BooleanUtils.and(true, false), is(false));
		assertThat(BooleanUtils.and(true, true, false), is(false));
		assertThat(BooleanUtils.and(true, true, true), is(true));
	}

– or()
Boolean配列全体のorを返します。

	/**
	 * Test for {@link BooleanUtils#or(boolean...)}
	 */
	@Test
	public void testOr() {
		assertThat(BooleanUtils.or(true, true), is(true));
		assertThat(BooleanUtils.or(false, false), is(false));
		assertThat(BooleanUtils.or(true, false), is(true));
		assertThat(BooleanUtils.or(true, true, false), is(true));
		assertThat(BooleanUtils.or(true, true, true), is(true));
		assertThat(BooleanUtils.or(false, false, false), is(false));
	}

– xor()
Boolean配列全体のxorを返します。

	/**
	 * Test for {@link BooleanUtils#xor(boolean...)}
	 */
	@Test
	public void testXor() {
		assertThat(BooleanUtils.xor(true, true), false);
		assertThat(BooleanUtils.xor(false, false), false);
		assertThat(BooleanUtils.xor(true, false), true);
	}

– negate()
指定されたBoolean値の反対の値を返します。

	/**
	 * Test for {@link BooleanUtils#negate(Boolean)}
	 */
	@Test
	public void testNegate() {
		assertThat(BooleanUtils.negate(true), is(false));
		assertThat(BooleanUtils.negate(false), is(true));
		assertThat(BooleanUtils.negate(null), nullValue());
	}

– isXxx()
Boolean値を判定して結果を返します。

	/**
	 * Test for
	 *		{@link BooleanUtils#isTrue(Boolean)}
	 * 		{@link BooleanUtils#isNotTrue(Boolean)}
	 * 		{@link BooleanUtils#isFalse(Boolean)}
	 * 		{@link BooleanUtils#isNotFalse(Boolean)}
	 */
	@Test
	public void testIsXxx() {
		// Test for BooleanUtils#isTrue()
		assertThat(BooleanUtils.isTrue(true), is(true));
		assertThat(BooleanUtils.isTrue(false), is(false));

		// Test for BooleanUtils#isNotTrue()
		assertThat(BooleanUtils.isNotTrue(true), is(false));
		assertThat(BooleanUtils.isNotTrue(false), is(true));

		// Test for BooleanUtils#isFalse()
		assertThat(BooleanUtils.isFalse(true), is(false));
		assertThat(BooleanUtils.isFalse(false), is(true));

		// Test for BooleanUtils#isNotFalse()
		assertThat(BooleanUtils.isNotFalse(true), is(true));
		assertThat(BooleanUtils.isNotFalse(false), is(false));
	}

– toXxx()
Boolean値を指定された型に変換します。

	/**
	 * Test for
	 * 		{@link BooleanUtils#toInteger(Boolean, int, int, int)}
	 * 		{@link BooleanUtils#toStringOnOff(boolean)}
	 * 		{@link BooleanUtils#toStringYesNo(boolean)}
	 * 		{@link BooleanUtils#toString(Boolean, String, String, String)}
	 */
	@Test
	public void testToXxxx() {
		// Test for BooleanUtils#isInteger()
		assertThat(BooleanUtils.toInteger(true, 1, 0, 9), is(1));
		assertThat(BooleanUtils.toInteger(false, 1, 0, 9), is(0));
		assertThat(BooleanUtils.toInteger(null, 1, 0, 9), is(9));

		// Test for BooleanUtils#toStringOnOff()
		assertThat(BooleanUtils.toStringOnOff(Boolean.TRUE), is("on"));
		assertThat(BooleanUtils.toStringOnOff(Boolean.FALSE), is("off"));
		assertThat(BooleanUtils.toStringOnOff(null), nullValue());

		// Test for BooleanUtils#toStringYesNo()
		assertThat(BooleanUtils.toStringYesNo(Boolean.TRUE), is("yes"));
		assertThat(BooleanUtils.toStringYesNo(Boolean.FALSE), is("no"));
		assertThat(BooleanUtils.toStringYesNo(null), nullValue());

		// Test for BooleanUtils#toString()
		assertThat(BooleanUtils.toString(Boolean.TRUE, "true", "false", null), is("true"));
		assertThat(BooleanUtils.toString(Boolean.FALSE, "true", "false", null), is("false"));
		assertThat(BooleanUtils.toString(null, "true", "false", null), nullValue());
	}

CharSetUtils

CharSetインスタンスの操作を行います。
このクラスはnull入力を正常に処理します。 null入力に対しては例外はスローされません。各メソッドは、その動作をより詳細に記録します。
#ThreadSafe#

– containsAny()
指定された文字列内に文字が存在するかどうかを確認します。

	/**
	 * Test for {@link CharSetUtils#containsAny(String, String...)}
	 */
	@Test
	public void testContainsAny() {
		assertFalse(CharSetUtils.containsAny(null, (String) null));
		assertFalse(CharSetUtils.containsAny(null, ""));

		assertFalse(CharSetUtils.containsAny("", (String) null));
		assertFalse(CharSetUtils.containsAny("", ""));
		assertFalse(CharSetUtils.containsAny("", "a-e"));

		assertFalse(CharSetUtils.containsAny("hello", (String) null));
		assertFalse(CharSetUtils.containsAny("hello", ""));
		assertTrue(CharSetUtils.containsAny("hello", "a-e"));
		assertTrue(CharSetUtils.containsAny("hello", "l-p"));
	}

– count()
指定された文字列に存在する文字数を返します。

	/**
	 * Test for {@link CharSetUtils#count(String, String...)}
	 */
	@Test
	public void testCount() {
		assertThat(CharSetUtils.count(null, (String) null), is(0));
		assertThat(CharSetUtils.count(null, ""), is(0));

		assertThat(CharSetUtils.count("", (String) null), is(0));
		assertThat(CharSetUtils.count("", ""), is(0));
		assertThat(CharSetUtils.count("", "a-e"), is(0));

		assertThat(CharSetUtils.count("hello", (String) null), is(0));
		assertThat(CharSetUtils.count("hello", ""), is(0));
		assertThat(CharSetUtils.count("hello", "a-e"), is(1));
		assertThat(CharSetUtils.count("hello", "l-p"), is(3));
	}

– delete()
指定された文字列にある文字を削除します。

	/**
	 * Test for {@link CharSetUtils#delete(String, String...)}
	 */
    @Test
    public void testDelete() {
    	assertThat(CharSetUtils.delete(null, (String) null), nullValue());
    	assertThat(CharSetUtils.delete(null, ""), nullValue());

    	assertThat(CharSetUtils.delete("", (String)null), is(""));
    	assertThat(CharSetUtils.delete("", ""), is(""));
    	assertThat(CharSetUtils.delete("", "a-e"), is(""));

    	assertThat(CharSetUtils.delete("hello", (String)null), is("hello"));
    	assertThat(CharSetUtils.delete("hello", ""), is("hello"));
    	assertThat(CharSetUtils.delete("hello", "a-e"), is("hllo"));
    	assertThat(CharSetUtils.delete("hello", "l-p"), is("he"));
    	assertThat(CharSetUtils.delete("hello", "z"), is("hello"));
    }

– keep()
指定された文字列に存在する文字をすべて保持します。

	/**
	 * Test for {@link CharSetUtils#keep(String, String...)}
	 */
	@Test
	public void testKeep() {
		assertThat(CharSetUtils.keep(null, (String) null), nullValue());
		assertThat(CharSetUtils.keep(null, ""), nullValue());

		assertThat(CharSetUtils.keep("", (String) null), is(""));
		assertThat(CharSetUtils.keep("", ""), is(""));
		assertThat(CharSetUtils.keep("", "a-e"), is(""));

		assertThat(CharSetUtils.keep("hello", (String) null), is(""));
		assertThat(CharSetUtils.keep("hello", ""), is(""));
		assertThat(CharSetUtils.keep("hello", "xyz"), is(""));
		assertThat(CharSetUtils.keep("hello", "a-z"), is("hello"));
		assertThat(CharSetUtils.keep("hello", "oleh"), is("hello"));
		assertThat(CharSetUtils.keep("hello", "el"), is("ell"));
	}

– squeeze()
指定されたセットに記載されている文字の繰り返しを絞り込みます。

	/**
	 * Test for {@link CharSetUtils#squeeze(String, String...)}
	 */
	@Test
	public void testSqueeze() {
		assertThat(CharSetUtils.squeeze(null, (String) null), nullValue());
		assertThat(null, CharSetUtils.squeeze(null, ""), nullValue());

		assertThat(CharSetUtils.squeeze("", (String) null), is(""));
		assertThat(CharSetUtils.squeeze("", ""), is(""));
		assertThat(CharSetUtils.squeeze("", "a-e"), is(""));

		assertThat(CharSetUtils.squeeze("hello", (String) null), is("hello"));
		assertThat(CharSetUtils.squeeze("hello", ""), is("hello"));
		assertThat(CharSetUtils.squeeze("hello", "a-e"), is("hello"));
		assertThat(CharSetUtils.squeeze("hello", "l-p"), is("helo"));
		assertThat(CharSetUtils.squeeze("helloo", "l"), is("heloo"));
		assertThat(CharSetUtils.squeeze("helloo", "^l"), is("hello"));
	}

【Apache Commons Lang】いかがだったでしょうか。
次回も引き続き【Apache Commons Lang】をご紹介します。

【関連記事】
Google App Engine 第一回
Google App Engine 第二回
Javaのライブラリを手軽にテストしたい!! Groovy入門 第1回
Google App Engine 第三回
Google App Engine 第四回
Google App Engine 第五回
Google App Engine 第六回
Google App Engine 第七回
Google App Engine 第八回
Google App Engine 第九回
Google App Engine 第十回
Google App Engine 第十一回
Google App Engine 第十二回
AngularJS入門01
AngularJS入門02
AngularJS入門03
AngularJS入門04
AngularJS入門05
AngularJS入門06
AngularJS入門07
AngularJS入門08
AngularJS入門09
攻略 Elevator Saga =基本編=
攻略 Elevator Saga =応用編=
Java Tips -=-= Library =-=- JodaTime [前半]
Java Tips -=-= Library =-=- JodaTime [後半]
Java Tips -=-= Library =-=- Lombok Part 1
Java Tips -=-= Library =-=- Lombok Part 2
Java Tips -=-= Library =-=- Lombok Part 3
Java Tips -=-= Library =-=- Lombok Part 4
Java Tips -=-= Library =-=- Apache Commons Lang Part 1