View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.javagen.agile.oo.model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.xml.bind.annotation.XmlRootElement;
22  import javax.xml.bind.annotation.XmlTransient;
23  
24  import org.javagen.agile.core.model.AbstractModel;
25  import org.javagen.agile.core.model.Model;
26  import org.javagen.agile.core.util.ModelUtil;
27  
28  /***
29   * Models a Object-Oriented class.
30   *
31   * @author Richard Easterling
32   */
33  @XmlRootElement
34  public class Class extends AbstractModel {
35  	
36  	public static final String[] EMPTY_STRING_ARRAY =  new String[0];
37      public static final String DEFAULT_MODEL_TYPE = "CLASS";
38      
39  //	protected String superClass;
40  //	protected Set<String> interfaces;
41  //	protected Map<String, String> imports;
42  	//protected List<Model> childModels;
43  	//protected List<Property> properties;
44  	protected String code;
45  	//protected String pluralName;
46  	//protected String packageName;
47  	
48      @Override
49      public void copyTo(Model targetModel) {
50          Class target = (Class)targetModel;
51          super.copyTo(target);
52  //        if (this.getSuperClass()!=null)
53  //            target.setSuperClass(this.getSuperClass());
54  //        if (this.getCode()!=null)
55  //            target.setCode(this.getCode());
56      }
57  
58      public Class() {
59          setModelType(DEFAULT_MODEL_TYPE);
60      }
61      
62      public Class(String name) {
63          this();
64          this.setName(name);
65      }
66      
67      @XmlTransient
68  	public List<Property> getProperties() {
69         List<Property> list = new ArrayList<Property>(getChildModels().size());
70          for(Model child : getChildModels())
71              list.add((Property)child);
72          return list;
73  	}
74  
75      public Property lookupProperty(String name) {
76          return (Property)ModelUtil.lookupChildByName(this, name, Property.class, false);
77      }
78  
79  //	public String getCode() {
80  //		return code;
81  //	}
82  //
83  //	public void setCode(String code) {
84  //		this.code = code;
85  //	}
86  //
87  //    @XmlTransient
88  //	public Set<String> getInterfaces() {
89  //		return interfaces;
90  //	}
91  //
92  //	public void setInterfaces(Set<String> interfaces) {
93  //		this.interfaces = interfaces;
94  //	}
95  //
96  //	public void addInterface(String _interface) {
97  //		if (interfaces==null)
98  //			interfaces = new HashSet<String>();
99  //		addImport(_interface);
100 //		interfaces.add(_interface);
101 //	}
102 //
103 //	public String getDelineatedInterfaces(String keyword) {
104 //		if (interfaces==null) {
105 //			return "";
106 //		} else {
107 //			List<String> interfacesList = new ArrayList<String>();
108 //			for(String fullClassName : interfaces) {
109 //				String decl = classDeclFormat(fullClassName);
110 //				interfacesList.add(decl);
111 //			}
112 //			String[] array = interfacesList.toArray(new String[interfacesList.size()]);
113 //			Arrays.sort(array);
114 //			StringBuilder sb = null;
115 //			for(String interfaceName : array) {
116 //				if (sb==null) {
117 //					sb = new StringBuilder(keyword).append(" ");
118 //				} else {
119 //					sb.append(", ");
120 //				}
121 //				sb.append(interfaceName);
122 //			}
123 //			return sb.toString();
124 //		}
125 //	}
126 //	
127 //	@XmlTransient
128 //	public Map<String, String> getImports() {
129 //		return imports;
130 //	}
131 //
132 //    @XmlTransient
133 //	public String[] getSortedImports() {
134 //		if (imports==null) {
135 //			return EMPTY_STRING_ARRAY;
136 //		} else {
137 //			List<String> validImports = new ArrayList<String>();
138 //			for(String shortClassName : imports.keySet()) {
139 //				String fullClassName = imports.get(shortClassName);
140 //				if (!shortClassName.equals(fullClassName)) {
141 //					//only add imports that were shortened
142 //					validImports.add(fullClassName);
143 //				}
144 //			}
145 //			String[] array = validImports.toArray(new String[validImports.size()]);
146 //			Arrays.sort(array);
147 //			return array;
148 //		}
149 //	}
150 //
151 //	public void setImports(Map<String, String> imports) {
152 //		this.imports = imports;
153 //	}
154 //
155 //	public String addImport(String _import) {
156 ////		if (_import==null || _import.startsWith("java.lang."))
157 ////			return _import;
158 //		if (imports==null)
159 //			imports = new HashMap<String, String>();
160 //		if (imports.get(_import)!=null) //exists in full name format
161 //			return _import;
162 //		String shortClassName = OOUtil.shortClassName(_import);
163 //		String existingLongClassName = imports.get(shortClassName);
164 //		if (existingLongClassName==null) {
165 //			imports.put(shortClassName, _import);
166 //		} else if ( ! _import.equals(existingLongClassName) ) {
167 //			//existing class with the same name, must store full name
168 //			imports.put(_import, _import);
169 //			return _import;
170 //		}
171 //		return shortClassName;
172 //	}
173 //
174 //	/***
175 //	 * Lookup how to declare class names (short or long format) based on defined imports.
176 //	 * @param fullClassName
177 //	 * @return short or full class name that should be used in code.
178 //	 */
179 //	public String classDeclFormat(String fullClassName) {
180 //		if (imports==null || imports.get(fullClassName)!=null) //exists in full name format
181 //			return fullClassName;
182 //		String shortClassName = OOUtil.shortClassName(fullClassName);
183 //		String storedNameDecl = (imports==null) ? null : imports.get(shortClassName);
184 //		return storedNameDecl!=null && storedNameDecl.equals(fullClassName) ? shortClassName : fullClassName;
185 //	}
186 //	
187 //    @XmlTransient
188 //	public String getSuperClass() {
189 //		return superClass;
190 //	}
191 //
192 //	public void setSuperClass(String superClass) {
193 //		this.superClass = superClass;
194 //	}
195 //
196 	public Reference toReference(Property prop) {
197 		return prop instanceof Reference ? (Reference)prop : null;
198 	}
199 
200     @Override
201     public int hashCode() {
202         final int PRIME = 31;
203         int result = 1;
204         result = PRIME * result + ((code == null) ? 0 : code.hashCode());
205 //        result = PRIME * result + ((interfaces == null) ? 0 : interfaces.hashCode());
206 //        result = PRIME * result + ((superClass == null) ? 0 : superClass.hashCode());
207         return result;
208     }
209 
210     @Override
211     public boolean equals(Object obj) {
212         if (this == obj)
213             return true;
214         if (!super.equals(obj))
215             return false;
216         if (getClass() != obj.getClass())
217             return false;
218         final Class other = (Class) obj;
219         if (code == null) {
220             if (other.code != null)
221                 return false;
222         } else if (!code.equals(other.code))
223             return false;
224 //        if (interfaces == null) {
225 //            if (other.interfaces != null)
226 //                return false;
227 //        } else if (!interfaces.equals(other.interfaces))
228 //            return false;
229 //        if (superClass == null) {
230 //            if (other.superClass != null)
231 //                return false;
232 //        } else if (!superClass.equals(other.superClass))
233 //            return false;
234         return true;
235     }
236     
237     
238 
239 }