View Javadoc

1   package org.molwind.model;
2   
3   /*
4    * This file is part of Molwind.
5    *
6    * Molwind is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Molwind is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.HashMap;
23  
24  /**
25   * BaseWorldEntity provides a basic implementation of a world entity.
26   *
27   * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
28   * @version 1.0
29   */
30  public class BaseWorldEntity implements WorldEntity {
31  
32      protected ArrayList relationships;
33      private HashMap   attributes;
34      
35  
36      /**
37       * Sets the value of the attribute with the given name.
38       *
39       * @param name
40       *      the name of the attribute
41       * @param value
42       *      the value of the attribute
43       */
44      public void putAttribute(String name,Object value){
45  	
46  	attributes.put(name,value);
47  	
48      }
49      
50  
51       
52      /**
53       * Returns the value of the attribute with the given name.
54       *
55       * @param name
56       *      the name of the attribute
57       * @return
58       *      the value of the attribute
59       */
60      public Object getAttribute(final String name){
61  	return attributes.get(name);
62  
63      }
64  
65      protected BaseWorldEntity() {
66          relationships = new ArrayList();
67  	attributes = new HashMap();
68      }
69  
70      /**
71       * Returns an iterator of relationship objects.
72       *
73       * @return
74       *      an iterator of relationship objects
75       */
76      public Iterator getRelationships() {
77          return relationships.iterator();
78      }
79  
80  
81      /**
82       * Tests whether the given relationship already exists.
83       *
84       * @param relation
85       *      the relationship to be tested
86       * @return
87       *      true if the relationship already exists, false otherwise
88       */
89      public boolean existRelationship(final Relationship relation) {
90          return relationships.contains(relation);
91      }
92  
93      /**
94       * Adds a relationship object to this entity.
95       *
96       * @param relation
97       *      the relationship to be added
98       */
99      public void addRelationship(final Relationship relation) {
100         if ((relation.getLeft() == null)
101                 && (relation instanceof BaseRelationship)) {
102             ((BaseRelationship) relation).setLeft(this);
103         }
104 
105         if ((relation.getRight() == null)
106                 && (relation instanceof BaseRelationship)) {
107             ((BaseRelationship) relation).setRight(this);
108         }
109 
110         relationships.add(relation);
111     }
112 
113     /**
114      * Searches the relationships for the given relationship.
115      *
116      * @param relation
117      *      the query relationship
118      * @return
119      *      a possibly empty array of relationship(s) matching the query
120      */
121     public Relationship[] getRelationship(final Relationship relation) {
122         Iterator it = getRelationships();
123         ArrayList matches = new ArrayList();
124         BaseRelationship baseRelation = null;
125 
126         if (relation instanceof PartialMatch) {
127             baseRelation = (BaseRelationship) relation;
128         }
129 
130         while (it.hasNext()) {
131             Relationship rel = (Relationship) it.next();
132             boolean addIt = false;
133 
134             if (baseRelation != null) {
135                 addIt = baseRelation.matches(rel);
136             } else {
137                 addIt = rel.equals(relation);
138             }
139 
140             if (addIt) {
141                 matches.add(rel);
142             }
143         }
144 
145         Relationship[] relations = new Relationship[matches.size()];
146 
147         return ((Relationship[]) matches.toArray(relations));
148     }
149     /**
150      * Returns 0 for this kind of entity
151      * @return
152      *     int 0
153      */
154     public int getDimension(){
155 	return 0;
156     }
157 
158 }