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);
}
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;
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();
import com.fasterxml.jackson.databind.JsonNode;
final class BooleanBaseType extends BaseType {
+ static final BooleanBaseType SINGLETON = new BooleanBaseType();
@Override
void fillConstraints(final JsonNode node) {
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;
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();
}
}
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";
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;
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();
}
}
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";
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;
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();
}
}
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";
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;
* 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);
}
}
// 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
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";