com.arm.mbed.cloud.sdk.common.ServiceStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mbed-cloud-sdk Show documentation
Show all versions of mbed-cloud-sdk Show documentation
The Pelion Cloud SDK (formerly known as Mbed Cloud SDK) provides a simplified interface to the Pelion Cloud APIs by exposing functionality using conventions and paradigms familiar to Java developers.
package com.arm.mbed.cloud.sdk.common;
import java.util.concurrent.ConcurrentHashMap;
import com.arm.mbed.cloud.sdk.annotations.Internal;
import com.arm.mbed.cloud.sdk.annotations.Preamble;
@Preamble(description = "Store of created services")
@Internal
public class ServiceStore {
private final ConcurrentHashMap, Object> store;
private final ApiClientWrapper client;
/**
* Constructor.
*
* @param client
* client to use.
*/
public ServiceStore(ApiClientWrapper client) {
store = new ConcurrentHashMap<>();
this.client = client;
}
/**
* Instantiates a service based on its class definition.
*
* @param serviceClass
* service definition
* @param
* type of the service to call
* @return corresponding service.
* @throws MbedCloudException
* if parameters are incorrect.
*/
public S getService(Class serviceClass) throws MbedCloudException {
if (serviceClass == null) {
throw new MbedCloudException(new IllegalArgumentException("serviceClass is null"));
}
final S service = getServiceFromStore(serviceClass);
if (service != null) {
return service;
}
createService(serviceClass);
return getService(serviceClass);
}
protected void createService(Class serviceClass) {
final S service = client.createService(serviceClass);
if (service != null) {
store.putIfAbsent(serviceClass, service);
}
}
@SuppressWarnings("unchecked")
protected S getServiceFromStore(Class serviceClass) {
return (S) store.get(serviceClass);
}
}