Posted by & filed under Java.

This is a follow up to Pattern matching in Java, where I demonstrated pattern matching on type and structure using Java 8 features. The first thing most people asked is “does it support matching on nested structures?”

The previous approach did not, at least not without creating excessive boilerplate constructors. So here’s another approach that does.

Let’s suppose we have a nested structure representing a customer like this. Illustrated as a JavaScript object literal for clarity.

{
  firstName: "Benji",
  lastName: "Weber",
  address: {
    firstLine: {
      houseNumber: 123,
      roadName: "Some Street"
    },
    postCode: "AB123CD"
  }
}

What if we want to match customers with my name and pull out my house number, road name, and post code? With pattern matching it becomes straightforward.

First we’ll create Java types to represent it such that we can create the above representation like:

Customer customer = customer(
    "Benji", 
    "Weber", 
    address(
        firstLine(123,"Some Street"), 
        "AB123CD"
    )
);

I’ll use the value object pattern I described previously to create these.

Now we just need a way to build up a structure to match against, which retains the properties we want to extract for the pattern matching we previously implemented.

Here’s what we can do. We use underscores to indicate properties we wish to extract rather than match. All other properties are matched.

// Using the customer instance from above
String address = customer.match()
    .when(a(Customer::customer).matching(
        "Benji",
        "Weber",
        an(Address::address).matching(
            a(FirstLine::firstLine).matching(_,_),
            _
        )
    )).then((houseNo, road, postCode) -> houseNo + " " + road + " " + postCode)
    .otherwise("unknown");
 
assertEquals("123 Some Street AB123CD", address);

So how does it work? Well we get the .match() method by implementing the Case interface on our value types. This interface has a default method match which returns a match builder which we can use to specify our cases.

Last time we implemented overloads to the when(..) method such that we could match on types or instances. Now we can re-use that work and add overloads that take a Match reference. e.g.

public <A,B> BiMatchConstructorBuilder<T, A, B> when(BiMatch<T, A, B> matchRef) {
// Here we can know we are matching for missing properties of types A and B
// So we can expect a function to consume these properties that accepts an A and B
    return new BiMatchConstructorBuilder<T, A, B>() {
        public <R> MatchBuilderR<T, R> then(BiFunction<A, B, R> f) {
            // ...
        }
    };
}

The matchRef can capture method references to the properties we want to extract, and then we can apply these method references to the object we are matching against to check for a match.

Lastly we simply add a couple of static methods: a(constructor) and an(constructor) for building up our matches, which return a builder that accepts either the constructor arguments, or a wildcard underscore to indicate we want to match and extract that value.

Here are some more examples to help illustrate the idea

Leave a Reply

  • (will not be published)