Migrate TyperUtils.getTableSchema() users
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / RealBaseType.java
index a3f601834ae588ec21b3858a286b881c19841856..569aaff3f11a3817dcbd7a07798103937803b766 100644 (file)
@@ -8,29 +8,21 @@
 package org.opendaylight.ovsdb.lib.schema;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import java.util.HashSet;
-import java.util.Optional;
+import com.google.common.collect.ImmutableSet;
 import java.util.Set;
 
 final class RealBaseType extends BaseType<RealBaseType> {
-    private double min = Double.MIN_VALUE;
-    private double max = Double.MAX_VALUE;
-    private Set<Double> enums;
+    static final RealBaseType SINGLETON = new RealBaseType(Double.MIN_VALUE, Double.MAX_VALUE, null);
+    static final BaseTypeFactory<RealBaseType> FACTORY = new Factory();
 
-    @Override
-    void fillConstraints(final JsonNode type) {
-        JsonNode typeMaxNode = type.get("maxReal");
-        if (typeMaxNode != null) {
-            setMax(typeMaxNode.asLong());
-        }
-        JsonNode typeMinNode = type.get("minReal");
-        if (typeMinNode != null) {
-            setMin(typeMinNode.asLong());
-        }
-        Optional<Set<Double>> typeEnumsOpt = populateEnum(type);
-        if (typeEnumsOpt.isPresent()) {
-            setEnums(typeEnumsOpt.get());
-        }
+    private final double min;
+    private final double max;
+    private final ImmutableSet<Double> enums;
+
+    RealBaseType(final double min, final double max, final ImmutableSet<Double> enums) {
+        this.min = min;
+        this.max = max;
+        this.enums = enums;
     }
 
     @Override
@@ -43,43 +35,18 @@ final class RealBaseType extends BaseType<RealBaseType> {
 
     }
 
-    private static Optional<Set<Double>> populateEnum(final JsonNode node) {
-        if (node.has("enum")) {
-            Set<Double> nodesEnums = new HashSet<>();
-            JsonNode anEnum = node.get("enum").get(1);
-            for (JsonNode enm : anEnum) {
-                nodesEnums.add(enm.asDouble());
-            }
-            return Optional.of(nodesEnums);
-        } else {
-            return Optional.empty();
-        }
-    }
-
     public double getMin() {
         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";
@@ -125,4 +92,28 @@ final class RealBaseType extends BaseType<RealBaseType> {
         }
         return true;
     }
-}
\ No newline at end of file
+
+    private static final class Factory extends BaseTypeFactory.WithEnum<RealBaseType, Double> {
+        @Override
+        RealBaseType create(final JsonNode typeDefinition) {
+            // FIXME: is asLong() appropriate here?
+            final JsonNode typeMaxNode = typeDefinition.get("maxReal");
+            final double max = typeMaxNode != null ? typeMaxNode.asLong() : Double.MAX_VALUE;
+
+            final JsonNode typeMinNode = typeDefinition.get("minReal");
+            final double min = typeMinNode != null ? typeMinNode.asLong() : Double.MIN_VALUE;
+
+            final JsonNode typeEnumNode = typeDefinition.get("enum");
+            final ImmutableSet<Double> enums = typeEnumNode != null ? parseEnums(typeEnumNode) : null;
+
+            // TODO: improve accuracy here -- requires understanding the FIXME above
+            return typeMinNode == null && typeMaxNode == null && enums == null ? SINGLETON
+                    : new RealBaseType(min, max, enums);
+        }
+
+        @Override
+        Double getEnumValue(final JsonNode jsonEnum) {
+            return jsonEnum.asDouble();
+        }
+    }
+}