package org.oic.simulator;
-import org.oic.simulator.ResourceAttribute;
+import java.util.HashMap;
import java.util.Map;
/**
- * This class represents the resource model of a resource and it provides a set
- * of native methods for accessing the resource model.
+ * This class represents the resource model of a resource. A resource model
+ * contains a set of attributes and their properties. It represents the complex
+ * value type of an attribute.
*/
public class SimulatorResourceModel {
+ private Map<String, AttributeValue> mValues = null;
+ private Map<String, AttributeProperty> mProperties = null;
+
/**
- * Constructor for creating a native resource model object. Client requests
- * such as PUT and POST uses this method for passing the new new/updated
- * resource model.
+ * Constructs new {@link SimulatorResourceModel} object.
*/
public SimulatorResourceModel() {
- create();
+ mValues = new HashMap<>();
+ mProperties = new HashMap<>();
}
/**
- * Method for adding an attribute whose value is of type int.
- *
- * @param name
- * Name of the attribute
+ * API to add an attribute to resource model.
+ *
+ * @param attrName
+ * Name of the attribute.
* @param value
- * Value of the attribute
+ * Value of the attribute.
+ *
+ * @throws InvalidArgsException
+ * This exception will be thrown on invalid input.
*/
- public void addAttribute(String name, int value) {
- addAttributeInt(name, value);
+ public void addAttribute(String attrName, AttributeValue value)
+ throws InvalidArgsException {
+ if (null == attrName || attrName.isEmpty())
+ throw new InvalidArgsException("Invalid attribute name!");
+
+ if (null == value)
+ throw new InvalidArgsException("Attribute value is null!");
+
+ mValues.put(attrName, value);
}
/**
- * Method for adding an attribute whose value is of type double.
- *
- * @param name
- * Name of the attribute
- * @param value
- * Value of the attribute
+ * API to add an attribute to resource model.
+ *
+ * @param attribute
+ * {@link SimulatorResourceAttribute} to be added to resource
+ * model.
+ *
+ * @throws InvalidArgsException
+ * This exception will be thrown on invalid input.
*/
- public void addAttribute(String name, double value) {
- addAttributeDouble(name, value);
+ public void addAttribute(SimulatorResourceAttribute attribute)
+ throws InvalidArgsException {
+ if (null == attribute || null == attribute.name()
+ || attribute.name().isEmpty() || null == attribute.value())
+ throw new InvalidArgsException("Invalid attribute!");
+
+ mValues.put(attribute.name(), attribute.value());
}
/**
- * Method for adding an attribute whose value is of type boolean.
- *
- * @param name
- * Name of the attribute
- * @param value
- * Value of the attribute
+ * API to set attribute's property.
+ *
+ * @param attrName
+ * Name of the attribute.
+ * @param property
+ * {@link AttributeProperty} to be set for attribute.
+ *
+ * @throws InvalidArgsException
+ * This exception will be thrown on invalid input.
*/
- public void addAttribute(String name, boolean value) {
- addAttributeBoolean(name, value);
+ public void setAttributeProperty(String attrName, AttributeProperty property)
+ throws InvalidArgsException {
+ if (null == attrName || attrName.isEmpty())
+ throw new InvalidArgsException("Invalid attribute!");
+
+ if (null == property)
+ throw new InvalidArgsException("Invalid attribute property!");
+
+ mProperties.put(attrName, property);
}
/**
- * Method for adding an attribute whose value is of type string.
- *
- * @param name
- * Name of the attribute
+ * API to set attribute's value.
+ *
+ * @param attrName
+ * Name of the attribute.
* @param value
- * Value of the attribute
+ * {@link AttributeValue} to be set for attribute.
+ *
+ * @throws InvalidArgsException
+ * This exception will be thrown on invalid input.
*/
- public void addAttribute(String name, String value) {
- addAttributeString(name, value);
+ public void setAttributeValue(String attrName, AttributeValue value)
+ throws InvalidArgsException {
+ if (null == attrName || attrName.isEmpty())
+ throw new InvalidArgsException("Invalid attribute name!");
+
+ if (null == value)
+ throw new InvalidArgsException("Attribute value is null!");
+
+ mValues.put(attrName, value);
}
/**
- * Method for getting the number of attributes this model comprised of.
+ * API to get all the attributes of resource model.
*
- * @return Number of attributes.
+ * @return Map of attributes with attribute name as key and its
+ * corresponding {@link SimulatorResourceAttribute} as value.
*/
- public native int size();
+ public Map<String, SimulatorResourceAttribute> getAttributes() {
+ Map<String, SimulatorResourceAttribute> attributes = new HashMap<>();
+ for (Map.Entry<String, AttributeValue> entry : mValues.entrySet()) {
+ SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
+ entry.getKey(), entry.getValue(), mProperties.get(entry
+ .getKey()));
+ attributes.put(entry.getKey(), attribute);
+ }
+
+ return attributes;
+ }
/**
- * Method for getting all attributes.
+ * API to get attribute by name.
*
- * @return Map of attributes with attribute name as the key and its
- * corresponding {@link ResourceAttribute} object as the value.
+ * @param attrName
+ * Name of the attribute.
+ *
+ * @return {@link SimulatorResourceAttribute}.
*/
- public native Map<String, ResourceAttribute> getAttributes();
+ public SimulatorResourceAttribute getAttribute(String attrName) {
+ if (mValues.containsKey(attrName)) {
+ SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
+ attrName, mValues.get(attrName), mProperties.get(attrName));
+ return attribute;
+ }
+
+ return null;
+ }
/**
- * Method for getting a specific attribute by its name.
+ * API to check whether resource model has an attribute with given name.
*
* @param attrName
- * Name of the attribute
+ * Name of the attribute.
*
- * @return An object of {@link ResourceAttribute}.
+ * @return true if resource model has an attribute with given name,
+ * otherwise false.
*/
- public native ResourceAttribute getAttribute(String attrName);
-
- private native void addAttributeInt(String name, int value);
-
- private native void addAttributeDouble(String name, double value);
+ public boolean containsAttribute(String attrName) {
+ if (mValues.containsKey(attrName))
+ return true;
+ return false;
+ }
- private native void addAttributeBoolean(String name, boolean value);
+ /**
+ * API to get value type information of attribute.
+ *
+ * @param attrName
+ * Name of the attribute.
+ *
+ * @return Attribute value type information {@link AttributeValue.TypeInfo
+ *
+ * }.
+ */
+ public AttributeValue.TypeInfo getAttributeType(String attrName) {
+ if (mValues.containsKey(attrName))
+ return mValues.get(attrName).typeInfo();
+ return null;
+ }
- private native void addAttributeString(String name, String value);
+ /**
+ * API to remove attribute from resource model.
+ *
+ * @param attrName
+ * Name of the attribute.
+ *
+ * @return true if resource model has an attribute with the given name,
+ * otherwise false.
+ */
+ public boolean removeAttribute(String attrName) {
+ if (mValues.containsKey(attrName)) {
+ mValues.remove(attrName);
+ if (mProperties.containsKey(attrName))
+ mProperties.remove(attrName);
+ return true;
+ }
- private SimulatorResourceModel(long nativeHandle) {
- this.nativeHandle = nativeHandle;
+ return false;
}
- @Override
- protected void finalize() throws Throwable {
- dispose();
+ /**
+ * API to get number of attributes present in resource model.
+ *
+ * @return Number of attributes present in resource model.
+ */
+ public int size() {
+ return mValues.size();
}
- private native void create();
+ /**
+ * API to update the attribute values from given
+ * {@link SimulatorResourceModel}.
+ *
+ * @param resourceModel
+ * {@link SimulatorResourceModel} holding the new attribute
+ * values.
+ */
+ public void update(SimulatorResourceModel resourceModel) {
+ if (null == resourceModel || 0 == resourceModel.size())
+ return;
- private native void dispose();
+ for (Map.Entry<String, SimulatorResourceAttribute> entry : resourceModel
+ .getAttributes().entrySet()) {
+ SimulatorResourceAttribute newAttribute = entry.getValue();
+ SimulatorResourceAttribute attribute = getAttribute(entry.getKey());
+ if (null != newAttribute && null != attribute) {
+ if (null != attribute.property()) {
+ AttributeValueValidation validation = new AttributeValueValidation(
+ attribute.property());
+ if (!validation.validate(newAttribute.value())) {
+ mValues.put(entry.getKey(), newAttribute.value());
+ }
+ } else {
+ mValues.put(entry.getKey(), newAttribute.value());
+ }
+ }
+ }
+ }
- private long nativeHandle;
-}
\ No newline at end of file
+ private SimulatorResourceModel(Map<String, AttributeValue> values,
+ Map<String, AttributeProperty> properties) {
+ mValues = values;
+ mProperties = properties;
+ }
+}