Java/자카르타 Apache Commons-Collection Test_001 우혁이 아빠 2012. 3. 26. 11:42 /** * Apache Commons-Collection Test_001 * @throws Exception */ @SuppressWarnings("unchecked") @Test public void test021_CollectionUtils() throws Exception { /* * CollectionUtils 테스트 */ List list1 = new ArrayList(); // list1 객체가 널이거나 또는 size()가 0인지 확인 boolean result1 = CollectionUtils.isEmpty(list1); assertThat(result1, is(true)); list1.add("args1"); // list1 객체가 널이 아니거나 또는 size()가 0이 아닌지 확인 boolean result2 = CollectionUtils.isNotEmpty(list1); assertThat(result2, is(true)); list1.add("args2"); list1.add("args3"); list1.add("args4"); list1.add("args5"); list1.add("args6"); list1.add("args7"); list1.add("args8"); list1.add("args9"); list1.add("args10"); // 원하는 조건을 만든다. Predicate predicate1 = new Predicate(){ public boolean evaluate(Object object) { if (((String) object).equals("args1")) { return true; } else { return false; } } }; // 만들어 놓은 조건에 맞는 것이 있는지 확인하여 결과를 리턴해 준다. boolean result3 = CollectionUtils.exists(list1, predicate1); assertThat(result3, is(true)); // 원하는 조건을 만든다. Predicate predicate2 = new Predicate(){ public boolean evaluate(Object object) { int parsedInt = Integer.parseInt(((String) object).substring(4)); if (parsedInt >= 3 && parsedInt <= 5) { return true; } else { return false; } } }; // 조건에 맞춰 입력을 받는 Collection 객체가 생성되었다. Collection predicatedCollection = CollectionUtils.predicatedCollection(new ArrayList(), predicate2); // predicatedCollection에 객체를 add해 보면 만들어진 조건에 맞으면 들어가고 // 조건에 맞지않으면 IllegalArgumentException이 발생된다. for (String s : list1) { try { predicatedCollection.add(s); } catch (IllegalArgumentException e) { System.out.println(s + " : 조건에 맞지 않습니다." ); } } logger.debug("predicatedCollection : " + predicatedCollection.toString()); assertThat(predicatedCollection.size(), is(3)); } 저작자표시