Wednesday, January 27, 2016

Using Traits in Java 8

In one of my earlier articles I mentioned a programming component called "traits". These constructs have existed for many years in other programming languages like Scala and PHP, but have only recently been available through default methods in Java. I will not go into the possibilities with using traits in this article, but I will show you a neat trick we use a lot at Speedment that you can use if you ever need to stream over a collection of different objects and separate those that fulfill a number of traits.

Say that you have two noun classes Person and Elephant. There is no reason really why persons and elephants should belong to the same super class; elephants are intelligent four-legged creatures and most humans are not. You might still find the two of them in the same computer system and sometimes you even need to store them in the same collection. One way of operating on this collection of various living beings without making them share a common ancestor, (which would totally be just a theory), you can give them similar traits.

Take a look at this interface:


interface HasName extends Document {
    final String NAME = "name";

    default String getName() {
        return get(NAME);
    }
    
    default void setName(String name) {
        put(NAME, name);
    }
}

Using the Abstract Document Pattern presented earlier, the trait can set and get the attribute "name" from a map. If we now want to iterate over our collection of many living things that might or might not implement our specified traits, we can easily do it like this:


final Set<Object> livingBeings = new HashSet<>();

livingBeings.add(new Person(...));
livingBeings.add(new Person(...));
livingBeings.add(new Elephant(...));

livingBeings.stream()
    .filter(HasName.class::isInstance)
    .filter(HasAge.class::isInstance)
    .filter(HasWeight.class::isInstance)
    .map(p -> (HasName & HasAge & HasWeight) p)
    .forEach(p ->
        System.out.println(
            p.getName() + " is " +
            p.getAge() + " years old and weighs " +
            p.getWeight() + " pounds."
        )
    );

Using the and-character (&) we can cast instances that implement all the required traits into a dynamic type, without them sharing an ancestor.

Was this interesting? In the following article I present a more formal definition of the Trait Pattern in java.

5 comments:

  1. Hi Emil !
    Thank you very much for the post. Great Job!
    However, I have a concern regarding the HasName interface's method.
    Am I correct saying that there should be no return keyword for the void method ?
    Regards, Eugene

    ReplyDelete
    Replies
    1. You are absolutely right about that! I have fixed that typo now. Thanks for the help!

      Sincerely, Emil

      Delete
  2. Java.net is moving to a new location in April 2017. All administrators, please copy your project resources before closing java.net

    ReplyDelete
  3. This is an impressive post to understand.

    ReplyDelete
  4. Compromised data integrity may be harmful to any sized business. Besides, database management is an assignment that has become more and more complicated. There are many database tools available in the technology market. See more data homework

    ReplyDelete