0152d0a5937572b6511213532f2c291af48af703
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / schema / IntegerBaseType.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 IntegerBaseType extends BaseType<IntegerBaseType> {
16     private long min = Long.MIN_VALUE;
17     private long max = Long.MAX_VALUE;
18     private Set<Integer> enums;
19
20     @Override
21     void fillConstraints(final JsonNode type) {
22         JsonNode typeMaxNode = type.get("maxInteger");
23         if (typeMaxNode != null) {
24             setMax(typeMaxNode.asLong());
25         }
26         JsonNode typeMinNode = type.get("minInteger");
27         if (typeMinNode != null) {
28             setMin(typeMinNode.asLong());
29         }
30         Optional<Set<Integer>> 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.asLong();
39     }
40
41     @Override
42     public void validate(final Object value) {
43
44     }
45
46     private static Optional<Set<Integer>> populateEnum(final JsonNode node) {
47         if (node.has("enum")) {
48             Set<Integer> nodesEnums = new HashSet<>();
49             JsonNode anEnum = node.get("enum").get(1);
50             for (JsonNode enm : anEnum) {
51                 nodesEnums.add(enm.asInt());
52             }
53             return Optional.of(nodesEnums);
54         } else {
55             return Optional.empty();
56         }
57     }
58
59     public long getMin() {
60         return min;
61     }
62
63     public void setMin(final long min) {
64         this.min = min;
65     }
66
67     public long getMax() {
68         return max;
69     }
70
71     public void setMax(final long max) {
72         this.max = max;
73     }
74
75     public Set<Integer> getEnums() {
76         return enums;
77     }
78
79     public void setEnums(final Set<Integer> enums) {
80         this.enums = enums;
81     }
82
83     @Override
84     public String toString() {
85         return "IntegerBaseType";
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         result = prime * result + (int) (max ^ max >>> 32);
94         result = prime * result + (int) (min ^ min >>> 32);
95         return result;
96     }
97
98     @Override
99     public boolean equals(final Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (obj == null) {
104             return false;
105         }
106         if (getClass() != obj.getClass()) {
107             return false;
108         }
109         IntegerBaseType other = (IntegerBaseType) obj;
110         if (enums == null) {
111             if (other.enums != null) {
112                 return false;
113             }
114         } else if (!enums.equals(other.enums)) {
115             return false;
116         }
117         if (max != other.max) {
118             return false;
119         }
120         if (min != other.min) {
121             return false;
122         }
123         return true;
124     }
125 }