Saturday, November 29, 2014

Java Configuration API - Java 7 or Java 8 ?

Java 7 vs Java 8 for Apache Tamaya Config

Since the Apache mailing list is preventing me at all cost my more comprehensive addition to the current Tamaya discussion about Java 7 vs. Java 8, I now decided to write a blog.

I will shortly show here a small example how a modern configuration API written in Java 8 significantly differs from one written in Java 7 and why it is worth to do it in Java 8, so read ahead:


  1. the target is to build a modern API.
  2. there are many developers that do not use Spring or other technologies, where adoption is much faster.
  3. Wildfly as well as Weblogic 12.1.3 are Java 8 certified AFAIK (to be verified, since EE7 TCK does not run on Java 8...)
  4. Adoption within one year will be great.
  5. Java 8 is more than Lambdas and streams!
  6. Java 8 is the future! And we design for the future, we do not want to be one additional config framework.
  7. We can still provide a backport for Java 7. The core of Tamaya will be quite small. It should be possible to provide a backport within a couple of hours.

I give you an example here, e.g. let us start on the PropertyProvider:


In Java 8:


public interface PropertyProvider {


   Optional<String> get(String key);
   boolean containsKey(String key);
   Map<String, String> toMap();
   MetaInfo getMetaInfo();


   default boolean hasSameProperties(PropertyProvider provider) {...}
   default Set<String> keySet(){...}
   default ConfigChangeSet load(){...}
   default boolean isMutable(){...}
   default void apply(ConfigChangeSet change){...}
}


In Java 7:


With Java 7 this would be (to provide similar comfort, but on the cost of implementation dependencies and limited flexibility because of missing behavioural inheritance):


public interface PropertyProvider {


   String get(String key); // @throws ConfigException if no value
   String getOrDefault(String key, String value);


   boolean containsKey(String key);
   Map<String, String> toMap();
   MetaInfo getMetaInfo();


   default boolean hasSameProperties(PropertyProvider provider) {...}
   default Set<String> keySet(){...}
   default ConfigChangeSet load(){...}
   default boolean isMutable(){...}
   default void apply(ConfigChangeSet change){...}
}


protected abstract class AbstractPropertyProvider implements PropertyProvider {
   public boolean hasSameProperties(PropertyProvider provider) {...}
   public Set<String> keySet(){...}
   public ConfigChangeSet load(){...}
   public boolean isMutable(){...}
   public void apply(ConfigChangeSet change){...}
}


Example 2: Configuration


Looking at Configuration and the singleton access there things get even worse:


In Java 8:


public interface Configuration extends PropertyProvider{


   <T> Optional<T> get(String key, Class<T> type);
   void addPropertyChangeListener(PropertyChangeListener l);
   void removePropertyChangeListener(PropertyChangeListener l);


   default OptionalBoolean getBoolean(String key){... }
   default OptionalInt getInteger(String key){... }
   default OptionalLong getLong(String key){... }
   default OptionalDouble getDouble(String key){...  }
   default <T> Optional<T> getAdapted(String key, PropertyAdapter<T> adapter){... }
   default Set<String> getAreas(){... }
   default Set<String> getTransitiveAreas(){... }
   default Set<String> getAreas(final Predicate<String> predicate){... }
   default Set<String> getTransitiveAreas(Predicate<String> predicate){... }
   default boolean containsArea(String key){... }
   default Configuration with(ConfigOperator operator){... }
   default <T> T query(ConfigQuery<T> query){...}
   default String getVersion(){...}


   public static boolean isDefined(String name){...}
   public static <T> T of(String name, Class<T> template){...}
   public static Configuration of(String name){...}
   public static Configuration of(){...}
   public static <T> T of(Class<T> type){... }
   public static void configure(Object instance){... }
   public static String evaluateValue(String expression){... }
   public static String evaluateValue(Configuration config, String expression){...  }
   public static void addGlobalPropertyChangeListener(PropertyChangeListener listener){... }
   public static void removeGlobalPropertyChangeListener(PropertyChangeListener listener){...}
}


In Java 7:


public interface Configuration extends PropertyProvider{
   <T> T get(String key, Class<T> type); // throws ConfigException
   <T> T getOrDefault(String key, Class<T> type, T instance);
   void addPropertyChangeListener(PropertyChangeListener l);
   void removePropertyChangeListener(PropertyChangeListener l);


