Use local variable type inference and instanceof patterns.
Change-Id: I2f9222d0e6535bfaf7655ef6afccbb2ef431ddd4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
*/
package org.opendaylight.mdsal.binding.generator.impl.reactor;
*/
package org.opendaylight.mdsal.binding.generator.impl.reactor;
-import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Verify.verify;
import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import static com.google.common.base.Verify.verify;
import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.PathExpression;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.PathExpression;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
import org.opendaylight.yangtools.yang.model.ri.type.TypeBuilder;
import org.opendaylight.yangtools.yang.model.spi.ModuleDependencySort;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
import org.opendaylight.yangtools.yang.model.ri.type.TypeBuilder;
import org.opendaylight.yangtools.yang.model.spi.ModuleDependencySort;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
}
// Start measuring time...
}
// Start measuring time...
- final Stopwatch sw = Stopwatch.createStarted();
+ final var sw = Stopwatch.createStarted();
// Step 1a: Walk all composite generators and resolve 'uses' statements to the corresponding grouping generator,
// establishing implied inheritance. During this walk we maintain 'stack' to aid this process.
// Step 1a: Walk all composite generators and resolve 'uses' statements to the corresponding grouping generator,
// establishing implied inheritance. During this walk we maintain 'stack' to aid this process.
// step of each 'augment' statement to its corresponding instantiated site.
// Then start all UsesAugmentGenerators' target resolution.
final var augments = new ArrayList<AugmentRequirement>();
// step of each 'augment' statement to its corresponding instantiated site.
// Then start all UsesAugmentGenerators' target resolution.
final var augments = new ArrayList<AugmentRequirement>();
- for (ModuleGenerator module : children) {
- for (Generator gen : module) {
+ for (var module : children) {
+ for (var gen : module) {
if (gen instanceof ModuleAugmentGenerator moduleGen) {
augments.add(moduleGen.startLinkage(this));
}
}
}
if (gen instanceof ModuleAugmentGenerator moduleGen) {
augments.add(moduleGen.startLinkage(this));
}
}
}
- for (ModuleGenerator module : children) {
+ for (var module : children) {
module.startUsesAugmentLinkage(augments);
}
LOG.trace("Processing linkage of {} augment generators", augments.size());
module.startUsesAugmentLinkage(augments);
}
LOG.trace("Processing linkage of {} augment generators", augments.size());
// Step 1c: Establish linkage along the reverse uses/augment axis. This is needed to route generated type
// manifestations (isAddedByUses/isAugmenting) to their type generation sites. Since generator tree
// iteration order does not match dependencies, we may need to perform multiple passes.
// Step 1c: Establish linkage along the reverse uses/augment axis. This is needed to route generated type
// manifestations (isAddedByUses/isAugmenting) to their type generation sites. Since generator tree
// iteration order does not match dependencies, we may need to perform multiple passes.
- for (ModuleGenerator module : children) {
+ for (var module : children) {
verify(module.linkOriginalGenerator(), "Module %s failed to link", module);
}
verify(module.linkOriginalGenerator(), "Module %s failed to link", module);
}
* algorithm in CollisionDomain. Note that we may need to walk the domains multiple times, as the process of
* solving a domain may cause another domain's solution to be invalidated.
*/
* algorithm in CollisionDomain. Note that we may need to walk the domains multiple times, as the process of
* solving a domain may cause another domain's solution to be invalidated.
*/
- final List<CollisionDomain> domains = new ArrayList<>();
+ final var domains = new ArrayList<CollisionDomain>();
collectCollisionDomains(domains, children);
boolean haveUnresolved;
do {
haveUnresolved = false;
collectCollisionDomains(domains, children);
boolean haveUnresolved;
do {
haveUnresolved = false;
- for (CollisionDomain domain : domains) {
+ for (var domain : domains) {
if (domain.findSolution()) {
haveUnresolved = true;
}
if (domain.findSolution()) {
haveUnresolved = true;
}
// We have now properly cross-linked all generators and have assigned their naming roots, so from this point
// it looks as though we are performing a simple recursive execution. In reality, though, the actual path taken
// through generators is dictated by us as well as generator linkage.
// We have now properly cross-linked all generators and have assigned their naming roots, so from this point
// it looks as though we are performing a simple recursive execution. In reality, though, the actual path taken
// through generators is dictated by us as well as generator linkage.
- for (ModuleGenerator module : children) {
+ for (var module : children) {
module.ensureType(builderFactory);
}
module.ensureType(builderFactory);
}
private void collectCollisionDomains(final List<CollisionDomain> result,
final Iterable<? extends Generator> parent) {
private void collectCollisionDomains(final List<CollisionDomain> result,
final Iterable<? extends Generator> parent) {
- for (Generator gen : parent) {
+ for (var gen : parent) {
gen.ensureMember();
collectCollisionDomains(result, gen);
if (gen instanceof AbstractCompositeGenerator<?, ?> compositeGen) {
gen.ensureMember();
collectCollisionDomains(result, gen);
if (gen instanceof AbstractCompositeGenerator<?, ?> compositeGen) {
LOG.trace("Searching for tree-scoped argument {} at {}", argument, stack);
// Check if the requested QName matches current module, if it does search the stack
LOG.trace("Searching for tree-scoped argument {} at {}", argument, stack);
// Check if the requested QName matches current module, if it does search the stack
- final Iterable<? extends Generator> last = stack.getLast();
- verify(last instanceof ModuleGenerator, "Unexpected last stack item %s", last);
+ final var last = stack.getLast();
+ if (!(last instanceof ModuleGenerator lastModule)) {
+ throw new VerifyException("Unexpected last stack item " + last);
+ }
- if (argument.getModule().equals(((ModuleGenerator) last).statement().localQNameModule())) {
- for (Iterable<? extends Generator> ancestor : stack) {
- for (Generator child : ancestor) {
+ if (argument.getModule().equals(lastModule.statement().localQNameModule())) {
+ for (var ancestor : stack) {
+ for (var child : ancestor) {
if (type.isInstance(child)) {
if (type.isInstance(child)) {
- final G cast = type.cast(child);
+ final var cast = type.cast(child);
if (argument.equals(cast.statement().argument())) {
LOG.trace("Found matching {}", child);
return cast;
if (argument.equals(cast.statement().argument())) {
LOG.trace("Found matching {}", child);
return cast;
- final ModuleGenerator module = generators.get(argument.getModule());
+ final var module = generators.get(argument.getModule());
- for (Generator child : module) {
+ for (var child : module) {
if (type.isInstance(child)) {
if (type.isInstance(child)) {
- final G cast = type.cast(child);
+ final var cast = type.cast(child);
if (argument.equals(cast.statement().argument())) {
LOG.trace("Found matching {}", child);
return cast;
if (argument.equals(cast.statement().argument())) {
LOG.trace("Found matching {}", child);
return cast;
@Override
ModuleGenerator resolveModule(final QNameModule namespace) {
@Override
ModuleGenerator resolveModule(final QNameModule namespace) {
- final ModuleGenerator module = generators.get(requireNonNull(namespace));
- checkState(module != null, "Failed to find module for %s", namespace);
+ final var module = generators.get(requireNonNull(namespace));
+ if (module == null) {
+ throw new IllegalStateException("Failed to find module for " + namespace);
+ }
verify(inferenceStack.isEmpty(), "Unexpected data tree state %s", inferenceStack);
try {
// Populate inferenceStack with a grouping + data tree equivalent of current stack's state.
verify(inferenceStack.isEmpty(), "Unexpected data tree state %s", inferenceStack);
try {
// Populate inferenceStack with a grouping + data tree equivalent of current stack's state.
- final Iterator<Iterable<? extends Generator>> it = stack.descendingIterator();
+ final var it = stack.descendingIterator();
// Skip first item, as it points to our children
verify(it.hasNext(), "Unexpected empty stack");
it.next();
while (it.hasNext()) {
// Skip first item, as it points to our children
verify(it.hasNext(), "Unexpected empty stack");
it.next();
while (it.hasNext()) {
- final Iterable<? extends Generator> item = it.next();
- verify(item instanceof Generator, "Unexpected stack item %s", item);
- ((Generator) item).pushToInference(inferenceStack);
+ final var item = it.next();
+ if (item instanceof Generator generator) {
+ generator.pushToInference(inferenceStack);
+ } else {
+ throw new VerifyException("Unexpected stack item " + item);
+ }
}
return inferenceStack.inGrouping() ? lenientResolveLeafref(path) : strictResolvePath(path);
}
return inferenceStack.inGrouping() ? lenientResolveLeafref(path) : strictResolvePath(path);
// Map a statement to the corresponding generator
private @NonNull AbstractTypeAwareGenerator<?, ?, ?> mapToGenerator() {
// Some preliminaries first: we need to be in the correct module to walk the path
// Map a statement to the corresponding generator
private @NonNull AbstractTypeAwareGenerator<?, ?, ?> mapToGenerator() {
// Some preliminaries first: we need to be in the correct module to walk the path
- final ModuleEffectiveStatement module = inferenceStack.currentModule();
- final ModuleGenerator gen = verifyNotNull(generators.get(module.localQNameModule()),
+ final var module = inferenceStack.currentModule();
+ final var gen = verifyNotNull(generators.get(module.localQNameModule()),
"Cannot find generator for %s", module);
// Now kick of the search
"Cannot find generator for %s", module);
// Now kick of the search
- final List<EffectiveStatement<?, ?>> stmtPath = inferenceStack.toInference().statementPath();
- final AbstractExplicitGenerator<?, ?> found = gen.findGenerator(stmtPath);
+ final var stmtPath = inferenceStack.toInference().statementPath();
+ final var found = gen.findGenerator(stmtPath);
if (found instanceof AbstractTypeAwareGenerator<?, ?, ?> typeAware) {
return typeAware;
}
if (found instanceof AbstractTypeAwareGenerator<?, ?, ?> typeAware) {
return typeAware;
}
// Note: unlike other methods, this method pushes matching child to the stack
private void linkUsesDependencies(final Iterable<? extends Generator> parent) {
// Note: unlike other methods, this method pushes matching child to the stack
private void linkUsesDependencies(final Iterable<? extends Generator> parent) {
- for (Generator child : parent) {
+ for (var child : parent) {
if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
LOG.trace("Visiting composite {}", composite);
stack.push(composite);
if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
LOG.trace("Visiting composite {}", composite);
stack.push(composite);
}
private void linkDependencies(final Iterable<? extends Generator> parent) {
}
private void linkDependencies(final Iterable<? extends Generator> parent) {
- for (Generator child : parent) {
+ for (var child : parent) {
if (child instanceof AbstractDependentGenerator<?, ?> dependent) {
dependent.linkDependencies(this);
} else if (child instanceof AbstractCompositeGenerator) {
if (child instanceof AbstractDependentGenerator<?, ?> dependent) {
dependent.linkDependencies(this);
} else if (child instanceof AbstractCompositeGenerator) {
}
private void bindTypeDefinition(final Iterable<? extends Generator> parent) {
}
private void bindTypeDefinition(final Iterable<? extends Generator> parent) {
- for (Generator child : parent) {
+ for (var child : parent) {
stack.push(child);
if (child instanceof AbstractTypeObjectGenerator<?, ?> typeObject) {
typeObject.bindTypeDefinition(this);
stack.push(child);
if (child instanceof AbstractTypeObjectGenerator<?, ?> typeObject) {
typeObject.bindTypeDefinition(this);