Fixed minor issues found during integration testing.
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / StringProperty.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 StringProperty extends AttributeProperty {
22
23     private String   mDefaultValue;
24     private int      mMin;
25     private int      mMax;
26     private String[] mValues;
27     private boolean  mHasRange = false;
28
29     private StringProperty(String value) {
30         super(Type.STRING);
31         mDefaultValue = value;
32     }
33
34     private StringProperty(String value, int min, int max) {
35         super(Type.STRING);
36         mDefaultValue = value;
37         mMin = min;
38         mMax = max;
39         mHasRange = true;
40     }
41
42     private StringProperty(String value, String[] values) {
43         super(Type.STRING);
44         mDefaultValue = value;
45         mValues = Arrays.copyOf(values, values.length);
46         Arrays.sort(mValues);
47     }
48
49     @Override
50     public boolean isString() {
51         return true;
52     }
53
54     @Override
55     public StringProperty asString() {
56         return this;
57     }
58
59     public String getDefaultValue() {
60         return mDefaultValue;
61     }
62
63     public boolean hasRange() {
64         return mHasRange;
65     }
66
67     public int min() {
68         return mMin;
69     }
70
71     public int max() {
72         return mMax;
73     }
74
75     public boolean hasValues() {
76         return (null != mValues && (mValues.length > 0));
77     }
78
79     public String[] getValues() {
80         return mValues;
81     }
82
83     @Override
84     public boolean validate(AttributeValue value) {
85         return new StringValueValidator(this).validate(value);
86     }
87
88     public boolean validate(String value) {
89         int length = value.length();
90         if (mHasRange && (length < mMin || length > mMax)) {
91             return false;
92         } else if (hasValues() && 0 > Arrays.binarySearch(mValues, value)) {
93             return false;
94         }
95
96         return true;
97     }
98
99     public static class Builder {
100         private String   mDefaultValue = new String();
101         private int      mMin;
102         private int      mMax;
103         private String[] mValues;
104         private boolean  mHasRange     = false;
105
106         public void setDefaultValue(String value) {
107             if (null != value)
108                 mDefaultValue = value;
109         }
110
111         public void setRange(int min, int max) {
112             mMin = min;
113             mMax = max;
114             mHasRange = true;
115         }
116
117         public void setValues(String[] values) {
118             mValues = values;
119         }
120
121         public StringProperty build() {
122             if (mHasRange)
123                 return new StringProperty(mDefaultValue, mMin, mMax);
124             if (null != mValues && (0 != mValues.length))
125                 return new StringProperty(mDefaultValue, mValues);
126             return new StringProperty(mDefaultValue);
127         }
128     }
129 }