Merge branch 'upstream' into tizen
[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 (hasValues() && 0 > Arrays.binarySearch(mValues, value)) {
91             return false;
92         }
93
94         return true;
95     }
96
97     public static class Builder {
98         private int     mDefaultValue = 0;
99         private int     mMin;
100         private int     mMax;
101         private int[]   mValues;
102         private boolean mHasRange     = false;
103
104         public void setDefaultValue(int value) {
105             mDefaultValue = value;
106         }
107
108         public void setRange(int min, int max) {
109             mMin = min;
110             mMax = max;
111             mHasRange = true;
112         }
113
114         public void setValues(int[] values) {
115             mValues = values;
116         }
117
118         public IntegerProperty build() {
119             if (mHasRange)
120                 return new IntegerProperty(mDefaultValue, mMin, mMax);
121             if (null != mValues && (0 != mValues.length))
122                 return new IntegerProperty(mDefaultValue, mValues);
123             return new IntegerProperty(mDefaultValue);
124         }
125     }
126 }