Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

API Enhancements Missing Since Java 8

DZone 's Guide to

API Enhancements Missing Since Java 8

Here are the API enhancements you might have missed since Java 8.

· Java Zone ·
Free Resource

Starting from Java 8, interfaces can include default and static methods, and we have lambda expressions for functional interfaces (interfaces with single abstract methods). Here are some API enhancements that could make Java API more user-friendly, without breaking backward compatibility.

Some of the API enhancements that have been missed since Java 8 are as follows:

1.  Enhancing interfaces with more intuitive methods.

For example, the interface Comparable<T> in the java.lang package has an abstract method compareTo, which is defined as follows: 

public int compareTo(T obj);

We could have also easily enhanced the interface to include some more intuitive methods, like:

public default boolean lessThan(T obj) {
    return this.compareTo(obj) < 0;
}

public default boolean greaterThan(T obj) {
    return this.compareTo(obj) > 0;
}

public default boolean equalTo(T obj) {
    return this.compareTo(obj) == 0;
    // or probably
    // return this.equals(obj);
}

public default boolean notEqualTo(T obj) {
    return this.compareTo(obj) != 0;
    // or probably
    // return !this.equals(obj);
}

public default boolean lessThanEqualTo(T obj) {
    return this.compareTo(obj) <= 0;
}
public default boolean greaterThanEqualTo(T obj) {
    return this.compareTo(obj) >= 0;
}


All of the existing interfaces could be enhanced with more intuitive methods on similar lines.

2.  Enhancing the pre-existing FunctionalInterface types to be usable as the standard functional interfaces, provided in the java.util.function package.

For example, the FileFilter interface in java.io package, which is a functional interface since it has only one abstract method accept, defined as follows:

 public boolean accept(File file); 

This interface could have been enhanced to extend from the Predicate interface of the  java.util.function package as follows:

public interface FileFilter extends Predicate<File> {
    public abstract boolean accept(File file);
    @Override
    public default boolean test(File file) {
        return accept(file);
    }
}


This would help make FileFilter usable wherever Predicate is allowed. 

For example, if we have a Collection<File>, it would be possible to use removeIf method on this by passing an instance of FileFilter.

Also, another advantage is that FileFilter would be inheriting the default methods from the  Predicate interface.

Most of the pre-existing functional interfaces could be enhanced similarly.

Another example is the ActionListener interface from the java.awt package used to extend from theConsumer<ActionEvent>.

3. Adding a few more functional interfaces into the list of standard functional interfaces, part of  java.util.function  package.

An example would be a functional interface with return type void and no parameters, like the Runnable  interface, and adding a default method andThen for this type of functional interface.

For example, we may add a functional interface Work as follows:

public interface Work {
    public abstract void do();
    public default Work andThen(Work after) {
        return () -> { this.do(); after.do(); };
    }
    public default Work compose(Work previous) {
        return () -> { previous.do(); this.do(); };
    }
    public static Work noOp() {
        return () -> { };
    }
}


4.  Adding another set of standard functional interfaces, or extending the existing FunctionalInterfaces, with the throws Exception in the abstract method to throw checked exceptions.

We may add a variant of the functional interface Consumer<T> called  UncaughtConsumer<T> as follows:

@FunctionalInterface
public interface UncaughtConsumer<T> {
    public abstract void accept(T t) throws Exception;
    // and all default and static methods similar to Consumer<T>
}


Or we may extend from the Consumer<T> as follows:

@FunctionalInterface
public interface UncaughtConsumer<T> extends Consumer<T> {
    public abstract void uncaughtAccept(T t) throws Exception;
    @Override
    public default void accept(T t) {
        try {
            uncaughtAccept(t);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // add appropriate implementations for the default methods in Consumer<T>
}


This would enable the use of method references when using methods that throw an exception.

Some of these points are open to lots of discussions, especially points 2, 3, and 4. Hoping to see some of these changes in the next releases of Java.

Topics:
java 8 ,default methods ,api ,interfaces ,java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}