   boolean getBoolean(String key){... } // throws ConfigException
   boolean getBooleanOrDefault(String key, boolean defaultVal){... }
   int getInteger(String key){... } // throws ConfigException
   int getIntegerOrDefault(String key, int defaultVal){... } 
// throws ConfigException
   long getLong(String key){... } // throws ConfigException
   long getLongOrDefault(String key, long defaultVal);
   double getDouble(String key){... } // throws ConfigException
   double getDoubleOrDefault(String key, double defaultVal);
   <T> getAdapted(String key, PropertyAdapter<T> adapter){... } 
// throws ConfigException
   <T> getAdaptedOrDefault(String key, PropertyAdapter<T> adapter, T defaultVal){... } // throws ConfigException
   Set<String> getAreas(){... }
   Set<String> getTransitiveAreas(){... }
   Set<String> getAreas(final Predicate<String> predicate){... }
// Duplicate predicate class, or introduce additional interface
   Set<String> getTransitiveAreas(Predicate<String> predicate){... } 
// Duplicate predicate class, or introduce additional interface
   boolean containsArea(String key){... }
   Configuration with(ConfigOperator operator){... }
   <T> T query(ConfigQuery<T> query){...}
   String getVersion(){...}
}


public final class ConfigManager{
  private ConfigManager(){}


   public static boolean isDefined(String name){...}
   public static <T> T of(String name, Class<T> template){...}
   public static Configuration of(String name){...}
   public static Configuration of(){...}
   public static <T> T of(Class<T> type){... }
   public static void configure(Object instance){... }
   public static String evaluateValue(String expression){... }
   public static String evaluateValue(Configuration config, String expression)
{... }
   public static void addGlobalPropertyChangeListener(
PropertyChangeListener listener){... }
   public static void removeGlobalPropertyChangeListener(
PropertyChangeListener listener){...}
}


protected abstract class AbstractConfiguration extends AbstractPropertyProvider implements Configuration{
   boolean getBoolean(String key){... } // throws ConfigException
   boolean getBooleanOrDefault(String key, boolean defaultVal){... }
   int getInteger(String key){... } // throws ConfigException
   int getIntegerOrDefault(String key, int defaultVal){... } // throws ConfigException if not found
   long getLong(String key){... } // throws ConfigException
   long getLongOrDefault(String key, long defaultVal);
   double getDouble(String key){... } // throws ConfigExceptio
   double getDoubleOrDefault(String key, double defaultVal);
   <T> getAdapted(String key, PropertyAdapter<T> adapter){... } 
// throws ConfigException
   <T> getAdaptedOrDefault(String key, PropertyAdapter<T> adapter, T defaultVal)  
{...} // throws ConfigException
   public Set<String> getAreas(){... }
   public Set<String> getTransitiveAreas(){... }
   public Set<String> getAreas(final Predicate<String> predicate){... }
   public Set<String> getTransitiveAreas(Predicate<String> predicate){... }
   public boolean containsArea(String key){... }
   public Configuration with(ConfigOperator operator){... }
   public <T> T query(ConfigQuery<T> query){...}
   public String getVersion(){...}
}


And even when looking from the client side:


Java 8:


String value = Configuration.of().get("a.b.c").orElse(MyClass::calculateDefault);


Java 7:


String value = ConfigurationManager.getConfiguration().getOrDefault("a.b.c", null);
if(value==null){
 value = calculateDefault();
}


So obviously the strength of Java 8 are far beyond Streams and Lambdas:
  • The API footprint for clients overall is half the size.
  • The implementations of APIs/SPIs is much more easier and does not introduce implementation dependencies on abstract classes
  • Users must known much less artefacts to use the API!
  • It is much more flexible and extendable (eg method references)
  • ...

The above case with the deferred calculation is additionally a simple but common use case for Lambda usage. Considering implementation use cases like filtering and mapping/combining of configuration to other things Streams are incredibly useful as well. Similarly we would loose for sure great support from some of the most communities like SouJava and LJC.

So I hope I have now convinced really everybody that it is NOT worth to stick on Java 7, just because we would have faster adoption ;-) ! Do the API right for Java 8 and if enough people ask for do a backport. With the current relative small size of Tamaya a backport should be doable in about 3-4 hours ;)

