Use singleton BaseType instances for simple definitions
[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     static final IntegerBaseType SINGLETON = new IntegerBaseType();
17
18     private long min = Long.MIN_VALUE;
19     private long max = Long.MAX_VALUE;
20     private Set<Integer> enums;
21
22     @Override
23     void fillConstraints(final JsonNode type) {
24         JsonNode typeMaxNode = type.get("maxInteger");
25         if (typeMaxNode != null) {
26             max = typeMaxNode.asLong();
27         }
28         JsonNode typeMinNode = type.get("minInteger");
29         if (typeMinNode != null) {
30             min = typeMinNode.asLong();
31         }
32         Optional<Set<Integer>> 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.asLong();
41     }
42
43     @Override
44     public void validate(final Object value) {
45
46     }
47
48     private static Optional<Set<Integer>> populateEnum(final JsonNode node) {
49         if (node.has("enum")) {
50             Set<Integer> nodesEnums = new HashSet<>();
51             JsonNode anEnum = node.get("enum").get(1);
52             for (JsonNode enm : anEnum) {
53                 nodesEnums.add(enm.asInt());
54             }
55             return Optional.of(nodesEnums);
56         } else {
57             return Optional.empty();
58         }
59     }
60
61     public long getMin() {
62         return min;
63     }
64
65     public long getMax() {
66         return max;
67     }
68
69     public Set<Integer> getEnums() {
70         return enums;
71     }
72
73     @Override
74     public String toString() {
75         return "IntegerBaseType";
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         result = prime * result + (int) (max ^ max >>> 32);
84         result = prime * result + (int) (min ^ min >>> 32);
85         return result;
86     }
87
88     @Override
89     public boolean equals(final Object obj) {
90         if (this == obj) {
91             return true;
92         }
93         if (obj == null) {
94             return false;
95         }
96         if (getClass() != obj.getClass()) {
97             return false;
98         }
99         IntegerBaseType other = (IntegerBaseType) obj;
100         if (enums == null) {
101             if (other.enums != null) {
102                 return false;
103             }
104         } else if (!enums.equals(other.enums)) {
105             return false;
106         }
107         if (max != other.max) {
108             return false;
109         }
110         if (min != other.min) {
111             return false;
112         }
113         return true;
114     }
115 }