64. Adding guarded pattern labels in switch
Do you remember that type patterns for instanceof
can be refined with extra boolean
checks applied to the binding variables to obtain fine-grained use cases? Well, we can do the same for the switch
expressions that use pattern labels. The result is named guarded pattern labels. Let’s consider the following code:
private static String turnOnTheHeat(Heater heater) {
return switch (heater) {
case Stove stove -> "Make a fire in the stove";
case Chimney chimney -> "Make a fire in the chimney";
default -> "No heater available!";
};
}
Having a Stove
and a Chimney
, this switch
decides where to make a fire based on pattern labels. But, what will happen if Chimney
is electric? Obviously, we will have to plug Chimney
in instead of firing it up. This means that we should add a guarded pattern label that helps us to make the difference between an electric and non-electric Chimney
:
return switch (heater) {
case Stove stove -> "Make a fire in the stove";
case Chimney chimney
&& chimney.isElectric() -> "Plug in the chimney";
case Chimney chimney -> "Make a fire in the chimney";
default -> "No heater available!";
};
Well, that was easy, wasn’t it? Let’s have another example that starts from the following code:
enum FuelType { GASOLINE, HYDROGEN, KEROSENE }
class Vehicle {
private final int gallon;
private final FuelType fuel;
...
}
For each Vehicle
, we know the fuel type and how many gallons of fuel fit in the tank. Now, we can write a switch
that can rely on guarded pattern labels to try to guess the type of the vehicle based on this information:
private static String theVehicle(Vehicle vehicle) {
return switch (vehicle) {
case Vehicle v && v.getFuel().equals(GASOLINE)
&& v.getGallon() < 120 -> "probably a car/van";
case Vehicle v && v.getFuel().equals(GASOLINE)
&& v.getGallon() > 120 -> "probably a big rig";
case Vehicle v && v.getFuel().equals(HYDROGEN)
&& v.getGallon() < 300_000 -> "probably an aircraft";
case Vehicle v && v.getFuel().equals(HYDROGEN)
&& v.getGallon() > 300_000 -> "probably a rocket";
case Vehicle v && v.getFuel().equals(KEROSENE)
&& v.getGallon() > 2_000 && v.getGallon() < 6_000
-> "probably a narrow-body aircraft";
case Vehicle v && v.getFuel().equals(KEROSENE)
&& v.getGallon() > 6_000 && v.getGallon() < 55_000
-> "probably a large (B747-400) aircraft";
default -> "no clue";
};
}
Notice that the pattern labels are the same in all cases (Vehicle v
) and the decision is refined via the guarded conditions. The previous examples work just fine in JDK 17 and 18, but they don’t work starting with JDK 19+. Because the &&
operator was considered confusing, starting with JDK 19+, we have to deal with a refinement syntax. Practically, instead of the &&
operator, we use the new context-specific keyword when
between the pattern label and the refining boolean
checks. So, in JDK 19+, the previous code becomes:
return switch (vehicle) {
case Vehicle v when (v.getFuel().equals(GASOLINE)
&& v.getGallon() < 120) -> "probably a car/van";
case Vehicle v when (v.getFuel().equals(GASOLINE)
&& v.getGallon() > 120) -> "probably a big rig";
...
case Vehicle v when (v.getFuel().equals(KEROSENE)
&& v.getGallon() > 6_000 && v.getGallon() < 55_000)
-> "probably a large (B747-400) aircraft";
default -> "no clue";
};
In the bundled code, you can find both versions for JDK 17/18, and JDK 19+.