View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  package com.ontotext.wsmo4j.common;
20  
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.LinkedHashMap;
27  import java.util.LinkedHashSet;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.omwg.ontology.Value;
32  import org.wsmo.common.Entity;
33  import org.wsmo.common.IRI;
34  import org.wsmo.common.Identifier;
35  import org.wsmo.common.exception.InvalidModelException;
36  import org.wsmo.common.exception.SynchronisationException;
37  
38  /**
39   * <p>Title: WSMO4J</p>
40   * <p>Description: WSMO API and a Reference Implementation</p>
41   * <p>Copyright:  Copyright (c) 2004-2005</p>
42   * <p>Company: OntoText Lab. / SIRMA </p>
43   * @author not attributable
44   * @version 1.0
45   *
46   */
47  public abstract class EntityImpl implements Entity {
48  
49      private Identifier id;
50      private HashMap <IRI, Set <Object>> nfpProps;
51  
52      public EntityImpl(Identifier thisID) {
53          if (thisID == null) {
54              throw new IllegalArgumentException(Errors.ERR_INVALID_ID);
55          }
56          this.id = thisID;
57          nfpProps = new HashMap <IRI, Set <Object>> ();
58      }
59  
60      /**
61       * Returns the list of values associated
62       * with the specified key.
63       * @param key The key of interest.
64       * @return A list of values associated with
65       * the specified key.
66       */
67      public Set <Object> listNFPValues(IRI key) throws SynchronisationException {
68          //preconditions
69          if (key == null) {
70              throw new IllegalArgumentException(Errors.ERR_INVALID_NFP_KEY);
71          }
72  
73          LinkedHashSet <Object> vals = (LinkedHashSet <Object>) nfpProps.get(key);
74  
75          if (vals == null) {
76              return Collections.unmodifiableSet(new HashSet <Object> ()); // immutable
77          }
78          return Collections.unmodifiableSet(vals);
79      }
80  
81      public Map <IRI, Set <Object>> listNFPValues() throws SynchronisationException {
82          LinkedHashMap <IRI, Set <Object>>  result = new LinkedHashMap <IRI, Set <Object>> ();
83          for(Iterator <IRI> it = nfpProps.keySet().iterator(); it.hasNext();) {
84              IRI key = it.next();
85              LinkedHashSet <Object> curVal = (LinkedHashSet <Object>) nfpProps.get(key);
86              result.put(key, Collections.unmodifiableSet(curVal));
87          }
88          return result;
89      }
90  
91      private void _addNFPValue(IRI key, Object value) throws SynchronisationException,
92              InvalidModelException {
93          //preconditions
94          if (key == null) {
95              throw new IllegalArgumentException(Errors.ERR_INVALID_NFP_KEY);
96          }
97          if (value == null) {
98              throw new IllegalArgumentException(Errors.ERR_INVALID_NFP_VALUE);
99          }
100 
101         synchronized (this.nfpProps) {
102             LinkedHashSet <Object> vals = (LinkedHashSet <Object>) nfpProps.get(key);
103 
104             if (vals == null) {
105                 vals = new LinkedHashSet <Object>();
106                 nfpProps.put(key, vals);
107             }
108             vals.add(value);
109         }
110     }
111 
112     public void addNFPValue(IRI key, Identifier value) throws SynchronisationException,
113             InvalidModelException {
114 
115         this._addNFPValue(key, value);
116     }
117 
118 
119     public void addNFPValue(IRI key, Value value) throws SynchronisationException,
120             InvalidModelException {
121 
122         this._addNFPValue(key, value);
123     }
124 
125 
126     private void _removeNFPValue(IRI key, Object value) throws SynchronisationException,
127             InvalidModelException {
128 
129         //preconditions
130         if (key == null) {
131             throw new IllegalArgumentException(Errors.ERR_INVALID_NFP_KEY);
132         }
133         if (value == null) {
134             throw new IllegalArgumentException(Errors.ERR_INVALID_NFP_VALUE);
135         }
136 
137         synchronized (this.nfpProps) {
138             LinkedHashSet <Object> vals = (LinkedHashSet <Object>) nfpProps.get(key);
139             //no such entry?
140             if (vals == null) {
141                 return;
142             }
143             vals.remove(value);
144             if (vals.size() == 0) {
145                 //remove key entry, no values left
146                 nfpProps.remove(key);
147             }
148         }
149     }
150 
151     public void removeNFPValue(IRI key, Identifier value) throws SynchronisationException,
152             InvalidModelException {
153 
154         this._removeNFPValue(key, value);
155     }
156 
157     public void removeNFPValue(IRI key, Value value) throws SynchronisationException,
158             InvalidModelException {
159 
160         this._removeNFPValue(key, value);
161     }
162 
163     public void removeNFP(IRI key)
164             throws SynchronisationException, InvalidModelException {
165 
166         //preconditions
167         if (key == null) {
168             throw new IllegalArgumentException(Errors.ERR_INVALID_NFP_KEY);
169         }
170 
171         this.nfpProps.remove(key);
172     }
173 
174     public Identifier getIdentifier() {
175         return id;
176     }
177 
178     public boolean equals(Object target) {
179         if (target == this) {
180             return true; // instance match
181         }
182         if (null == target || false == target instanceof Entity) {
183             return false;
184         }
185         return getIdentifier().equals(((Entity) target).getIdentifier());
186     }
187 
188     public int hashCode() {
189         return id.hashCode();
190     }
191 }
192 
193 /*
194  * $Log$
195  * Revision 1.8  2007/04/02 12:19:08  morcen
196  * Generics support added to wsmo-api, wsmo4j and wsmo-test
197  *
198  * Revision 1.7  2007/04/02 12:13:20  morcen
199  * Generics support added to wsmo-api, wsmo4j and wsmo-test
200  *
201  * Revision 1.6  2005/09/16 12:41:05  marin_dimitrov
202  * 1. Entity::addNfpValue(IRI, Object) split into Entity::addNfpValue(IRI, Identifier) and Entity::addNfpValue(IRI, Value)
203  *
204  * 2. same for removeNFP
205  *
206  * Revision 1.5  2005/08/31 12:28:44  vassil_momtchev
207  * addNFPValue and removeNFPValue changed back to accept Object. The Value type limits all kind of identifiers (IRI, AnonymousID)
208  *
209  * Revision 1.4  2005/08/31 09:17:29  vassil_momtchev
210  * use Type and Value instead of Object where appropriate bug SF[1276677]
211  *
212  * Revision 1.3  2005/08/19 08:58:31  marin_dimitrov
213  * added removeNFP(key)
214  *
215  * Revision 1.2  2005/06/22 09:16:05  alex_simov
216  * Simplified equals() method. Identity strongly relies on identifier string
217  *
218  * Revision 1.1  2005/06/01 12:00:32  marin_dimitrov
219  * v0.4.0
220  *
221  * Revision 1.5  2005/05/17 12:42:56  alex
222  * immutable Collections.EMPTY_SET instead of a new empty set
223  *
224  * Revision 1.4  2005/05/17 12:00:00  alex
225  * Collections.unmodifiableSet() used instead of new set construction
226  *
227  * Revision 1.3  2005/05/12 13:46:32  marin
228  * changed to an _abstract_ class (not supposed to be used directly but only through subclassing)
229  *
230  * Revision 1.2  2005/05/11 13:33:58  alex
231  * copyright notice updated
232  *
233  * Revision 1.1  2005/05/11 11:50:56  alex
234  * initial commit
235  *
236  */