X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fsimulator%2Fjava%2Fsdk%2Fsrc%2Forg%2Foic%2Fsimulator%2FSimulatorResourceModel.java;h=c1e3b80425a942a6f755435829b555f0710a118e;hb=b76f9709482b03334b468ab47a295761b3fd6a78;hp=4186c6e7e1d64b3deb363368208f4049b3872358;hpb=01be9765a95ede47003777ee576fe9a4de4229e5;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java b/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java index 4186c6e..c1e3b80 100644 --- a/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java +++ b/service/simulator/java/sdk/src/org/oic/simulator/SimulatorResourceModel.java @@ -16,158 +16,237 @@ 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. */ public class SimulatorResourceModel { + private Map mValues = null; + private Map mProperties = null; + /** - * Constructor for creating a native resource model object. Client requests - * such as PUT and POST uses this method for passing the new/updated - * resource model. + * Constructs new {@SimulatorResourceModel} object. */ public SimulatorResourceModel() { - create(); + mValues = new HashMap<>(); + mProperties = new HashMap<>(); } /** - * API to add an attribute of type integer. + * API to add an attribute to resource model. * - * @param name - * Name of the attribute + * @param attrName + * Name of the attribute. * @param value - * Value of the attribute + * Value of the attribute. * * @throws InvalidArgsException - * This exception will be thrown if the attribute name is - * invalid. - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * This exception will be thrown on invalid input. */ - public native void addAttributeInt(String name, int value) - throws InvalidArgsException, SimulatorException; + 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); + } /** - * API to add an attribute of type double. + * API to add an attribute to resource model. * - * @param name - * Name of the attribute - * @param value - * Value of the attribute + * @param attribute + * {@link SimulatorResourceAttribute} to be add to resource + * model. * * @throws InvalidArgsException - * This exception will be thrown if the attribute name is - * invalid. - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * This exception will be thrown on invalid input. */ - public native void addAttributeDouble(String name, double value) - throws InvalidArgsException, SimulatorException; + 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()); + } /** - * API to add an attribute of type boolean. + * API to set attribute's property. * - * @param name - * Name of the attribute - * @param value - * Value of the attribute + * @param attrName + * Name of the attribute. + * @param property + * {@link AttributeProperty} to be set for attribute. * * @throws InvalidArgsException - * This exception will be thrown if the attribute name is - * invalid. - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * This exception will be thrown on invalid input. */ - public native void addAttributeBoolean(String name, boolean value) - throws InvalidArgsException, SimulatorException; + 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); + } /** - * API to add an attribute of type string. + * API to set attribute's value. * - * @param name - * Name of the attribute + * @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 if the attribute name is - * invalid. - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * This exception will be thrown on invalid input. */ - public native void addAttributeString(String name, String value) - throws InvalidArgsException, SimulatorException; + 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); + } /** - * API to get number of attributes for this model. + * API to get all the attributes of resource model. * - * @return Number of attributes. - * - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * @return Map of attributes with attribute name as key and its + * corresponding {@link SimulatorResourceAttribute} as value. */ - public native int size() throws SimulatorException; + public Map getAttributes() { + if (mValues.size() == 0) + return null; + + Map attributes = new HashMap<>(); + for (Map.Entry entry : mValues.entrySet()) { + SimulatorResourceAttribute attribute = new SimulatorResourceAttribute( + entry.getKey(), entry.getValue(), mProperties.get(entry + .getKey())); + attributes.put(entry.getKey(), attribute); + } + + return attributes; + } /** - * API 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. * - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * @return {@link SimulatorResourceAttribute}. */ - public native Map getAttributes() - throws SimulatorException; + public SimulatorResourceAttribute getAttribute(String attrName) { + if (mValues.containsKey(attrName)) { + SimulatorResourceAttribute attribute = new SimulatorResourceAttribute( + attrName, mValues.get(attrName), mProperties.get(attrName)); + return attribute; + } + + return null; + } /** - * API to get attribute by its name. + * API to check whether resource model has attribute. * - * @param name - * Name of the attribute + * @param attrName + * Name of the attribute. * - * @return An object of {@link ResourceAttribute}. + * @return true if resource model has an attribute with given name, + * otherwise false. + */ + public boolean containsAttribute(String attrName) { + if (mValues.containsKey(attrName)) + return true; + return false; + } + + /** + * API to get value type information of attribute. * - * @throws InvalidArgsException - * This exception will be thrown if the attribute does not - * exist. + * @param attrName + * Name of the attribute. * - * @throws SimulatorException - * This exception will be thrown either if the resource model is - * not found or for some general errors. + * @return Attribute value type information {@AttributeValue.TypeInfo}. */ - public native ResourceAttribute getAttribute(String name) - throws InvalidArgsException, SimulatorException; - - private SimulatorResourceModel(long nativeHandle) { - this.nativeHandle = nativeHandle; + public AttributeValue.TypeInfo getAttributeType(String attrName) { + if (mValues.containsKey(attrName)) + return mValues.get(attrName).typeInfo(); + return null; } - @Override - protected void finalize() throws Throwable { - try { - dispose(); - } catch(Throwable t){ - throw t; - } finally{ - System.out.println("Calling finalize of Super Class"); - super.finalize(); + /** + * API to remove attribute from resource model. + * + * @param attrName + * Name of the attribute. + * + * @return true if resource model has attribute it is removed, otherwise + * false. + */ + public boolean removeAttribute(String attrName) { + if (mValues.containsKey(attrName)) { + mValues.remove(attrName); + if (mProperties.containsKey(attrName)) + mProperties.remove(attrName); + return true; } + + return false; } - private native void create(); + /** + * 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 dispose(); + /** + * API to update the attribute values from given + * {@link SimulatorResourceModel}. + */ + public void update(SimulatorResourceModel resourceModel) { + if (null == resourceModel || 0 == resourceModel.size()) + return; + + for (Map.Entry 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 + // Methods used in native code + private SimulatorResourceModel(Map values, + Map properties) { + mValues = values; + mProperties = properties; + } +}