From 0d6bdae6008f657d12b1c376372487fe02c2e183 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 5 Oct 2022 14:50:53 +0200 Subject: [PATCH] Use instanceof patterns We have quite a bit of checking and casting going on, simplify a bit but using JDK17 constructs. Change-Id: Iffd0537e9832f8372b696fba7025c49c0fbf597c Signed-off-by: Robert Varga --- .../impl/BindingRuntimeTypesFactory.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/BindingRuntimeTypesFactory.java b/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/BindingRuntimeTypesFactory.java index 29df37b404..106c45496e 100644 --- a/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/BindingRuntimeTypesFactory.java +++ b/binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/BindingRuntimeTypesFactory.java @@ -82,21 +82,19 @@ final class BindingRuntimeTypesFactory implements Mutable { // index module's identities and RPC input/outputs for (var gen : modGen) { - if (gen instanceof IdentityGenerator) { - ((IdentityGenerator) gen).runtimeType().ifPresent(identity -> { + if (gen instanceof IdentityGenerator idGen) { + idGen.runtimeType().ifPresent(identity -> { safePut(identities, "identities", identity.statement().argument(), identity); }); } // FIXME: do not collect these once we they generate a proper RuntimeType - if (gen instanceof RpcGenerator) { - final QName rpcName = ((RpcGenerator) gen).statement().argument(); + if (gen instanceof RpcGenerator rpcGen) { + final QName rpcName = rpcGen.statement().argument(); for (var subgen : gen) { - if (subgen instanceof RpcInputGenerator) { - ((RpcInputGenerator) subgen).runtimeType() - .ifPresent(input -> rpcInputs.put(rpcName, input)); - } else if (subgen instanceof RpcOutputGenerator) { - ((RpcOutputGenerator) subgen).runtimeType() - .ifPresent(output -> rpcOutputs.put(rpcName, output)); + if (subgen instanceof RpcInputGenerator inputGen) { + inputGen.runtimeType().ifPresent(input -> rpcInputs.put(rpcName, input)); + } else if (subgen instanceof RpcOutputGenerator outputGen) { + outputGen.runtimeType().ifPresent(output -> rpcOutputs.put(rpcName, output)); } } } @@ -108,11 +106,10 @@ final class BindingRuntimeTypesFactory implements Mutable { private void indexRuntimeTypes(final Iterable generators) { for (Generator gen : generators) { - if (gen instanceof AbstractExplicitGenerator && gen.generatedType().isPresent()) { - final var type = ((AbstractExplicitGenerator) gen).runtimeType().orElseThrow(); - final var javaType = type.javaType(); - if (javaType instanceof GeneratedType) { - final var name = javaType.getIdentifier(); + if (gen instanceof AbstractExplicitGenerator explicitGen && gen.generatedType().isPresent()) { + final var type = explicitGen.runtimeType().orElseThrow(); + if (type.javaType() instanceof GeneratedType genType) { + final var name = genType.getIdentifier(); final var prev = allTypes.put(name, type); verify(prev == null || prev == type, "Conflict on runtime type mapping of %s between %s and %s", name, prev, type); @@ -120,12 +117,12 @@ final class BindingRuntimeTypesFactory implements Mutable { // Global indexing of cases generated for a particular choice. We look at the Generated type // and make assumptions about its shape -- which works just fine without touching the // ChoiceRuntimeType for cases. - if (type instanceof CaseRuntimeType) { - final var ifaces = ((GeneratedType) javaType).getImplements(); + if (type instanceof CaseRuntimeType caseType) { + final var ifaces = genType.getImplements(); // The appropriate choice and DataObject at the very least. The choice interface is the first // one mentioned. verify(ifaces.size() >= 2, "Unexpected implemented interfaces %s", ifaces); - choiceToCases.put(ifaces.get(0).getIdentifier(), (CaseRuntimeType) type); + choiceToCases.put(ifaces.get(0).getIdentifier(), caseType); } } } -- 2.36.6