Use singleton BaseType instances for simple definitions 68/86068/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Nov 2019 14:56:12 +0000 (15:56 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Nov 2019 16:03:57 +0000 (17:03 +0100)
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 <robert.varga@pantheon.tech>
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BaseType.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/BooleanBaseType.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/IntegerBaseType.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/RealBaseType.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/StringBaseType.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/UuidBaseType.java

index 8024a036204f34dec87609f350a075562f536408..3c37aa2a726f7b112fa7e84162de9512125b4516 100644 (file)
@@ -14,9 +14,13 @@ import org.slf4j.Logger;
 public abstract class BaseType<E extends BaseType<E>> {
     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<E extends BaseType<E>> {
         }
         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<E extends BaseType<E>> {
 
     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();
index cef682922f2be5629de72a3b69d99b42018a8219..c9d893a240bf0275864d13271d6b2bf1434d5ea4 100644 (file)
@@ -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) {
index 0152d0a5937572b6511213532f2c291af48af703..de1d874ff4294fd92a68f14b25c169b3137e3a61 100644 (file)
@@ -13,6 +13,8 @@ import java.util.Optional;
 import java.util.Set;
 
 final class IntegerBaseType extends BaseType<IntegerBaseType> {
+    static final IntegerBaseType SINGLETON = new IntegerBaseType();
+
     private long min = Long.MIN_VALUE;
     private long max = Long.MAX_VALUE;
     private Set<Integer> enums;
@@ -21,15 +23,15 @@ final class IntegerBaseType extends BaseType<IntegerBaseType> {
     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<Set<Integer>> typeEnumsOpt = populateEnum(type);
         if (typeEnumsOpt.isPresent()) {
-            setEnums(typeEnumsOpt.get());
+            enums = typeEnumsOpt.get();
         }
     }
 
@@ -60,26 +62,14 @@ final class IntegerBaseType extends BaseType<IntegerBaseType> {
         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<Integer> getEnums() {
         return enums;
     }
 
-    public void setEnums(final Set<Integer> enums) {
-        this.enums = enums;
-    }
-
     @Override
     public String toString() {
         return "IntegerBaseType";
index a3f601834ae588ec21b3858a286b881c19841856..199a8fd54906134f21101190c632e0cd1af02298 100644 (file)
@@ -13,6 +13,8 @@ import java.util.Optional;
 import java.util.Set;
 
 final class RealBaseType extends BaseType<RealBaseType> {
+    static final RealBaseType SINGLETON = new RealBaseType();
+
     private double min = Double.MIN_VALUE;
     private double max = Double.MAX_VALUE;
     private Set<Double> enums;
@@ -21,15 +23,15 @@ final class RealBaseType extends BaseType<RealBaseType> {
     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<Set<Double>> typeEnumsOpt = populateEnum(type);
         if (typeEnumsOpt.isPresent()) {
-            setEnums(typeEnumsOpt.get());
+            enums = typeEnumsOpt.get();
         }
     }
 
@@ -60,26 +62,14 @@ final class RealBaseType extends BaseType<RealBaseType> {
         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<Double> getEnums() {
         return enums;
     }
 
-    public void setEnums(final Set<Double> enums) {
-        this.enums = enums;
-    }
-
     @Override
     public String toString() {
         return "RealBaseType";
index 7e1e77dfffa9baaed51b2e307cd21f4360c44edf..77680c189364a8f9c9ad28c40ba59ad1635d0f42 100644 (file)
@@ -13,6 +13,8 @@ import java.util.Optional;
 import java.util.Set;
 
 final class StringBaseType extends BaseType<StringBaseType> {
+    static final StringBaseType SINGLETON = new StringBaseType();
+
     private int minLength = Integer.MIN_VALUE;
     private int maxLength = Integer.MAX_VALUE;
     private Set<String> enums;
@@ -21,15 +23,15 @@ final class StringBaseType extends BaseType<StringBaseType> {
     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<Set<String>> typeEnumsOpt = populateEnum(type);
         if (typeEnumsOpt.isPresent()) {
-            setEnums(typeEnumsOpt.get());
+            enums = typeEnumsOpt.get();
         }
     }
 
@@ -65,26 +67,14 @@ final class StringBaseType extends BaseType<StringBaseType> {
         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<String> getEnums() {
         return enums;
     }
 
-    public void setEnums(final Set<String> enums) {
-        this.enums = enums;
-    }
-
     @Override
     public String toString() {
         return "StringBaseType";
index ea63f412df02fd9c498f5f6b63920fb9a0a7c6a1..15efee38000768327c23784adc777c6eaae87286 100644 (file)
@@ -25,6 +25,9 @@ import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.operations.Insert;
 
 public abstract class TableSchema<E extends TableSchema<E>> {
+    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<String, ColumnSchema> columns;
@@ -161,7 +164,7 @@ public abstract class TableSchema<E extends TableSchema<E>> {
      * 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);
     }
 }
index 7a50b76b440c4dbd766c209c2393821a7d2bed19..5e88e30d652077b646cbe9d4e944b19575eb20d3 100644 (file)
@@ -15,16 +15,18 @@ final class UuidBaseType extends BaseType<UuidBaseType> {
     // 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<UuidBaseType> {
         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";