Monday, May 6, 2013

Apache Commons Collections - shot note


Apache Commons Collections


1. Closures are functions that can alter the object and get a reference to each object in the collection.
2. Transformers are responsible for transforming data from one format to another or from one object  to another.
3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.

/**
 * model to show details.
 */
package com.apachecommonscollections.ApacheCommonsCollections;

/**
 * @author Thanooj
 * 
 */
public class ShowDetails {

 private int id;
 private String name;
 private String addr;

 public ShowDetails() {
  super();
 }

 public ShowDetails(int id, String name, String addr) {
  super();
  this.id = id;
  this.name = name;
  this.addr = addr;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddr() {
  return addr;
 }

 public void setAddr(String addr) {
  this.addr = addr;
 }

 @Override
 public String toString() {
  return name + " " + addr + " " + id;
 }

}

--------------------------
package com.apachecommonscollections.ApacheCommonsCollections;

import org.apache.commons.collections.*;

public class PrintMe implements Closure {
 // This class implements a Singleton Pattern
 private static PrintMe ourInstance = new PrintMe();

 /**
  * Get a singleton instance of PrintIt
  */
 public static PrintMe getInstance() {
  return ourInstance;
 }

 private PrintMe() // This is a singleton, dont change this!
 {
 }

 public void execute(Object o) {
  System.out.println(o.toString());
 }
}
-----------------
package com.apachecommonscollections.ApacheCommonsCollections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import 
java.util.List;

import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;

/**
 * 1. Closures are functions that can alter the object and get a reference to each object in the collection. 
 * 2. Transformers are responsible for transforming data from one format to another or from one object to another.
 * 3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.
 */
public class App {
 public static void main(String[] args) {
  System.out.println("1. Closures are functions that can alter the object and get a reference to each object in the collection.");

  List<ShowDetails> collectionOfShowDetails = Arrays.asList(
    new ShowDetails(1, "srirama", "ayodhya"), new ShowDetails(2,
      "seetha", "midhila"), new ShowDetails(3, "lakshmana",
      "ayodhya"), new ShowDetails(4, "hanuma", "kishkindha"));
  
  CollectionUtils.forAllDo(collectionOfShowDetails, new Closure() {
   public void execute(Object o) {
    ShowDetails showDetails = (ShowDetails) o;
    assert showDetails != null;
    String s = StringUtils.defaultIfEmpty(showDetails.getName(),
      "null");
    showDetails.setName("Jai! " + s);
   }
  });
  CollectionUtils.forAllDo(collectionOfShowDetails, PrintMe.getInstance());
  System.out.println("\n---------------------------------------------------------------------------------------------------------------");
  System.out.println("\n2. Transformers are responsible for transforming data from one format to another or from one object to another.");

  Collection<String> stringOfNumbers = Arrays.asList("0", "1", "2", "3", "4", "5");
  @SuppressWarnings("unchecked")
  Collection<Integer> ints = CollectionUtils.collect(stringOfNumbers,
    new Transformer() {
     public Object transform(final Object o) {
      return Integer.valueOf((String) o);
     }
    });
  CollectionUtils.forAllDo(ints, PrintMe.getInstance());
  System.out.println("\n-------------------------------------------------------------------------------------------------------------------------");
  System.out.println("\n3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.");
  
  List<String> charsNums = Arrays.asList("A", "0", "B", "1", "C", "3", "D", "4", "E", "5");  
    @SuppressWarnings("rawtypes")
    Collection numbersOnlyList = CollectionUtils.predicatedCollection(new ArrayList(),  
        new Predicate() {  
            public boolean evaluate(Object o) {  
                try {  
                    Integer.valueOf((String) o);  
                    return true;  
                } catch (NumberFormatException e) {  
                  return false;  
                }  
            }  
        });  
    for (String s : charsNums) {  
        try {  
            numbersOnlyList.add(s);  
        } catch (IllegalArgumentException e) {  
            System.out.println("Its a String!");  
        }  
    }  
    CollectionUtils.forAllDo(numbersOnlyList, PrintMe.getInstance());  
 }
}

 


Output: 

1. 
Closures are functions that can alter the object and get a reference to each object in the collection.
Jai! srirama ayodhya 1
Jai! seetha midhila 2
Jai! lakshmana ayodhya 3
Jai! hanuma kishkindha 4

---------------------------------------------------------------------------------------------------------------

2. Transformers are responsible for transforming data from one format to another or from one object to another.
0
1
2
3
4
5

-------------------------------------------------------------------------------------------------------------------------

3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.
Its a String!
Its a String!
Its a String!
Its a String!
Its a String!
0
1
3
4
5

No comments: