Java API implementation for changes in simulator resource model.
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / ArrayProperty.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 public class ArrayProperty extends AttributeProperty {
20
21     private int               mMin;
22     private int               mMax;
23     private boolean           mIsVariableSize;
24     private boolean           mIsUnique;
25     private AttributeProperty mElementProperty;
26     private boolean           mHasRange = false;
27
28     private ArrayProperty(int min, int max, boolean variableSize,
29             boolean unique, AttributeProperty elementProperty) {
30         super(Type.ARRAY);
31         mMin = min;
32         mMax = max;
33         mIsVariableSize = variableSize;
34         mIsUnique = unique;
35         mElementProperty = elementProperty;
36         mHasRange = true;
37     }
38
39     private ArrayProperty(boolean variableSize, boolean unique,
40             AttributeProperty elementProperty) {
41         super(Type.ARRAY);
42         mIsVariableSize = variableSize;
43         mIsUnique = unique;
44         mElementProperty = elementProperty;
45     }
46
47     @Override
48     public boolean isArray() {
49         return true;
50     }
51
52     @Override
53     public ArrayProperty asArray() {
54         return this;
55     }
56
57     public boolean hasRange() {
58         return mHasRange;
59     }
60
61     public int minItems() {
62         return mMin;
63     }
64
65     public int maxItems() {
66         return mMax;
67     }
68
69     public boolean isVariable() {
70         return mIsVariableSize;
71     }
72
73     public boolean isUnique() {
74         return mIsUnique;
75     }
76
77     public AttributeProperty getElementProperty() {
78         return mElementProperty;
79     }
80
81     @Override
82     public boolean validate(AttributeValue value) {
83         return new ArrayValueValidator(this).validate(value);
84     }
85
86     public static class Builder {
87         private int               mMin;
88         private int               mMax;
89         private boolean           mIsVariableSize  = false;
90         private boolean           mIsUnique        = false;
91         private AttributeProperty mElementProperty = null;
92         private boolean           mHasRange        = false;
93
94         public void setRange(int minItems, int maxItems) {
95             mMin = minItems;
96             mMax = maxItems;
97             mHasRange = true;
98         }
99
100         public void setVariableSize(boolean state) {
101             mIsVariableSize = state;
102         }
103
104         public void setUnique(boolean state) {
105             mIsUnique = state;
106         }
107
108         public void setElementProperty(AttributeProperty property) {
109             mElementProperty = property;
110         }
111
112         public ArrayProperty build() {
113             if (null == mElementProperty)
114                 return null;
115
116             if (mHasRange)
117                 return new ArrayProperty(mMin, mMax, mIsVariableSize,
118                         mIsUnique, mElementProperty);
119             return new ArrayProperty(mIsVariableSize, mIsUnique,
120                     mElementProperty);
121         }
122     }
123 }