001: /*
002: * Created on 11-May-2005
003: *
004: */
005: package com.jofti.cache.adapter;
006:
007: import com.jofti.api.NameSpaceKey;
008:
009: /**
010: *
011: *
012: *Used in the JBossCacheAdapter index. The wrapper provides comparableness and ordering for
013: *the JBoss Fqn key. Specifically, the namespace object is checked first. If they are of the same type and equal then the key is checked.
014: * Otherwise if both objects are of the same type and comparable then natual ordering occurs on these objects. However, if they are not the same type
015: * then ordering of the toString() method is taken (while this may not provide consistency
016: * across time, we do not care as this is reasonable to provide key ordering in the index - which is not visible to the caller).
017: * <p>
018: * Key Comparison is done in a similar manner.<p>
019: *
020: * @author Steve Woodcock
021: */
022: public class NameSpaceKeyWrapper implements Comparable, NameSpaceKey {
023:
024: /**
025: * @return Returns the key.
026: */
027: public Object getKey() {
028: return key;
029: }
030:
031: private static String KEY_DEFAULT = "KEY";
032:
033: protected Object nameSpace = null;
034:
035: protected Class nameSpaceClass;
036:
037: protected Object key = null;
038: protected Class keyClass = null;
039: int hashCode = 0;
040:
041: private String nameSpaceCompareString = null;
042:
043: /**
044: * Constructs a wrapper for a namespace and key object. Null keys are given the String
045: * value "KEY".
046: * @param nameSpace
047: * @param key
048: */
049: public NameSpaceKeyWrapper(Object nameSpace, Object key) {
050: this .nameSpace = nameSpace;
051: nameSpaceClass = this .nameSpace.getClass();
052: if (key != null) {
053: this .key = key;
054: keyClass = key.getClass();
055: } else {
056: key = KEY_DEFAULT;
057: keyClass = this .key.getClass();
058: }
059: this .nameSpaceCompareString = nameSpace.toString() + "." + key;
060: hashCode = nameSpace.hashCode() + key.hashCode();
061: }
062:
063: public NameSpaceKeyWrapper(Object nameSpace) {
064: this (nameSpace, KEY_DEFAULT);
065: }
066:
067: public int compareTo(Object o) {
068:
069: try {
070:
071: NameSpaceKeyWrapper temp = (NameSpaceKeyWrapper) o;
072:
073: // if the namespaces are the same type & comparable
074: if (nameSpaceClass.equals(temp.nameSpaceClass)
075: && nameSpace.equals(temp.nameSpace)) {
076: // the name spaces are equal
077: // compare the keys if they are comparable
078: if (keyClass.equals(temp.keyClass)
079: && key instanceof Comparable) {
080: return ((Comparable) key).compareTo(temp.key);
081: }
082: // otherwise we have mismatched key types - so devolve to String
083: return key.toString().compareTo(temp.key.toString());
084:
085: } else {
086: // do a compare to on String
087: if (nameSpaceClass.equals(temp.nameSpaceClass)
088: && nameSpace instanceof Comparable) {
089: return ((Comparable) nameSpace)
090: .compareTo((Comparable) temp.nameSpace);
091: } else {
092: return nameSpace.toString().compareTo(
093: temp.nameSpace.toString());
094: }
095:
096: }
097: } catch (Throwable t) {
098:
099: }
100: return -1;
101: }
102:
103: public boolean equals(Object o) {
104: try {
105: NameSpaceKeyWrapper temp = (NameSpaceKeyWrapper) o;
106: // see if name spaces equal
107: if (nameSpaceClass.equals(temp.nameSpaceClass)) {
108: // name spaces are equal
109: if (nameSpace.equals(temp.nameSpace)) {
110: // see if keys are equals
111: if (keyClass.equals(temp.keyClass)) {
112: return key.equals(temp.key);
113: }
114: }
115: }
116: } catch (Exception e) {
117:
118: }
119: return false;
120: }
121:
122: public int hashCode() {
123: return hashCode;
124: }
125:
126: public String toString() {
127: return nameSpaceCompareString;
128: }
129:
130: /* (non-Javadoc)
131: * @see com.jofti.core.NameSpaceKey#getNameSpace()
132: */
133: public Object getNameSpace() {
134: // TODO Auto-generated method stub
135: return nameSpace;
136: }
137:
138: protected void setNameSpace(Object nameSpace) {
139: this .nameSpace = nameSpace;
140: this .nameSpaceClass = nameSpace.getClass();
141: this .nameSpaceCompareString = nameSpace.toString() + "." + key;
142: }
143: }
|