a3f601834ae588ec21b3858a286b881c19841856
[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     private double min = Double.MIN_VALUE;
17     private double max = Double.MAX_VALUE;
18     private Set<Double> enums;
19
20     @Override
21     void fillConstraints(final JsonNode type) {
22         JsonNode typeMaxNode = type.get("maxReal");
23         if (typeMaxNode != null) {
24             setMax(typeMaxNode.asLong());
25         }
26         JsonNode typeMinNode = type.get("minReal");
27         if (typeMinNode != null) {
28             setMin(typeMinNode.asLong());
29         }
30         Optional<Set<Double>> typeEnumsOpt = populateEnum(type);
31         if (typeEnumsOpt.isPresent()) {
32             setEnums(typeEnumsOpt.get());
33         }
34     }
35
36     @Override
37     public Object toValue(final JsonNode value) {
38         return value.asDouble();
39     }
40
41     @Override
42     public void validate(final Object value) {
43
44     }
45
46     private static Optional<Set<Double>> populateEnum(final JsonNode node) {
47         if (node.has("enum")) {
48             Set<Double> nodesEnums = new HashSet<>();
49             JsonNode anEnum = node.get("enum").get(1);
50             for (JsonNode enm : anEnum) {
51                 nodesEnums.add(enm.asDouble());
52             }
53             return Optional.of(nodesEnums);
54         } else {
55             return Optional.empty();
56         }
57     }
58
59     public double getMin() {
60         return min;
61     }
62
63     public void setMin(final double min) {
64         this.min = min;
65     }
66
67     public double getMax() {
68         return max;
69     }
70
71     public void setMax(final double max) {
72         this.max = max;
73     }
74
75     public Set<Double> getEnums() {
76         return enums;
77     }
78
79     public void setEnums(final Set<Double> enums) {
80         this.enums = enums;
81     }
82
83     @Override
84     public String toString() {
85         return "RealBaseType";
86     }
87
88     @Override
89     public int hashCode() {
90         final int prime = 31;
91         int result = 1;
92         result = prime * result + (enums == null ? 0 : enums.hashCode());
93         long temp;
94         temp = Double.doubleToLongBits(max);
95         result = prime * result + (int) (temp ^ temp >>> 32);
96         temp = Double.doubleToLongBits(min);
97         result = prime * result + (int) (temp ^ temp >>> 32);
98         return result;
99     }
100
101     @Override
102     public boolean equals(final Object obj) {
103         if (this == obj) {
104             return true;
105         }
106         if (obj == null) {
107             return false;
108         }
109         if (getClass() != obj.getClass()) {
110             return false;
111         }
112         RealBaseType other = (RealBaseType) obj;
113         if (enums == null) {
114             if (other.enums != null) {
115                 return false;
116             }
117         } else if (!enums.equals(other.enums)) {
118             return false;
119         }
120         if (Double.doubleToLongBits(max) != Double.doubleToLongBits(other.max)) {
121             return false;
122         }
123         if (Double.doubleToLongBits(min) != Double.doubleToLongBits(other.min)) {
124             return false;
125         }
126         return true;
127     }
128 }