001: package org.space4j.indexing;
002:
003: import java.util.*;
004: import java.io.*;
005: import java.lang.reflect.*;
006:
007: /**
008: * An object representing an Index in the system.<br>
009: * An index contains a name, a classname, and a set o attributes.<br>
010: * Index can be simple or compound.
011: */
012: public class Index implements Serializable {
013:
014: private String name;
015: private String classname;
016: private String[] attributes;
017:
018: private transient Method[] methods = null;
019:
020: /**
021: * Create a new Index.
022: * @param name The name of this index.
023: * @param klass The class of this index.
024: * @param attributes An array of attributes.
025: */
026: public Index(String name, Class klass, String[] attributes) {
027: this .name = name;
028: this .classname = klass.getName();
029: this .attributes = attributes;
030: }
031:
032: /**
033: * Return the name of this index.
034: * @return The name of the index.
035: */
036: public String getName() {
037: return name;
038: }
039:
040: /**
041: * Return the name of the class of this index.
042: * @return The name of the class.
043: */
044: public String getClassName() {
045: return classname;
046: }
047:
048: /**
049: * Return the attributes of this index.
050: * @return The attributes of the index.
051: */
052: public String[] getAttributes() {
053: return attributes;
054: }
055:
056: /**
057: * Check if this index contains a given attribute.
058: * @param attribute The attribute we are looking for.
059: * @return True if the index contains this attribute.
060: */
061: public boolean hasAttribute(String attribute) {
062: for (int i = 0; i < attributes.length; i++) {
063: if (attributes[i].equalsIgnoreCase(attribute))
064: return true;
065: }
066: return false;
067: }
068:
069: private void findMethods(Class klass) {
070:
071: // for security...
072: if (!klass.getName().equalsIgnoreCase(this .classname))
073: return;
074:
075: try {
076: methods = new Method[attributes.length];
077:
078: for (int i = 0; i < methods.length; i++) {
079: String name = getMethodName(attributes[i]);
080: methods[i] = klass.getDeclaredMethod(name, null);
081: }
082: } catch (Exception e) {
083: e.printStackTrace();
084: methods = null;
085: }
086: }
087:
088: private String getMethodName(String attribute) {
089: String firstletter = attribute.substring(0, 1);
090: StringBuffer sb = new StringBuffer("get");
091: sb.append(firstletter.toUpperCase());
092: sb.append(attribute.substring(1, attribute.length()));
093: return sb.toString();
094: }
095:
096: /**
097: * Get a key to be used by this index for a given object.
098: * @param value The object from which we want a key.
099: * @return A key for this object.
100: */
101: public Key getKey(Object value) {
102:
103: // Find methods if needed...
104: if (methods == null) {
105: synchronized (this ) {
106: if (methods == null)
107: findMethods(value.getClass());
108: }
109: }
110:
111: // If it is not possible to find methods return null...
112: if (methods == null)
113: return null;
114:
115: Key key = new Key();
116:
117: try {
118: for (int i = 0; i < methods.length; i++) {
119: Object obj = methods[i].invoke(value, null);
120: key.add(obj);
121: }
122: } catch (Exception e) {
123: e.printStackTrace();
124: return null;
125: }
126:
127: return key;
128: }
129:
130: public boolean equals(Object obj) {
131: if (!(obj instanceof Index))
132: return false;
133: Index indx = (Index) obj;
134: if (!indx.classname.equalsIgnoreCase(this .classname))
135: return false;
136: if (indx.attributes.length != this .attributes.length)
137: return false;
138: for (int i = 0; i < indx.attributes.length; i++) {
139: if (!indx.attributes[i]
140: .equalsIgnoreCase(this .attributes[i]))
141: return false;
142: }
143: return true;
144: }
145:
146: public int hashCode() {
147: StringBuffer sb = new StringBuffer();
148: sb.append(classname);
149: for (int i = 0; i < attributes.length; i++) {
150: sb.append(attributes[i]);
151: }
152: return sb.toString().hashCode();
153: }
154:
155: public String toString() {
156: StringBuffer sb = new StringBuffer();
157: sb.append("Index: ").append(classname);
158: sb.append(" (");
159: for (int i = 0; i < attributes.length; i++) {
160: sb.append(attributes[i]).append(",");
161: }
162: sb.deleteCharAt(sb.length() - 1);
163: sb.append(")");
164: return sb.toString();
165: }
166:
167: }
|