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:
- the target is to build a modern API.
- there are many developers that do not use Spring or other technologies, where adoption is much faster.
- 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...)
- Adoption within one year will be great.
- Java 8 is more than Lambdas and streams!
- Java 8 is the future! And we design for the future, we do not want to be one additional config framework.
- 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
// 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
// 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
// Duplicate predicate class, or introduce additional interface
Set<String> getTransitiveAreas(Predicate<String> predicate){... }
// Duplicate predicate class, or introduce additional interface
// 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){... }
PropertyChangeListener listener){... }
public static void removeGlobalPropertyChangeListener(
PropertyChangeListener listener){...}
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
// throws ConfigException
<T> getAdaptedOrDefault(String key, PropertyAdapter<T> adapter, T defaultVal)
{...} // throws ConfigException
{...} // 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 ;)