1 19 20 21 package org.apache.cayenne.event; 22 23 import java.io.Serializable ; 24 import java.util.Map ; 25 26 import org.apache.commons.collections.map.ReferenceMap; 27 import org.apache.commons.lang.builder.HashCodeBuilder; 28 29 43 public class EventSubject implements Serializable { 44 45 private static Map _registeredSubjects = new ReferenceMap( 47 ReferenceMap.HARD, 48 ReferenceMap.WEAK); 49 50 private String _fullyQualifiedSubjectName; 52 53 61 public static EventSubject getSubject(Class subjectOwner, String subjectName) { 62 if (subjectOwner == null) { 63 throw new IllegalArgumentException ("Owner class must not be null."); 64 } 65 66 if ((subjectName == null) || (subjectName.length() == 0)) { 67 throw new IllegalArgumentException ("Subject name must not be null or empty."); 68 } 69 70 String fullSubjectName = subjectOwner.getName() + "/" + subjectName; 71 EventSubject newSubject = (EventSubject) _registeredSubjects.get(fullSubjectName); 72 if (newSubject == null) { 73 newSubject = new EventSubject(fullSubjectName); 74 _registeredSubjects.put(newSubject.getSubjectName(), newSubject); 75 } 76 77 return newSubject; 78 } 79 80 83 private EventSubject() { 84 } 85 86 91 protected EventSubject(String fullSubjectName) { 92 _fullyQualifiedSubjectName = fullSubjectName; 93 } 94 95 public boolean equals(Object obj) { 96 if (obj instanceof EventSubject) { 97 return _fullyQualifiedSubjectName.equals(((EventSubject) obj) 98 .getSubjectName()); 99 } 100 101 return false; 102 } 103 104 public int hashCode() { 105 return new HashCodeBuilder(17, 3).append(_fullyQualifiedSubjectName).toHashCode(); 106 } 107 108 public String getSubjectName() { 109 return _fullyQualifiedSubjectName; 110 } 111 112 116 public String toString() { 117 StringBuffer buf = new StringBuffer (64); 118 119 buf.append("<"); 120 buf.append(this.getClass().getName()); 121 buf.append(" 0x"); 122 buf.append(Integer.toHexString(System.identityHashCode(this))); 123 buf.append("> "); 124 buf.append(_fullyQualifiedSubjectName); 125 126 return buf.toString(); 127 } 128 } 129 | Popular Tags |