KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > event > EventSubject


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.event;
22
23 import java.io.Serializable JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.commons.collections.map.ReferenceMap;
27 import org.apache.commons.lang.builder.HashCodeBuilder;
28
29 /**
30  * This class encapsulates the String that is used to identify the <em>subject</em> that
31  * a listener is interested in. Using plain Strings causes several severe problems:
32  * <ul>
33  * <li>it's easy to misspell a subject, leading to undesired behaviour at runtime that is
34  * hard to debug.</li>
35  * <li>in systems with many different subjects there is no safeguard for defining the
36  * same subject twice for different purposes. This is especially true in a distributed
37  * setting.
38  * </ul>
39  *
40  * @author Dirk Olmes
41  * @author Holger Hoffstaette
42  */

43 public class EventSubject implements Serializable JavaDoc {
44
45     // a Map that will allow the values to be GC'ed
46
private static Map JavaDoc _registeredSubjects = new ReferenceMap(
47             ReferenceMap.HARD,
48             ReferenceMap.WEAK);
49
50     // Subject identifier in the form "com.foo.bar/SubjectName"
51
private String JavaDoc _fullyQualifiedSubjectName;
52
53     /**
54      * Returns an event subject identified by the given owner and subject name.
55      *
56      * @param subjectOwner the Class used for uniquely identifying this subject
57      * @param subjectName a String used as name, e.g. "MyEventTopic"
58      * @throws IllegalArgumentException if subjectOwner/subjectName are <code>null</code>
59      * or subjectName is empty.
60      */

61     public static EventSubject getSubject(Class JavaDoc subjectOwner, String JavaDoc subjectName) {
62         if (subjectOwner == null) {
63             throw new IllegalArgumentException JavaDoc("Owner class must not be null.");
64         }
65
66         if ((subjectName == null) || (subjectName.length() == 0)) {
67             throw new IllegalArgumentException JavaDoc("Subject name must not be null or empty.");
68         }
69
70         String JavaDoc 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     /**
81      * Private constructor to force use of #getSubject(Class, String)
82      */

83     private EventSubject() {
84     }
85
86     /**
87      * Protected constructor for new subjects.
88      *
89      * @param fullSubjectName the name of the new subject to be created
90      */

91     protected EventSubject(String JavaDoc fullSubjectName) {
92         _fullyQualifiedSubjectName = fullSubjectName;
93     }
94
95     public boolean equals(Object JavaDoc 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 JavaDoc getSubjectName() {
109         return _fullyQualifiedSubjectName;
110     }
111
112     /**
113      * @return a String in the form <code>&lt;ClassName 0x123456&gt; SomeName</code>
114      * @see Object#toString()
115      */

116     public String JavaDoc toString() {
117         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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