0220718fa1f5956291a40eca4e8a2f451620a194
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / IntegerProperty.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.oic.simulator;
18
19 import java.util.Arrays;
20
21 public class IntegerProperty extends AttributeProperty {
22     private int     mDefaultValue;
23     private int     mMin;
24     private int     mMax;
25     private int[]   mValues;
26     private boolean mHasRange = false;
27
28     private IntegerProperty(int value) {
29         super(Type.INTEGER);
30         mDefaultValue = value;
31     }
32
33     private IntegerProperty(int value, int min, int max) {
34         super(Type.INTEGER);
35         mDefaultValue = value;
36         mMin = min;
37         mMax = max;
38         mHasRange = true;
39     }
40
41     private IntegerProperty(int value, int[] values) {
42         super(Type.INTEGER);
43         mDefaultValue = value;
44         mValues = Arrays.copyOf(values, values.length);
45         Arrays.sort(mValues);
46     }
47
48     @Override
49     public boolean isInteger() {
50         return true;
51     }
52
53     @Override
54     public IntegerProperty asInteger() {
55         return this;
56     }
57
58     public int getDefaultValue() {
59         return mDefaultValue;
60     }
61
62     public boolean hasRange() {
63         return mHasRange;
64     }
65
66     public int min() {
67         return mMin;
68     }
69
70     public int max() {
71         return mMax;
72     }
73
74     public boolean hasValues() {
75         return (null != mValues && (mValues.length > 0));
76     }
77
78     public int[] getValues() {
79         return mValues;
80     }
81
82     @Override
83     public boolean validate(AttributeValue value) {
84         return new IntegerValueValidator(this).validate(value);
85     }
86
87     public boolean validate(int value) {
88         if (mHasRange && (value < mMin || value > mMax)) {
89             return false;
90         } else if (mValues.length > 0
91                 && -1 == Arrays.binarySearch(mValues, value)) {
92             return false;
93         }
94
95         return true;
96     }
97
98     public static class Builder {
99         private int     mDefaultValue = 0;
100         private int     mMin;
101         private int     mMax;
102         private int[]   mValues;
103         private boolean mHasRange     = false;
104
105         public void setDefaultValue(int value) {
106             mDefaultValue = value;
107         }
108
109         public void setRange(int min, int max) {
110             mMin = min;
111             mMax = max;
112             mHasRange = true;
113         }
114
115         public void setValues(int[] values) {
116             mValues = values;
117         }
118
119         public IntegerProperty build() {
120             if (mHasRange)
121                 return new IntegerProperty(mDefaultValue, mMin, mMax);
122             if (null != mValues && (0 != mValues.length))
123                 return new IntegerProperty(mDefaultValue, mValues);
124             return new IntegerProperty(mDefaultValue);
125         }
126     }
127 }