We can be more expressive now that we have Java 17, let's do that.
Change-Id: I669fe4bd871c9cc269ead6b3b376af21295efc4e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
enclosedTypes = b.build();
final Set<String> cb = new HashSet<>();
- if (genType instanceof Enumeration) {
- ((Enumeration) genType).getValues().stream().map(Pair::getMappedName).forEach(cb::add);
+ if (genType instanceof Enumeration enumeration) {
+ enumeration.getValues().stream().map(Pair::getMappedName).forEach(cb::add);
}
// TODO: perhaps we can do something smarter to actually access the types
collectAccessibleTypes(cb, genType);
private void collectAccessibleTypes(final Set<String> set, final GeneratedType type) {
for (Type impl : type.getImplements()) {
- if (impl instanceof GeneratedType) {
- final GeneratedType genType = (GeneratedType) impl;
+ if (impl instanceof GeneratedType genType) {
for (GeneratedType inner : Iterables.concat(genType.getEnclosedTypes(), genType.getEnumerations())) {
set.add(inner.getIdentifier().simpleName());
}
}
private String annotateReference(final String ref, final Type type, final String annotation) {
- if (type instanceof ParameterizedType) {
- return getReferenceString(annotate(ref, annotation), type,
- ((ParameterizedType) type).getActualTypeArguments());
+ if (type instanceof ParameterizedType parameterized) {
+ return getReferenceString(annotate(ref, annotation), type, parameterized.getActualTypeArguments());
}
return "byte[]".equals(ref) ? "byte @" + annotation + "[]" : annotate(ref, annotation).toString();
}
final String getReferenceString(final Type type) {
final String ref = getReferenceString(type.getIdentifier());
- return type instanceof ParameterizedType ? getReferenceString(new StringBuilder(ref), type,
- ((ParameterizedType) type).getActualTypeArguments())
+ return type instanceof ParameterizedType parameterized
+ ? getReferenceString(new StringBuilder(ref), type, parameterized.getActualTypeArguments())
: ref;
}
@Override
public boolean equals(final Object obj) {
- if (obj == this) {
- return true;
- }
- if (!(obj instanceof BuilderGeneratedProperty)) {
- return false;
- }
- final BuilderGeneratedProperty other = (BuilderGeneratedProperty) obj;
- return name.equals(other.name) && getter.equals(other.getter);
+ return obj == this || obj instanceof BuilderGeneratedProperty other
+ && name.equals(other.name) && getter.equals(other.getter);
}
@Override
*/
@Override
public boolean isAcceptable(final Type type) {
- if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
- for (Type t : ((GeneratedType) type).getImplements()) {
+ if (type instanceof GeneratedType generated && !(type instanceof GeneratedTransferObject)) {
+ for (Type t : generated.getImplements()) {
// "rpc" and "grouping" elements do not implement Augmentable
final JavaTypeName name = t.getIdentifier();
if (name.equals(AUGMENTABLE) || name.equals(AUGMENTATION)) {
*/
@Override
public String generate(final Type type) {
- if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
- return templateForType((GeneratedType) type).generate();
+ if (type instanceof GeneratedType generated && !(type instanceof GeneratedTransferObject)) {
+ return templateForType(generated).generate();
}
return "";
}
private static Type getConcreteType(final Type type) {
if (type instanceof ConcreteType) {
return type;
- } else if (type instanceof ParameterizedType) {
- return ((ParameterizedType) type).getRawType();
- } else if (type instanceof GeneratedTransferObject) {
- GeneratedTransferObject rootGto = (GeneratedTransferObject) type;
+ } else if (type instanceof ParameterizedType generated) {
+ return generated.getRawType();
+ } else if (type instanceof GeneratedTransferObject gto) {
+ GeneratedTransferObject rootGto = gto;
while (rootGto.getSuperType() != null) {
rootGto = rootGto.getSuperType();
}
if (type.equals(BindingTypes.INSTANCE_IDENTIFIER) || type.equals(BindingTypes.KEYED_INSTANCE_IDENTIFIER)) {
return RANK_INSTANCE_IDENTIFIER;
}
- if (type instanceof GeneratedTransferObject) {
- final TypeDefinition<?> typedef = topParentTransportObject((GeneratedTransferObject) type).getBaseType();
+ if (type instanceof GeneratedTransferObject gto) {
+ final TypeDefinition<?> typedef = topParentTransportObject(gto).getBaseType();
if (typedef instanceof BitsTypeDefinition) {
return RANK_VARIABLE_ARRAY;
}
*/
@Override
public String generate(final Type type) {
- if (type instanceof Enumeration) {
- final Enumeration enums = (Enumeration) type;
+ if (type instanceof Enumeration enums) {
final EnumTemplate enumTemplate = new EnumTemplate(enums);
return enumTemplate.generate();
}
}
// REGULAR EXPRESSION
- if (genType instanceof GeneratedTransferObject
- && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, (GeneratedTransferObject) genType)) {
+ if (genType instanceof GeneratedTransferObject gto
+ && isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, gto)) {
putTypeIntoImports(genType, PATTERN, imports);
}
}
// PROPERTIES
- if (genType instanceof GeneratedTransferObject) {
- final GeneratedTransferObject genTO = (GeneratedTransferObject) genType;
- final List<GeneratedProperty> properties = genTO.getProperties();
+ if (genType instanceof GeneratedTransferObject gto) {
+ final List<GeneratedProperty> properties = gto.getProperties();
if (properties != null) {
for (GeneratedProperty property : properties) {
final Type propertyType = property.getReturnType();
if (!imports.containsKey(typeName)) {
imports.put(typeName, type.getIdentifier());
}
- if (type instanceof ParameterizedType) {
- final ParameterizedType paramType = (ParameterizedType) type;
+ if (type instanceof ParameterizedType paramType) {
final Type[] params = paramType.getActualTypeArguments();
if (params != null) {
for (Type param : params) {
*/
private static StringBuilder addActualTypeParameters(final StringBuilder builder, final Type type,
final GeneratedType parentGenType, final Map<String, JavaTypeName> imports) {
- if (type instanceof ParameterizedType) {
- final ParameterizedType pType = (ParameterizedType) type;
+ if (type instanceof ParameterizedType pType) {
final Type[] pTypes = pType.getActualTypeArguments();
builder.append('<').append(getParameters(parentGenType, pTypes, imports)).append('>');
}
public final class InterfaceGenerator implements CodeGenerator {
@Override
- public boolean isAcceptable(Type type) {
+ public boolean isAcceptable(final Type type) {
return type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)
&& !(type instanceof Enumeration);
}
* written in XTEND language.
*/
@Override
- public String generate(Type type) {
- if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
- final GeneratedType genType = (GeneratedType) type;
+ public String generate(final Type type) {
+ if (type instanceof GeneratedType genType && !(type instanceof GeneratedTransferObject)) {
final InterfaceTemplate interfaceTemplate = new InterfaceTemplate(genType);
return interfaceTemplate.generate();
}
}
@Override
- public String getUnitName(Type type) {
+ public String getUnitName(final Type type) {
return type.getName();
}
-
}
}
final @NonNull String importedJavadocName(final @NonNull Type intype) {
- return importedName(intype instanceof ParameterizedType ? ((ParameterizedType) intype).getRawType() : intype);
+ return importedName(intype instanceof ParameterizedType parameterized ? parameterized.getRawType() : intype);
}
final @NonNull String importedName(final @NonNull Type intype) {
}
final CharSequence generateInnerClass(final GeneratedType innerClass) {
- if (!(innerClass instanceof GeneratedTransferObject)) {
+ if (!(innerClass instanceof GeneratedTransferObject gto)) {
return "";
}
- final GeneratedTransferObject gto = (GeneratedTransferObject) innerClass;
final NestedJavaGeneratedType innerJavaType = javaType.getEnclosedType(innerClass.getIdentifier());
return gto.isUnionType() ? new UnionTemplate(innerJavaType, gto).generateAsInnerClass()
: new ClassTemplate(innerJavaType, gto).generateAsInnerClass();
ParameterizedType augmentType = null;
for (Type implementedIfc : implementedIfcs) {
- if (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject)) {
- final GeneratedType ifc = (GeneratedType) implementedIfc;
+ if (implementedIfc instanceof GeneratedType ifc && !(implementedIfc instanceof GeneratedTransferObject)) {
addImplMethods(methods, ifc);
final ParameterizedType t = collectImplementedMethods(type, methods, ifc.getImplements());
type.getYangSourceDefinition().ifPresent(def -> {
sb.append('\n');
- if (def instanceof Single) {
- final DocumentedNode node = ((Single) def).getNode();
+ if (def instanceof Single single) {
+ final DocumentedNode node = single.getNode();
sb.append("<p>\n")
.append("This class represents the following YANG schema fragment defined in module <b>")
appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
sb.append("</pre>");
- if (node instanceof SchemaNode) {
- final SchemaNode schema = (SchemaNode) node;
+ if (node instanceof SchemaNode schema) {
// sb.append("The schema path to identify an instance is\n");
// appendPath(sb.append("<i>"), def.getModule(), schema.getPath().getPathFromRoot());
// sb.append("</i>\n");
}
}
}
- } else if (def instanceof Multiple) {
+ } else if (def instanceof Multiple multiple) {
sb.append("<pre>\n");
- for (SchemaNode node : ((Multiple) def).getNodes()) {
+ for (SchemaNode node : multiple.getNodes()) {
appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
}
sb.append("</pre>\n");
*/
@Override
public String generate(final Type type) {
- if (type instanceof GeneratedTransferObject) {
- final GeneratedTransferObject genTO = (GeneratedTransferObject) type;
+ if (type instanceof GeneratedTransferObject genTO) {
if (genTO.isUnionType()) {
final UnionTemplate template = new UnionTemplate(genTO);
return template.generate();
*/
static ConcreteType getBaseYangType(final @NonNull Type type) {
// Already the correct type
- if (type instanceof ConcreteType) {
- return (ConcreteType) type;
+ if (type instanceof ConcreteType concrete) {
+ return concrete;
}
checkArgument(type instanceof GeneratedTransferObject, "Unsupported type %s", type);