1 5 package com.jofti.cache.adapter; 6 7 8 import com.jofti.api.NameSpaceKey; 9 10 23 public class NameSpaceKeyWrapper implements Comparable , NameSpaceKey { 24 25 26 27 30 public Object getKey() { 31 return key; 32 } 33 34 private static String KEY_DEFAULT = "KEY"; 35 36 protected Object nameSpace = null; 37 38 protected Class nameSpaceClass; 39 40 protected Object key =null; 41 protected Class keyClass = null; 42 int hashCode =0; 43 44 private String nameSpaceCompareString = null; 45 46 52 public NameSpaceKeyWrapper(Object nameSpace, Object key){ 53 this.nameSpace = nameSpace; 54 nameSpaceClass = this.nameSpace.getClass(); 55 if (key != null){ 56 this.key = key; 57 keyClass = key.getClass(); 58 } else{ 59 key = KEY_DEFAULT; 60 keyClass = this.key.getClass(); 61 } 62 this.nameSpaceCompareString = nameSpace.toString() + "."+key; 63 hashCode =nameSpace.hashCode() + key.hashCode(); 64 } 65 66 public NameSpaceKeyWrapper(Object nameSpace){ 67 this(nameSpace, KEY_DEFAULT); 68 } 69 70 71 public int compareTo(Object o) { 72 73 74 try{ 75 76 NameSpaceKeyWrapper temp = (NameSpaceKeyWrapper)o; 77 78 79 if (nameSpaceClass.equals(temp.nameSpaceClass) 81 && nameSpace.equals(temp.nameSpace)) 82 { 83 if (keyClass.equals(temp.keyClass) 86 && key instanceof Comparable ){ 87 return ((Comparable )key).compareTo(temp.key); 88 } 89 return key.toString().compareTo(temp.key.toString()); 91 92 }else{ 93 if (nameSpaceClass.equals(temp.nameSpaceClass) 95 && nameSpace instanceof Comparable ) 96 { 97 return ((Comparable )nameSpace).compareTo((Comparable )temp.nameSpace); 98 }else{ 99 return nameSpace.toString().compareTo(temp.nameSpace.toString()); 100 } 101 102 } 103 } catch (Throwable t){ 104 105 } 106 return -1; 107 } 108 109 110 111 public boolean equals(Object o){ 112 try { 113 NameSpaceKeyWrapper temp = (NameSpaceKeyWrapper)o; 114 if (nameSpaceClass.equals(temp.nameSpaceClass)){ 116 if (nameSpace.equals(temp.nameSpace)){ 118 if (keyClass.equals(temp.keyClass)){ 120 return key.equals(temp.key); 121 } 122 } 123 } 124 } catch (Exception e){ 125 126 } 127 return false; 128 } 129 130 public int hashCode(){ 131 return hashCode; 132 } 133 134 public String toString(){ 135 return nameSpaceCompareString; 136 } 137 140 public Object getNameSpace() { 141 return nameSpace; 143 } 144 145 protected void setNameSpace(Object nameSpace){ 146 this.nameSpace = nameSpace; 147 this.nameSpaceClass = nameSpace.getClass(); 148 this.nameSpaceCompareString = nameSpace.toString() + "."+key; 149 } 150 } 151 | Popular Tags |