34 comments:

  1. Nice comparison! But I really don't know when my customers will be ready for Java 8. Maybe in a year or two. Currently working for a customer still on Java 6!

    ReplyDelete
    Replies
    1. Apache Tamaya will also require some time to be really final in version 1.0. 6 month would be quite fast, I assume...

      Delete
  2. Are you sure that you can use "default" in Java 7 interfaces:

    >>> With Java 7 this would be (to provide similar comfort, but on the cost of implementation dependencies and limited flexibility because of missing behavioural inheritance):

    ...

    default boolean hasSameProperties(PropertyProvider provider) {...}
    default Set keySet(){...}
    default ConfigChangeSet load(){...}
    default boolean isMutable(){...}
    default void apply(ConfigChangeSet change){...}
    }

    ReplyDelete
  3. Java language was discovered by James Gosling of Sun Micro systems in 1991. Although C, C++ like programming languages were present in the market but due to fix platform constraint, web developers were unable to develop high end applications.Java

    ReplyDelete
  4. Both the source code and annotated specification information exist side by side leading to a simplified development model for
    Java developers(Java Developer "www.dev2one.com"). This information access simplicity is critical to outsource Java development
    where Java developers need to be on the same page.
    java

    ReplyDelete
  5. Is there a mailing list for Apache Tamaya? The webpage just says ". Basically it is enough to just drop as a mail on our [developer mailing list][1].", without a hyperlink. :(

    ReplyDelete
  6. Hi Admin, I went through your article and it’s totally awesome. You can consider including RSS feed for easy content sharing, So that you can drive huge traffic to your blog. Hadoop Training in Chennai | Big Data Training in Chennai

    ReplyDelete
  7. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

    ReplyDelete
  8. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

    ReplyDelete
  9. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

    ReplyDelete
  10. I cant wait to check out some of these blogs! I’ve really wanted to start learning more about cars and auto repairs lately and I think this will help a lot. I think it can save my family some money if we knew how to do some repairs at home.! Thanks again for all the options.

    bike spa services in mumbai
    house cleaning services in mumbai
    car wash services in mumbai

    ReplyDelete
  11. Thank you for sharing such a nice and interesting blog with us. i have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle. please keep on updates. hope it might be much useful for us. keep on updating.
    Sharepoint Training in Chennai

    ReplyDelete
  12. Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.

    Salesforce Training in Chennai

    ReplyDelete
  13. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.I'm very happy for this blog site my comment post.
    java training in chennai

    ReplyDelete
  14. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai

    ReplyDelete


  15. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Ios training in chennai

    ReplyDelete
  16. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  17. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Weblogic Training in Chennai

    ReplyDelete
  18. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    Java Training in Chennai

    ReplyDelete
  19. Really Good blog post.provided a helpful difference between java7 and java 8 API .keep updating...
    Digital marketing company in Chennai

    ReplyDelete
  20. Thanks for your post! Through your pen I found the problem up interesting! I believe there are many other people who are interested in them just like me! Thanks your shared!...
    happy wheels

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts. java training in chennai

    ReplyDelete
  23. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic.


    Pawn Shop

    Pawn Loans

    Pawn Shops

    Pawn Loan

    Pawn Shop near me

    ReplyDelete
  24. Informative article, thanks for sharing your views and knowledge for us... it is very glad to read your article about java...

    Java courses in chennai

    ReplyDelete
  25. The Spring Framework is a lightweight framework for developing Java enterprise applications. It provides high performing, easily testable and reusable code. Spring handles the infrastructure as the underlying framework so that you can focus on your application.Spring is modular in design, thereby making creation, handling and linking of individual components so much easier. Spring implements Model View Container(MVC) design pattern.
    spring custom validator example

    ReplyDelete
  26. The Spring Framework is a lightweight framework for developing Java enterprise applications. It provides high performing, easily testable and reusable code. Spring handles the infrastructure as the underlying framework so that you can focus on your application.Spring is modular in design, thereby making creation, handling and linking of individual components so much easier. Spring implements Model View Container(MVC) design pattern.
    spring mvc validation example

    ReplyDelete
  27. It's Really A Great Post. Looking For Some More Stuff
    I really enjoyed reading the Post. It was very informative and useful for me.
    Best Java Training institute in Bangalore

    ReplyDelete
  28. Thank you for posting, its a nice post and very informative, looking for some more stuff.
    Best IT Training in Bangalore

    ReplyDelete