View Javadoc

1   /*
2    * Created on 21 Mar 2009
3    *
4    */
5   package org.sourceforge.jemm.database;
6   
7   import java.io.Serializable;
8   import java.util.Collections;
9   import java.util.HashSet;
10  import java.util.Map;
11  import java.util.Set;
12  import java.util.TreeMap;
13  
14  import org.sourceforge.jemm.util.JEMMType;
15  
16  /**
17   * Immutable class representing a class.
18   * 
19   * @author Rory Graves
20   * @author Paul Keeble
21   */
22  public class ClassInfo implements Serializable {
23  	private static final long serialVersionUID = 1L;
24  
25  	/** The formal classname of the class. */
26  	public final String className;
27  	
28  	/** Whether the given class type is a jemm type */
29  	public final boolean isType;
30  	
31  	/** An immutable set of the fields belonging to the class */
32  	public final Set<FieldInfo> fields;
33  
34  	/**
35  	 * Create a class ClassInfo for the given class. 
36  	 * @param className The name of the class.
37  	 * @param fields The field definitions for this class.
38  	 */
39  	public ClassInfo(String className,Set<FieldInfo> fields) {
40  		this(className,false,fields);
41  	}
42  
43  	/**
44  	 * Constructs a ClassInfo instance.
45  	 * 
46  	 * @param className The formal name of the class (e.g. com.foo.Moose).
47  	 * @param fields The set of fields declared by the class - all fields must have
48  	 *            	 unique names.
49  	 */
50  	private ClassInfo(String className,boolean isType,Set<FieldInfo> fields) {
51  		
52  		if(className == null)
53  			throw new IllegalArgumentException("class name cannot be null");
54  			
55  		this.isType = isType;
56  		this.className = className;
57  		if (fields == null)
58  			throw new IllegalArgumentException("class fields cannot be null");
59  
60  		this.fields = Collections
61  				.unmodifiableSet(new HashSet<FieldInfo>(fields));
62  	}
63  	
64  	/** Create a ClassInfo with the given class name and no fields defined.
65  	 * @param className The name of the class.
66  	 */
67  	public ClassInfo(String className) {
68  		this(className,true,Collections.<FieldInfo>emptySet());
69  	}
70  	
71  	/** Create a ClassInfo with the given class name and fields.
72  	 * @param className The name of the class.
73  	 * @param fields The fields of the class.
74  	 */
75  	public ClassInfo(String className,FieldInfo... fields) {
76  		this(className,false,convertToSet(fields));
77  	}
78  	
79  	/** Create a ClassInfo for the given class.
80  	 * @param classType The Class instance of the represented class.
81  	 */
82  	public ClassInfo(Class<? extends JEMMType> classType) {
83  		this(classType.getName(),true,Collections.<FieldInfo>emptySet());
84  	}
85  
86  	private static Set<FieldInfo> convertToSet(FieldInfo[] fields) {
87  		Set<FieldInfo> set = new HashSet<FieldInfo>();
88  		for (FieldInfo fieldInfo : fields) {
89  		    if(set.contains(fieldInfo))
90  	            throw new IllegalArgumentException("Duplicate FieldInfo entries not allowed");
91  			set.add(fieldInfo);
92  		}
93  		
94  		return set;
95  	}
96      
97      @Override
98  	public int hashCode() {
99  		final int prime = 31;
100 		int result = 1;
101 		result = prime * result
102 				+ ((className == null) ? 0 : className.hashCode());
103 		result = prime * result + ((fields == null) ? 0 : fields.hashCode());
104 		result = prime * result + (isType ? 1231 : 1237);
105 		return result;
106 	}
107 
108 	@Override
109 	public boolean equals(Object obj) {
110 		if (this == obj)
111 			return true;
112 		if (obj == null)
113 			return false;
114 		if (getClass() != obj.getClass())
115 			return false;
116 		ClassInfo other = (ClassInfo) obj;
117 		if(!className.equals(other.className))
118 			return false;
119 		if(!fields.equals(other.fields))
120 			return false;
121 		if (isType != other.isType)
122 			return false;
123 		return true;
124 	}
125 
126 	@Override 
127     public String toString() {
128         StringBuilder sb = new StringBuilder("ClassInfo(");
129         sb.append(className).append(",");
130         sb.append(isType).append(",");
131         sb.append("[");
132 
133         Map<String,FieldInfo> orderedFields = new TreeMap<String, FieldInfo>();
134         for (FieldInfo fieldInfo : fields)
135             orderedFields.put(fieldInfo.fieldName, fieldInfo);
136         
137         
138         boolean first = true;
139         for (FieldInfo fieldInfo : orderedFields.values()) {
140             sb.append(fieldInfo);
141             if(first) {
142                 sb.append(',');
143                 first = false;
144             }
145         }
146         sb.append("])");
147         return sb.toString();
148     }
149 }