From a0a58e9b8e707c05d2e4e827e378e24c132868d7 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 29 Nov 2019 15:56:12 +0100 Subject: [PATCH] Use singleton BaseType instances for simple definitions We have two distinct cases for a type definition. It is either a simple reference without any further constraints or it is customized in some way. This patch separates the two codepaths, making the former use singleton instances. Change-Id: Ic9e63f2320a429bda076e4ae25eea97e5242a52c Signed-off-by: Robert Varga --- .../ovsdb/lib/schema/BaseType.java | 32 ++++++++++++++++--- .../ovsdb/lib/schema/BooleanBaseType.java | 1 + .../ovsdb/lib/schema/IntegerBaseType.java | 20 +++--------- .../ovsdb/lib/schema/RealBaseType.java | 20 +++--------- .../ovsdb/lib/schema/StringBaseType.java | 20 +++--------- .../ovsdb/lib/schema/TableSchema.java | 7 ++-- .../ovsdb/lib/schema/UuidBaseType.java | 14 +++----- 7 files changed, 53 insertions(+), 61 deletions(-) diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BaseType.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BaseType.java index 8024a0362..3c37aa2a7 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BaseType.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BaseType.java @@ -14,9 +14,13 @@ import org.slf4j.Logger; public abstract class BaseType> { private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(BaseType.class); + BaseType() { + // Prevent external instantiation + } + public static BaseType fromJson(final JsonNode json, final String keyorval) { if (json.isValueNode()) { - return fromString(json.asText().trim()); + return singletonFor(json.asText().trim()); } final JsonNode type = json.get(keyorval); @@ -25,13 +29,13 @@ public abstract class BaseType> { } if (type.isTextual()) { //json like "string" - return fromString(type.asText()); + return singletonFor(type.asText()); } if (type.isObject()) { //json like {"type" : "string", "enum": ["set", ["access", "native-tagged"]]}" for key or value final JsonNode nestedType = type.get("type"); if (nestedType != null) { - final BaseType ret = fromString(nestedType.asText()); + final BaseType ret = builderFor(nestedType.asText()); if (ret != null) { ret.fillConstraints(type); return ret; @@ -48,7 +52,27 @@ public abstract class BaseType> { public abstract void validate(Object value); - private static BaseType fromString(final String type) { + // Find a simple singleton instance + private static BaseType singletonFor(final String type) { + switch (type) { + case "boolean": + return BooleanBaseType.SINGLETON; + case "integer": + return IntegerBaseType.SINGLETON; + case "real": + return RealBaseType.SINGLETON; + case "string": + return StringBaseType.SINGLETON; + case "uuid": + return UuidBaseType.SINGLETON; + default: + LOG.debug("Unknown base type {}", type); + return null; + } + } + + // Create a new instance for customization + private static BaseType builderFor(final String type) { switch (type) { case "boolean": return new BooleanBaseType(); diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BooleanBaseType.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BooleanBaseType.java index cef682922..c9d893a24 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BooleanBaseType.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BooleanBaseType.java @@ -10,6 +10,7 @@ package org.opendaylight.ovsdb.lib.schema; import com.fasterxml.jackson.databind.JsonNode; final class BooleanBaseType extends BaseType { + static final BooleanBaseType SINGLETON = new BooleanBaseType(); @Override void fillConstraints(final JsonNode node) { diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/IntegerBaseType.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/IntegerBaseType.java index 0152d0a59..de1d874ff 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/IntegerBaseType.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/IntegerBaseType.java @@ -13,6 +13,8 @@ import java.util.Optional; import java.util.Set; final class IntegerBaseType extends BaseType { + static final IntegerBaseType SINGLETON = new IntegerBaseType(); + private long min = Long.MIN_VALUE; private long max = Long.MAX_VALUE; private Set enums; @@ -21,15 +23,15 @@ final class IntegerBaseType extends BaseType { void fillConstraints(final JsonNode type) { JsonNode typeMaxNode = type.get("maxInteger"); if (typeMaxNode != null) { - setMax(typeMaxNode.asLong()); + max = typeMaxNode.asLong(); } JsonNode typeMinNode = type.get("minInteger"); if (typeMinNode != null) { - setMin(typeMinNode.asLong()); + min = typeMinNode.asLong(); } Optional> typeEnumsOpt = populateEnum(type); if (typeEnumsOpt.isPresent()) { - setEnums(typeEnumsOpt.get()); + enums = typeEnumsOpt.get(); } } @@ -60,26 +62,14 @@ final class IntegerBaseType extends BaseType { return min; } - public void setMin(final long min) { - this.min = min; - } - public long getMax() { return max; } - public void setMax(final long max) { - this.max = max; - } - public Set getEnums() { return enums; } - public void setEnums(final Set enums) { - this.enums = enums; - } - @Override public String toString() { return "IntegerBaseType"; diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/RealBaseType.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/RealBaseType.java index a3f601834..199a8fd54 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/RealBaseType.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/RealBaseType.java @@ -13,6 +13,8 @@ import java.util.Optional; import java.util.Set; final class RealBaseType extends BaseType { + static final RealBaseType SINGLETON = new RealBaseType(); + private double min = Double.MIN_VALUE; private double max = Double.MAX_VALUE; private Set enums; @@ -21,15 +23,15 @@ final class RealBaseType extends BaseType { void fillConstraints(final JsonNode type) { JsonNode typeMaxNode = type.get("maxReal"); if (typeMaxNode != null) { - setMax(typeMaxNode.asLong()); + max = typeMaxNode.asLong(); } JsonNode typeMinNode = type.get("minReal"); if (typeMinNode != null) { - setMin(typeMinNode.asLong()); + min = typeMinNode.asLong(); } Optional> typeEnumsOpt = populateEnum(type); if (typeEnumsOpt.isPresent()) { - setEnums(typeEnumsOpt.get()); + enums = typeEnumsOpt.get(); } } @@ -60,26 +62,14 @@ final class RealBaseType extends BaseType { return min; } - public void setMin(final double min) { - this.min = min; - } - public double getMax() { return max; } - public void setMax(final double max) { - this.max = max; - } - public Set getEnums() { return enums; } - public void setEnums(final Set enums) { - this.enums = enums; - } - @Override public String toString() { return "RealBaseType"; diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/StringBaseType.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/StringBaseType.java index 7e1e77dff..77680c189 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/StringBaseType.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/StringBaseType.java @@ -13,6 +13,8 @@ import java.util.Optional; import java.util.Set; final class StringBaseType extends BaseType { + static final StringBaseType SINGLETON = new StringBaseType(); + private int minLength = Integer.MIN_VALUE; private int maxLength = Integer.MAX_VALUE; private Set enums; @@ -21,15 +23,15 @@ final class StringBaseType extends BaseType { void fillConstraints(final JsonNode type) { JsonNode typeMaxNode = type.get("maxLength"); if (typeMaxNode != null) { - setMaxLength(typeMaxNode.asInt()); + maxLength = typeMaxNode.asInt(); } JsonNode typeMinNode = type.get("minLength"); if (typeMinNode != null) { - setMinLength(typeMinNode.asInt()); + minLength = typeMinNode.asInt(); } Optional> typeEnumsOpt = populateEnum(type); if (typeEnumsOpt.isPresent()) { - setEnums(typeEnumsOpt.get()); + enums = typeEnumsOpt.get(); } } @@ -65,26 +67,14 @@ final class StringBaseType extends BaseType { return minLength; } - public void setMinLength(final int minLength) { - this.minLength = minLength; - } - public int getMaxLength() { return maxLength; } - public void setMaxLength(final int maxLength) { - this.maxLength = maxLength; - } - public Set getEnums() { return enums; } - public void setEnums(final Set enums) { - this.enums = enums; - } - @Override public String toString() { return "StringBaseType"; diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java index ea63f412d..15efee380 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java @@ -25,6 +25,9 @@ import org.opendaylight.ovsdb.lib.notation.UUID; import org.opendaylight.ovsdb.lib.operations.Insert; public abstract class TableSchema> { + private static final AtomicColumnType UUID_COLUMN_TYPE = new AtomicColumnType(UuidBaseType.SINGLETON); + private static final ColumnSchema UUID_COLUMN_SCHMEMA = new ColumnSchema("_uuid", UUID_COLUMN_TYPE); + private static final ColumnSchema VERSION_COLUMN_SCHMEMA = new ColumnSchema("_version", UUID_COLUMN_TYPE); private final String name; private final Map columns; @@ -161,7 +164,7 @@ public abstract class TableSchema> { * for better application experience using the library. */ public void populateInternallyGeneratedColumns() { - columns.put("_uuid", new ColumnSchema("_uuid", new AtomicColumnType(new UuidBaseType()))); - columns.put("_version", new ColumnSchema("_version", new AtomicColumnType(new UuidBaseType()))); + columns.put("_uuid", UUID_COLUMN_SCHMEMA); + columns.put("_version", VERSION_COLUMN_SCHMEMA); } } diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/UuidBaseType.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/UuidBaseType.java index 7a50b76b4..5e88e30d6 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/UuidBaseType.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/UuidBaseType.java @@ -15,16 +15,18 @@ final class UuidBaseType extends BaseType { // These enum types correspond to JSON values and need to be in lower-case currently public enum RefType { strong, weak } + static final UuidBaseType SINGLETON = new UuidBaseType(); + private String refTable; private UuidBaseType.RefType refType; @Override void fillConstraints(final JsonNode node) { JsonNode refTableNode = node.get("refTable"); - setRefTable(refTableNode != null ? refTableNode.asText() : null); + refTable = refTableNode != null ? refTableNode.asText() : null; JsonNode refTypeJson = node.get("refType"); - setRefType(refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong); + refType = refTypeJson != null ? RefType.valueOf(refTypeJson.asText()) : RefType.strong; } @Override @@ -54,18 +56,10 @@ final class UuidBaseType extends BaseType { return refTable; } - public void setRefTable(final String refTable) { - this.refTable = refTable; - } - public UuidBaseType.RefType getRefType() { return refType; } - public void setRefType(final UuidBaseType.RefType refType) { - this.refType = refType; - } - @Override public String toString() { return "UuidBaseType"; -- 2.36.6