/src/main/java/org/exoplatform/social/client/api/model/Model.java

http://github.com/exosocial/exo.social.client · Java · 211 lines · 79 code · 23 blank · 109 comment · 5 complexity · ddb861dcc9be7c5f6365305fead2b29e MD5 · raw file

  1. /*
  2. * Copyright (C) 2003-2011 eXo Platform SAS.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package org.exoplatform.social.client.api.model;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import org.exoplatform.social.client.api.event.PropertyChangeListener;
  23. import org.exoplatform.social.client.api.util.PropertyChangeSupport;
  24. import org.json.simple.JSONAware;
  25. import org.json.simple.JSONObject;
  26. import org.json.simple.JSONStreamAware;
  27. /**
  28. * The general model extends {@link org.json.simple.JSONObject}'s interfaces.
  29. * <p/>
  30. * Inspiration taken from: http://code.google.com/p/opensocial-java-client/source/browse/trunk/java/src/org/opensocial/models/Model.java
  31. *
  32. * @author <a href="http://hoatle.net">hoatle (hoatlevan at gmail dot com)</a>
  33. * @since May 19, 2011
  34. */
  35. public abstract class Model extends JSONObject implements Map, JSONAware, JSONStreamAware {
  36. /**
  37. * The property change event support for this model.
  38. */
  39. protected PropertyChangeSupport propertyChanges = new PropertyChangeSupport(this);
  40. /**
  41. * Returns the complete set of properties associated with the model instance.
  42. *
  43. * @return a string array
  44. */
  45. public String[] getFieldNames() {
  46. int i = 0;
  47. String[] fieldNames = new String[size()];
  48. Set<Map.Entry<String, Object>> fields = entrySet();
  49. for (Map.Entry<String, Object> field : fields) {
  50. fieldNames[i] = field.getKey();
  51. i++;
  52. }
  53. return fieldNames;
  54. }
  55. /**
  56. * Returns {@code true} if a value is associated with the specified field name, {@code false} otherwise.
  57. *
  58. * @param fieldName name of field to look up
  59. * @return a boolean value
  60. */
  61. public boolean hasField(String fieldName) {
  62. return containsKey(fieldName);
  63. }
  64. /**
  65. * Returns the value of the specified field as an Object.
  66. *
  67. * @param fieldName name of field whose value is to be returned
  68. * @return an object associated with fieldName
  69. */
  70. public Object getField(String fieldName) {
  71. return get(fieldName);
  72. }
  73. /**
  74. * Returns the value of the specified field as a {@link Map}. Equivalent to {@code (Map) getField(fieldName)}, hence
  75. * this method will throw a ClassCastException if the field does not implement Map.
  76. *
  77. * @param fieldName name of field whose value is to be returned
  78. * @return a map associated with fieldName
  79. * @see ClassCastException
  80. */
  81. public Map getFieldAsMap(String fieldName) {
  82. return (Map) get(fieldName);
  83. }
  84. /**
  85. * Returns the value of the specified field as a {@link java.util.List}. Equivalent to {@code (List)
  86. * getField(fieldName)}, hence this method will throw a ClassCastException if the field does not implement List.
  87. *
  88. * @param fieldName name of field whose value is to be returned
  89. * @return a list associated with fieldName
  90. * @see ClassCastException
  91. */
  92. public List getFieldAsList(String fieldName) {
  93. return (List) get(fieldName);
  94. }
  95. /**
  96. * Returns the value of the specified field as a {@link String}. Equivalent to {@code (String) getField(fieldName)},
  97. * hence this method will throw a ClassCastException if the field is not of type String.
  98. *
  99. * @param fieldName name of field whose value is to be returned
  100. * @return a string associated with fieldName
  101. * @see ClassCastException
  102. */
  103. public String getFieldAsString(String fieldName) {
  104. try {
  105. return (String) get(fieldName);
  106. } catch (ClassCastException e) {
  107. return "" + get(fieldName);
  108. }
  109. }
  110. /**
  111. * Returns {@code true} if the value of the specified field implements {@link Map}, {@code false} otherwise.
  112. *
  113. * @param fieldName name of field to look up
  114. * @return a boolean value
  115. */
  116. public boolean isFieldMultikeyed(String fieldName) {
  117. Object field = get(fieldName);
  118. if (field instanceof Map) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. /**
  124. * Returns {@code true} if the value of the specified field implements {@link List}, {@code false} otherwise.
  125. *
  126. * @param fieldName name of field to look up
  127. * @return a boolean value
  128. */
  129. public boolean isFieldMultivalued(String fieldName) {
  130. Object field = get(fieldName);
  131. if (field instanceof List) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * Sets the value of the specified field to the passed Object.
  138. *
  139. * @param fieldName name of field to set
  140. * @param value object to associate with passed field name
  141. */
  142. public void setField(String fieldName, Object value) {
  143. //Raise event when change value of property.
  144. propertyChanges.propertyChange(fieldName, get(fieldName), value);
  145. put(fieldName, value);
  146. }
  147. /**
  148. * Adds the passed Object to the list field with the specified name.
  149. *
  150. * @param fieldName name of list field for which the passed item should be added
  151. * @param item item to add
  152. */
  153. public void addToListField(String fieldName, Object item) {
  154. List<Object> listField;
  155. if (containsKey(fieldName)) {
  156. listField = getFieldAsList(fieldName);
  157. } else {
  158. listField = new ArrayList<Object>();
  159. }
  160. listField.add(item);
  161. put(fieldName, listField);
  162. }
  163. /**
  164. * Adds a property change event listener to this model
  165. *
  166. * @param listener The listener to be added
  167. */
  168. public void addPropertyChangeListener(PropertyChangeListener listener) {
  169. propertyChanges.addPropertyChangeListener(listener);
  170. }
  171. /**
  172. * Removes a property change event listener which was added to this model.
  173. *
  174. * @param listener The listener to be removed.
  175. */
  176. public void removePropertyChangeListener(PropertyChangeListener listener) {
  177. propertyChanges.removeLifecycleListener(listener);
  178. }
  179. /**
  180. * Gets the property change listeners registered and associated with this property change listener. If this
  181. * property change has no listeners registered, a zero-length array is returned.
  182. *
  183. * @return an array of listeners
  184. */
  185. public PropertyChangeListener[] findPropertyChangeListeners() {
  186. return propertyChanges.findPropertyChangeListeners();
  187. }
  188. }