1 19 20 package org.netbeans.modules.xml.xam; 21 22 33 public abstract class AbstractReference<T extends Referenceable> implements Reference<T> { 34 35 private T referenced; 36 private Class <T> classType; 37 private AbstractComponent parent; 38 protected String refString; 39 40 47 public AbstractReference(T referenced, Class <T> referencedType, AbstractComponent parent) { 48 if (referenced == null) { 49 throw new IllegalArgumentException ("Referenced component null"); } 51 checkParentAndType(parent, referencedType); 52 this.referenced = referenced; 53 this.classType = referencedType; 54 this.parent = parent; 55 } 56 57 64 public AbstractReference(Class <T> referencedType, AbstractComponent parent, String ref){ 65 checkParentAndType(parent, referencedType); 66 this.refString = ref; 67 this.classType = referencedType; 68 this.parent = parent; 69 } 70 71 74 protected T getReferenced() { 75 return referenced; 76 } 77 80 protected void setReferenced(T referenced) { 81 this.referenced = referenced; 82 } 83 84 87 public Class <T> getType() { 88 return classType; 89 } 90 91 94 public boolean isBroken() { 95 try { 96 return get() == null; 97 } catch(IllegalStateException ise) { 98 referenced = null; 99 return false; 100 } 101 } 102 103 106 public boolean references(T target) { 107 return get() == target; 108 } 109 110 114 public String getRefString() { 115 return refString; 116 } 117 118 protected AbstractComponent getParent() { 119 return parent; 120 } 121 122 private void checkParentAndType(AbstractComponent parent, Class <T> classType) { 123 if (parent == null) { 124 throw new IllegalArgumentException ("parent == null"); } 126 if (classType == null) { 127 throw new IllegalArgumentException ("classType == null"); } 129 } 130 131 @Override 132 public boolean equals(Object obj) { 133 if (obj == null) return false; 134 if (obj instanceof AbstractReference) { 135 AbstractReference<T> that = (AbstractReference<T>) obj; 136 return refString.equals(that.getRefString()) && 137 parent.equals(that.parent) && 138 getType().equals(that.getType()); 139 } else { 140 return super.equals(obj); 141 } 142 } 143 144 @Override 145 public int hashCode() { 146 return parent.hashCode(); 147 } 148 149 } 150 | Popular Tags |