PageRenderTime 62ms CodeModel.GetById 55ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
Possible License(s): AGPL-3.0
  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 */
 17package org.exoplatform.social.client.api.model;
 18
 19import java.util.ArrayList;
 20import java.util.List;
 21import java.util.Map;
 22import java.util.Set;
 23
 24import org.exoplatform.social.client.api.event.PropertyChangeListener;
 25import org.exoplatform.social.client.api.util.PropertyChangeSupport;
 26import org.json.simple.JSONAware;
 27import org.json.simple.JSONObject;
 28import org.json.simple.JSONStreamAware;
 29
 30/**
 31 * The general model extends {@link org.json.simple.JSONObject}'s interfaces.
 32 * <p/>
 33 * Inspiration taken from: http://code.google.com/p/opensocial-java-client/source/browse/trunk/java/src/org/opensocial/models/Model.java
 34 *
 35 * @author <a href="http://hoatle.net">hoatle (hoatlevan at gmail dot com)</a>
 36 * @since May 19, 2011
 37 */
 38public abstract class Model extends JSONObject implements Map, JSONAware, JSONStreamAware {
 39
 40  /**
 41   * The property change event support for this model.
 42   */
 43  protected PropertyChangeSupport propertyChanges = new PropertyChangeSupport(this);
 44  
 45  /**
 46   * Returns the complete set of properties associated with the model instance.
 47   *
 48   * @return a string array
 49   */
 50  public String[] getFieldNames() {
 51    int i = 0;
 52    String[] fieldNames = new String[size()];
 53
 54    Set<Map.Entry<String, Object>> fields = entrySet();
 55    for (Map.Entry<String, Object> field : fields) {
 56      fieldNames[i] = field.getKey();
 57      i++;
 58    }
 59
 60    return fieldNames;
 61  }
 62
 63  /**
 64   * Returns {@code true} if a value is associated with the specified field name, {@code false} otherwise.
 65   *
 66   * @param fieldName name of field to look up
 67   * @return a boolean value
 68   */
 69  public boolean hasField(String fieldName) {
 70    return containsKey(fieldName);
 71  }
 72
 73  /**
 74   * Returns the value of the specified field as an Object.
 75   *
 76   * @param fieldName name of field whose value is to be returned
 77   * @return an object associated with fieldName
 78   */
 79  public Object getField(String fieldName) {
 80    return get(fieldName);
 81  }
 82
 83  /**
 84   * Returns the value of the specified field as a {@link Map}. Equivalent to {@code (Map) getField(fieldName)}, hence
 85   * this method will throw a ClassCastException if the field does not implement Map.
 86   *
 87   * @param fieldName name of field whose value is to be returned
 88   * @return a map associated with fieldName
 89   * @see ClassCastException
 90   */
 91  public Map getFieldAsMap(String fieldName) {
 92    return (Map) get(fieldName);
 93  }
 94
 95  /**
 96   * Returns the value of the specified field as a {@link java.util.List}. Equivalent to {@code (List)
 97   * getField(fieldName)}, hence this method will throw a ClassCastException if the field does not implement List.
 98   *
 99   * @param fieldName name of field whose value is to be returned
100   * @return a list associated with fieldName
101   * @see ClassCastException
102   */
103  public List getFieldAsList(String fieldName) {
104    return (List) get(fieldName);
105  }
106
107  /**
108   * Returns the value of the specified field as a {@link String}. Equivalent to {@code (String) getField(fieldName)},
109   * hence this method will throw a ClassCastException if the field is not of type String.
110   *
111   * @param fieldName name of field whose value is to be returned
112   * @return a string associated with fieldName
113   * @see ClassCastException
114   */
115  public String getFieldAsString(String fieldName) {
116    try {
117      return (String) get(fieldName);
118    } catch (ClassCastException e) {
119      return "" + get(fieldName);
120    }
121  }
122
123  /**
124   * Returns {@code true} if the value of the specified field implements {@link Map}, {@code false} otherwise.
125   *
126   * @param fieldName name of field to look up
127   * @return a boolean value
128   */
129  public boolean isFieldMultikeyed(String fieldName) {
130    Object field = get(fieldName);
131    if (field instanceof Map) {
132      return true;
133    }
134
135    return false;
136  }
137
138  /**
139   * Returns {@code true} if the value of the specified field implements {@link List}, {@code false} otherwise.
140   *
141   * @param fieldName name of field to look up
142   * @return a boolean value
143   */
144  public boolean isFieldMultivalued(String fieldName) {
145    Object field = get(fieldName);
146    if (field instanceof List) {
147      return true;
148    }
149
150    return false;
151  }
152
153  /**
154   * Sets the value of the specified field to the passed Object.
155   *
156   * @param fieldName name of field to set
157   * @param value     object to associate with passed field name
158   */
159  public void setField(String fieldName, Object value) {
160  //Raise event when change value of property.
161    propertyChanges.propertyChange(fieldName, get(fieldName), value);
162    put(fieldName, value);
163  }
164
165  /**
166   * Adds the passed Object to the list field with the specified name.
167   *
168   * @param fieldName name of list field for which the passed item should be added
169   * @param item      item to add
170   */
171  public void addToListField(String fieldName, Object item) {
172    List<Object> listField;
173
174    if (containsKey(fieldName)) {
175      listField = getFieldAsList(fieldName);
176    } else {
177      listField = new ArrayList<Object>();
178    }
179
180    listField.add(item);
181    put(fieldName, listField);
182  }
183  
184  /**
185   * Adds a property change event listener to this model
186   * 
187   * @param listener The listener to be added
188   */
189  public void addPropertyChangeListener(PropertyChangeListener listener) {
190    propertyChanges.addPropertyChangeListener(listener);
191  }
192  
193  /**
194   * Removes a property change event listener which was added to this model.
195   *
196   * @param listener The listener to be removed.
197   */
198  public void removePropertyChangeListener(PropertyChangeListener listener) {
199    propertyChanges.removeLifecycleListener(listener);
200  }
201  
202  /**
203   * Gets the property change listeners registered and associated with this property change listener. If this
204   * property change has no listeners registered, a zero-length array is returned.
205   * 
206   * @return an array of listeners
207   */
208  public PropertyChangeListener[] findPropertyChangeListeners() {
209    return propertyChanges.findPropertyChangeListeners();
210  }
211}