2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.manager;
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;
27 import oic.simulator.serviceprovider.model.Resource;
28 import oic.simulator.serviceprovider.model.SingleResource;
31 private Map<String, SingleResource> singleResourceMap;
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;
38 singleResourceMap = new HashMap<String, SingleResource>();
39 resTypeToResUriMap = new HashMap<String, Set<String>>();
42 public synchronized void addResource(Resource resource) {
43 if (null == resource) {
46 if (resource instanceof SingleResource) {
47 singleResourceMap.put(resource.getResourceURI(),
48 (SingleResource) resource);
50 addToTypeAndUriMap(resource);
53 public synchronized void addToTypeAndUriMap(Resource resource) {
54 if (null == resource) {
57 String resType = resource.getResourceType();
58 if (null == resType || resType.isEmpty()) {
61 String uri = resource.getResourceURI();
62 if (null == uri || uri.isEmpty()) {
65 Set<String> newTypeSet;
66 newTypeSet = resTypeToResUriMap.get(resType);
67 if (null == newTypeSet) {
68 newTypeSet = new HashSet<String>();
69 resTypeToResUriMap.put(resType, newTypeSet);
74 public synchronized void deleteResource(Resource resource) {
75 if (null == resource) {
78 if (resource instanceof SingleResource) {
79 singleResourceMap.remove(resource.getResourceURI());
81 removeFromTypeAndUriMap(resource);
84 public synchronized void removeFromTypeAndUriMap(Resource resource) {
85 if (null == resource) {
88 String resType = resource.getResourceType();
89 if (null == resType || !resType.isEmpty()) {
92 String uri = resource.getResourceURI();
93 if (null == uri || uri.isEmpty()) {
96 Set<String> newTypeSet;
97 newTypeSet = resTypeToResUriMap.get(resType);
98 if (null != newTypeSet) {
99 newTypeSet.remove(uri);
101 if (null == newTypeSet || newTypeSet.isEmpty()) {
102 resTypeToResUriMap.remove(resType);
106 public List<SingleResource> getSingleResources() {
107 List<SingleResource> resources;
108 synchronized (singleResourceMap) {
109 if (singleResourceMap.isEmpty()) {
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()));
122 public List<Resource> getResources() {
123 if (singleResourceMap.isEmpty()) {
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()));
137 public synchronized boolean isResourceExist(String resourceURI) {
138 if (null == resourceURI || resourceURI.isEmpty()) {
141 if ((null != singleResourceMap && singleResourceMap
142 .containsKey(resourceURI))) {
148 public synchronized boolean isAnyResourceExist() {
149 return ((null != singleResourceMap && !singleResourceMap.isEmpty()));
152 public void changeResourceURI(Resource resource, String curURI,
154 if (null == resource || null == curURI || null == newURI) {
157 if (resource instanceof SingleResource) {
158 synchronized (singleResourceMap) {
159 singleResourceMap.remove(curURI);
160 singleResourceMap.put(newURI, (SingleResource) resource);
163 resource.setResourceURI(newURI);
166 public SingleResource getSingleResourceByURI(String resourceURI) {
167 if (null == resourceURI) {
171 synchronized (singleResourceMap) {
172 res = singleResourceMap.get(resourceURI);
177 public Resource getResourceByURI(String resourceURI) {
178 Resource res = getSingleResourceByURI(resourceURI);
182 public synchronized int getResourceCount() {
183 return singleResourceMap.size();