2cd2eee7dd008e08f53a649dca1ea2f6deeb88c5
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / Data.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 oic.simulator.serviceprovider.manager;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import oic.simulator.serviceprovider.model.Resource;
28 import oic.simulator.serviceprovider.model.SingleResource;
29
30 public class Data {
31     private Map<String, SingleResource> singleResourceMap;
32
33     // Holds resource type as key and a resource URI set as values.
34     // Helps in performing operations based on resource type.
35     private Map<String, Set<String>>    resTypeToResUriMap;
36
37     public Data() {
38         singleResourceMap = new HashMap<String, SingleResource>();
39         resTypeToResUriMap = new HashMap<String, Set<String>>();
40     }
41
42     public synchronized void addResource(Resource resource) {
43         if (null == resource) {
44             return;
45         }
46         if (resource instanceof SingleResource) {
47             singleResourceMap.put(resource.getResourceURI(),
48                     (SingleResource) resource);
49         }
50         addToTypeAndUriMap(resource);
51     }
52
53     public synchronized void addToTypeAndUriMap(Resource resource) {
54         if (null == resource) {
55             return;
56         }
57         String resType = resource.getResourceType();
58         if (null == resType || resType.isEmpty()) {
59             return;
60         }
61         String uri = resource.getResourceURI();
62         if (null == uri || uri.isEmpty()) {
63             return;
64         }
65         Set<String> newTypeSet;
66         newTypeSet = resTypeToResUriMap.get(resType);
67         if (null == newTypeSet) {
68             newTypeSet = new HashSet<String>();
69             resTypeToResUriMap.put(resType, newTypeSet);
70         }
71         newTypeSet.add(uri);
72     }
73
74     public synchronized void deleteResource(Resource resource) {
75         if (null == resource) {
76             return;
77         }
78         if (resource instanceof SingleResource) {
79             singleResourceMap.remove(resource.getResourceURI());
80         }
81         removeFromTypeAndUriMap(resource);
82     }
83
84     public synchronized void removeFromTypeAndUriMap(Resource resource) {
85         if (null == resource) {
86             return;
87         }
88         String resType = resource.getResourceType();
89         if (null == resType || !resType.isEmpty()) {
90             return;
91         }
92         String uri = resource.getResourceURI();
93         if (null == uri || uri.isEmpty()) {
94             return;
95         }
96         Set<String> newTypeSet;
97         newTypeSet = resTypeToResUriMap.get(resType);
98         if (null != newTypeSet) {
99             newTypeSet.remove(uri);
100         }
101         if (null == newTypeSet || newTypeSet.isEmpty()) {
102             resTypeToResUriMap.remove(resType);
103         }
104     }
105
106     public List<SingleResource> getSingleResources() {
107         List<SingleResource> resources;
108         synchronized (singleResourceMap) {
109             if (singleResourceMap.isEmpty()) {
110                 return null;
111             }
112             resources = new ArrayList<SingleResource>();
113             Set<String> uriSet = singleResourceMap.keySet();
114             Iterator<String> itr = uriSet.iterator();
115             while (itr.hasNext()) {
116                 resources.add(singleResourceMap.get(itr.next()));
117             }
118         }
119         return resources;
120     }
121
122     public List<Resource> getResources() {
123         if (singleResourceMap.isEmpty()) {
124             return null;
125         }
126         List<Resource> resourceList = new ArrayList<Resource>();
127         synchronized (singleResourceMap) {
128             Set<String> uriSet = singleResourceMap.keySet();
129             Iterator<String> itr = uriSet.iterator();
130             while (itr.hasNext()) {
131                 resourceList.add(singleResourceMap.get(itr.next()));
132             }
133         }
134         return resourceList;
135     }
136
137     public synchronized boolean isResourceExist(String resourceURI) {
138         if (null == resourceURI || resourceURI.isEmpty()) {
139             return false;
140         }
141         if ((null != singleResourceMap && singleResourceMap
142                 .containsKey(resourceURI))) {
143             return true;
144         }
145         return false;
146     }
147
148     public synchronized boolean isAnyResourceExist() {
149         return ((null != singleResourceMap && !singleResourceMap.isEmpty()));
150     }
151
152     public void changeResourceURI(Resource resource, String curURI,
153             String newURI) {
154         if (null == resource || null == curURI || null == newURI) {
155             return;
156         }
157         if (resource instanceof SingleResource) {
158             synchronized (singleResourceMap) {
159                 singleResourceMap.remove(curURI);
160                 singleResourceMap.put(newURI, (SingleResource) resource);
161             }
162         }
163         resource.setResourceURI(newURI);
164     }
165
166     public SingleResource getSingleResourceByURI(String resourceURI) {
167         if (null == resourceURI) {
168             return null;
169         }
170         SingleResource res;
171         synchronized (singleResourceMap) {
172             res = singleResourceMap.get(resourceURI);
173         }
174         return res;
175     }
176
177     public Resource getResourceByURI(String resourceURI) {
178         Resource res = getSingleResourceByURI(resourceURI);
179         return res;
180     }
181
182     public synchronized int getResourceCount() {
183         return singleResourceMap.size();
184     }
185 }