Tuesday, August 1, 2017

Java Lambdas Tips

Creating a lookup method for an Enum



Without lambdas:

public enum Type {
     
     INFO,
     WARNING,
     ERROR;

     final static Map<String, Type> lookup = new HashMap<String, Type>();
     
     static {
      for(Type type : Type.values()) {
       lookup.put(type.name().toLowerCase(), type);
      }
     }

       
     public static Type of(String value){
         Type val = lookup.get(value == null ? null : value.toLowerCase());
         if(val == null){
             val = ERROR;
         }
         return val;
     }
}

With lambda:


public enum Type {
     INFO,
     WARNING,
     ERROR;

     final static Map<String, Type> lookup = Arrays.stream(Type.values())
          .collect(Collectors.toMap(t -> t.name().toLowerCase(), Function.identity()));

     public static Type of(String value){
         Type val = lookup.get(value == null ? null : value.toLowerCase());
         if(val == null){
            val = ERROR;
         }
         return val;
     }
}

Wednesday, January 11, 2017

Switching DNS in Ubuntu Linux

One approach to switch among different development environments (QA, Integration, Stage, etc) is to have different DNS configs for routing one domain.

In Ubuntu you can setup different network connections with different DNS servers and be able to switch from network top menu,

1. Simply add a new network connection (may need two, one for Ethernet and one for WiFi)


2. Configure DNS settings


3. Switch to new connection from top menu


4. Test with nslookup command