25 April 2012

Payload extractor superclass

This post by Jonathan Wright contains an alternative strategy for Adam Bien's payload extractor. It uses a superclass instead of interceptors.

23 April 2012

Builder-Style DTO

MyDto is a Data Transfer Object that can be built using a chain of building calls.
You start by calling a factory method and then you add the info for MyDto step by step to the Builder. Each time this returns the builder, so you can chain the calls.
When you are finished, you call the build call, which returns the Data Transfer Object:

MyDto dto  = new MyDto.Builder()
       .name("Jef Blaaskop")
       .address("Antigoon 4")
       .city("Amoras")
       .build(); 
 
The constructor of MyDto is private: you can only make it using the Builder. The Builder is an inner class of MyDto, so it can call the private constructor.
public class MyDto implements Serializable{
    private String address;
    private String name;
    private String city;

    private MyDto (String name,String address,String city) {
        this.city= city;
        this.address = address;
        this.name = name;
    }

    private MyDto() {}

    public static class Builder{
        private MyDto dto;
         Builder(){
            this.dto= new MyDto ();
        }

        public Builder address(String address){
           // some checking
            this.dto.setAddress(address);
            return this;
        }

        public Builder name(String name){
            if(name == null)
                throw new IllegalArgumentException("Anonymous not allowed");
            this.dto.setName(name);
            return this;
        }

      public Builder city(String city){
           // some checking
            this.dto.setCitys(city);
            return this;
        }

        public MyDto build(){
            return this.dto;
        }
     }      // end Builder 
// remaining getters / setters ommitted
} // end MyDto

22 April 2012

DAO Example

public interface AnniversaryDao {
  Anniversary getByYear(int years);
}
public class AnniversaryJdbcDao implements AnniversaryDao{
  private static final String GET_BY_YEAR = 
    "SELECT * from Anniversary WHERE years = ?";
  //...
  public Anniversary getByYear(int years) {
    //…
    PreparedStatement statement = connection.prepareStatement(GET_BY_YEAR);
    statement.setInt(1,years);
    ResultSet rs = statement.executeQuery();
    if (rs.next()){
      result = new Anniversary(
      rs.getInt("years"),
      rs.getString("material"),
      rs.getString("flowers"));
    }
   //  ....
  return result;
  }
}
public class AnniversaryJpaSeDao implements AnniversaryDao{
   EntityManagerFactory emf = Persistence
    .createEntityManagerFactory("JPA-03PU");

  private EntityManager getEntityManager() {
    return emf.createEntityManager();
  }
  //...
  public Anniversary getByYear(int years) {
    return getEntityManager().createNamedQuery(findAnniversayByYear)
      .setParameter("years", years)
      .getSingleResult();
  }
}
@ManagedBean
@RequestScoped
public class AnniversaryJpaEe6Dao implements AnniversaryDao{
  @¨PersistenceContext private EntityManager em;

  //...
  public Anniversary getByYear(int years) {
    return em.createNamedQuery(findAnniversayByYear)
      .setParameter("years", years)
      .getSingleResult();
  }
}

Light field cameras are out

The Lytro light field camera on which we reported earlier, receives a mixed review. Apparently the camera does not work too well in bad light conditions.
The camera snaps pictures that you can refocus later on.So there's only a snap button on a tiny camera.
I think that this idea hitting the market exactly at the moment that we are shifting from paper to screens for viewing photos, makes this a promising technology.

15 April 2012

Java enum demo

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

public enum EnumDemo {
  // For this enum each value has an identification code
  // other than the internal ordinal value
  DEMANDED(10),
  SPECIFIED(20),
  EVALUATED(30),
  DUPLICATE(33),
  ESTIMATED(35),
  WORKAROUND (36),
  ACCEPTED(37),
  PLANNED(40),
  BUSY(50),
  IMPLEMENTED(60),
  REJECTED(100);

  private int code;

  /**
   * @param code  constructor takes code to  be associated with enum
   */
  EnumDemo(int code) {
    this.code = code;
  }

  /**
   * Helper map for reverse lookup in getByCode method
   */
  private static Map<Integer,EnumDemo> enumByCode;
  static{
    enumByCode = new HashMap<Integer, EnumDemo>();
    for (EnumDemo val: EnumDemo.values()) {
      enumByCode.put(val.getCode(),val);
    }
  }

  public int getCode() {
    return code;
  }

  /**
   * reverse lookup
   * @param code the code of the enum
   * @return the enum or null if the code does not exist
   */
  public static EnumDemo getByCode(int code){
    return enumByCode.get(code);
  }

  /**
   * test if a code exists
   * @param code
   * @return  true if the code exists
   */
  public static boolean isCodeValid(int code){
    return enumByCode.containsKey(code);
  }

  /**
   * enumToString: EnumDemo.DEMANDED to "DEMANDED"
   * @return enum value name as a String
   */
  public String getName(){
    return toString();
  }

  /**
   * StringToEnum: "  demanded" to EnumDemo.DEMANDED
   * @param name case insensitive and leading and trailing spaces are ignored
   * @return  the enum constant
   */
  public static EnumDemo getByName(String name){
    return EnumDemo.valueOf(name.trim().toUpperCase());
  }
 
  /**
   * convert between enum and internal ordinal number
   * not recommended to expose this, just here for demo purposes
   * @return internal ordinal number of the constant
   */
  public int getOrdinal(){
    return getOrdinal();
  }
 
  /**
   * convert from ordinal number to enum. Not recommended.
   * @param ordinal
   * @return  the enum
   */
  public static EnumDemo getByOrdinal(int ordinal){
    return EnumDemo.values()[ordinal];
  } 
 
  // some tests and subsets
  // alternate methods can be written using the codes
    
  /**
   *
   * @return  is this enum in a range?
   */
  public boolean isOK(){
    return ACCEPTED.compareTo(this) <= 0 && IMPLEMENTED.compareTo(this)  >= 0;
  }

  /**
   * @return  an enum subrange
   */
  public static EnumSet<EnumDemo> getOK(){
    return EnumSet.range(ACCEPTED,IMPLEMENTED);
  }

  /**
   * @return  is this enum in a subset?
   */
  public boolean isNOK(){
    return this == DUPLICATE
      || this == WORKAROUND
      || this == REJECTED;
  }

  /**
   *
   * @return  get an enum subset
   */
  public static EnumSet<EnumDemo> getNOK(){
    return EnumSet.of(DUPLICATE,WORKAROUND,REJECTED);
  }
} 

12 April 2012

idPhone

The Arabian Emirates is planning to incorporate the ID card in residents mobile phones.
This opens unprecedented possibilities for monitoring the population

  • Through the mobile phone operator the government will know the location of every single citizen
  • The ID card will also work using NFC. NFC allows the contents to be read from a short distance (currently max 20cm, and for practical purposes 4 cm), even if your battery is dead. If they improve this distance a bit it will be very easy to register everybody passing a checkpoint.
All in all this allows a small group of people to effectively monitor and control a large group of people.