Imported Upstream version 1.1.0
[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.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import oic.simulator.serviceprovider.model.Resource;
27 import oic.simulator.serviceprovider.model.SingleResource;
28
29 public class Data {
30     private Map<String, SingleResource> singleResourceMap;
31
32     public Data() {
33         singleResourceMap = new HashMap<String, SingleResource>();
34     }
35
36     public synchronized void addResource(Resource resource) {
37         if (null == resource) {
38             return;
39         }
40         if (resource instanceof SingleResource) {
41             singleResourceMap.put(resource.getResourceURI(),
42                     (SingleResource) resource);
43         }
44     }
45
46     public synchronized void deleteResource(Resource resource) {
47         if (null == resource) {
48             return;
49         }
50         if (resource instanceof SingleResource) {
51             singleResourceMap.remove(resource.getResourceURI());
52         }
53     }
54
55     public List<SingleResource> getSingleResources() {
56         List<SingleResource> resources;
57         synchronized (singleResourceMap) {
58             if (singleResourceMap.isEmpty()) {
59                 return null;
60             }
61             resources = new ArrayList<SingleResource>();
62             Set<String> uriSet = singleResourceMap.keySet();
63             Iterator<String> itr = uriSet.iterator();
64             while (itr.hasNext()) {
65                 resources.add(singleResourceMap.get(itr.next()));
66             }
67         }
68         return resources;
69     }
70
71     public List<Resource> getResources() {
72         if (singleResourceMap.isEmpty()) {
73             return null;
74         }
75         List<Resource> resourceList = new ArrayList<Resource>();
76         synchronized (singleResourceMap) {
77             Set<String> uriSet = singleResourceMap.keySet();
78             Iterator<String> itr = uriSet.iterator();
79             while (itr.hasNext()) {
80                 resourceList.add(singleResourceMap.get(itr.next()));
81             }
82         }
83         return resourceList;
84     }
85
86     public synchronized boolean isResourceExist(String resourceURI) {
87         if (null == resourceURI || resourceURI.isEmpty()) {
88             return false;
89         }
90         if ((null != singleResourceMap && singleResourceMap
91                 .containsKey(resourceURI))) {
92             return true;
93         }
94         return false;
95     }
96
97     public synchronized boolean isAnyResourceExist() {
98         return ((null != singleResourceMap && !singleResourceMap.isEmpty()));
99     }
100
101     public void changeResourceURI(Resource resource, String curURI,
102             String newURI) {
103         if (null == resource || null == curURI || null == newURI) {
104             return;
105         }
106         if (resource instanceof SingleResource) {
107             synchronized (singleResourceMap) {
108                 singleResourceMap.remove(curURI);
109                 singleResourceMap.put(newURI, (SingleResource) resource);
110             }
111         }
112         resource.setResourceURI(newURI);
113     }
114
115     public SingleResource getSingleResourceByURI(String resourceURI) {
116         if (null == resourceURI) {
117             return null;
118         }
119         SingleResource res;
120         synchronized (singleResourceMap) {
121             res = singleResourceMap.get(resourceURI);
122         }
123         return res;
124     }
125
126     public Resource getResourceByURI(String resourceURI) {
127         Resource res = getSingleResourceByURI(resourceURI);
128         return res;
129     }
130
131     public synchronized int getResourceCount() {
132         return singleResourceMap.size();
133     }
134 }