Use singleton BaseType instances for simple definitions
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / RealBaseType.java
1 /*
2  * Copyright © 2014, 2017 EBay Software Foundation and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.ovsdb.lib.schema;
9
10 import com.fasterxml.jackson.databind.JsonNode;
11 import java.util.HashSet;
12 import java.util.Optional;
13 import java.util.Set;
14
15 final class RealBaseType extends BaseType<RealBaseType> {
16     static final RealBaseType SINGLETON = new RealBaseType();
17
18     private double min = Double.MIN_VALUE;
19     private double max = Double.MAX_VALUE;
20     private Set<Double> enums;
21
22     @Override
23     void fillConstraints(final JsonNode type) {
24         JsonNode typeMaxNode = type.get("maxReal");
25         if (typeMaxNode != null) {
26             max = typeMaxNode.asLong();
27         }
28         JsonNode typeMinNode = type.get("minReal");
29         if (typeMinNode != null) {
30             min = typeMinNode.asLong();
31         }
32         Optional<Set<Double>> typeEnumsOpt = populateEnum(type);
33         if (typeEnumsOpt.isPresent()) {
34             enums = typeEnumsOpt.get();
35         }
36     }
37
38     @Override
39     public Object toValue(final JsonNode value) {
40         return value.asDouble();
41     }
42
43     @Override
44     public void validate(final Object value) {
45
46     }
47
48     private static Optional<Set<Double>> populateEnum(final JsonNode node) {
49         if (node.has("enum")) {
50             Set<Double> nodesEnums = new HashSet<>();
51             JsonNode anEnum = node.get("enum").get(1);
52             for (JsonNode enm : anEnum) {
53                 nodesEnums.add(enm.asDouble());
54             }
55             return Optional.of(nodesEnums);
56         } else {
57             return Optional.empty();
58         }
59     }
60
61     public double getMin() {
62         return min;
63     }
64
65     public double getMax() {
66         return max;
67     }
68
69     public Set<Double> getEnums() {
70         return enums;
71     }
72
73     @Override
74     public String toString() {
75         return "RealBaseType";
76     }
77
78     @Override
79     public int hashCode() {
80         final int prime = 31;
81         int result = 1;
82         result = prime * result + (enums == null ? 0 : enums.hashCode());
83         long temp;
84         temp = Double.doubleToLongBits(max);
85         result = prime * result + (int) (temp ^ temp >>> 32);
86         temp = Double.doubleToLongBits(min);
87         result = prime * result + (int) (temp ^ temp >>> 32);
88         return result;
89     }
90
91     @Override
92     public boolean equals(final Object obj) {
93         if (this == obj) {
94             return true;
95         }
96         if (obj == null) {
97             return false;
98         }
99         if (getClass() != obj.getClass()) {
100             return false;
101         }
102         RealBaseType other = (RealBaseType) obj;
103         if (enums == null) {
104             if (other.enums != null) {
105                 return false;
106             }
107         } else if (!enums.equals(other.enums)) {
108             return false;
109         }
110         if (Double.doubleToLongBits(max) != Double.doubleToLongBits(other.max)) {
111             return false;
112         }
113         if (Double.doubleToLongBits(min) != Double.doubleToLongBits(other.min)) {
114             return false;
115         }
116         return true;
117     }
118 }