OPENJPA-1050: Use generics for LifeCycleEventManager

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@823267 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-10-08 18:52:08 +00:00
parent 27e2c03fd2
commit 0d9bd741b3
1 changed files with 25 additions and 34 deletions

View File

@ -29,7 +29,6 @@ import java.util.Map;
import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.MetaDataDefaults; import org.apache.openjpa.meta.MetaDataDefaults;
import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.util.InvalidStateException; import org.apache.openjpa.util.InvalidStateException;
@ -47,6 +46,7 @@ import org.apache.openjpa.util.InvalidStateException;
* @since 0.3.3 * @since 0.3.3
* @nojavadoc * @nojavadoc
*/ */
@SuppressWarnings("serial")
public class LifecycleEventManager public class LifecycleEventManager
implements CallbackModes, Serializable { implements CallbackModes, Serializable {
@ -55,11 +55,12 @@ public class LifecycleEventManager
private static final Localizer _loc = Localizer.forPackage( private static final Localizer _loc = Localizer.forPackage(
LifecycleEventManager.class); LifecycleEventManager.class);
private Map _classListeners = null; // class -> listener list private Map<Class<?>, ListenerList> _classListeners = null;
private ListenerList _listeners = null; private ListenerList _listeners = null;
private List _addListeners = new LinkedList(); // odd-element: Listener even-element: Class[]
private List _remListeners = new LinkedList(); private List<Object> _addListeners = new LinkedList<Object>();
private List _exceps = new LinkedList(); private List<Object> _remListeners = new LinkedList<Object>();
private List<Exception> _exceps = new LinkedList<Exception>();
private boolean _firing = false; private boolean _firing = false;
private boolean _fail = false; private boolean _fail = false;
private boolean _failFast = false; private boolean _failFast = false;
@ -82,7 +83,7 @@ public class LifecycleEventManager
* Register a lifecycle listener for the given classes. If the classes * Register a lifecycle listener for the given classes. If the classes
* array is null, register for all classes. * array is null, register for all classes.
*/ */
public synchronized void addListener(Object listener, Class[] classes) { public synchronized void addListener(Object listener, Class<?>[] classes) {
if (listener == null) if (listener == null)
return; return;
if (classes != null && classes.length == 0) if (classes != null && classes.length == 0)
@ -101,7 +102,7 @@ public class LifecycleEventManager
} }
if (_classListeners == null) if (_classListeners == null)
_classListeners = new HashMap(); _classListeners = new HashMap<Class<?>, ListenerList>();
ListenerList listeners; ListenerList listeners;
for (int i = 0; i < classes.length; i++) { for (int i = 0; i < classes.length; i++) {
listeners = (ListenerList) _classListeners.get(classes[i]); listeners = (ListenerList) _classListeners.get(classes[i]);
@ -126,7 +127,7 @@ public class LifecycleEventManager
return; return;
if (_classListeners != null) { if (_classListeners != null) {
ListenerList listeners; ListenerList listeners;
for (Iterator itr = _classListeners.values().iterator(); for (Iterator<ListenerList> itr = _classListeners.values().iterator();
itr.hasNext();) { itr.hasNext();) {
listeners = (ListenerList) itr.next(); listeners = (ListenerList) itr.next();
listeners.remove(listener); listeners.remove(listener);
@ -236,16 +237,14 @@ public class LifecycleEventManager
ClassMetaData meta, int type) { ClassMetaData meta, int type) {
if (meta.getLifecycleMetaData().getIgnoreSystemListeners()) if (meta.getLifecycleMetaData().getIgnoreSystemListeners())
return false; return false;
if (fireEvent(null, source, null, type, _listeners, true, null) if (fireEvent(null, source, null, type, _listeners, true, null) == Boolean.TRUE)
== Boolean.TRUE)
return true; return true;
ListenerList system = meta.getRepository().getSystemListeners(); ListenerList system = meta.getRepository().getSystemListeners();
if (!system.isEmpty() && fireEvent(null, source, null, type, system, if (!system.isEmpty() && fireEvent(null, source, null, type, system,
true, null) == Boolean.TRUE) true, null) == Boolean.TRUE)
return true; return true;
if (_classListeners != null) { if (_classListeners != null) {
Class c = source == null ? meta.getDescribedType() : Class<?> c = source == null ? meta.getDescribedType() : source.getClass();
source.getClass();
do { do {
if (fireEvent(null, source, null, type, (ListenerList) if (fireEvent(null, source, null, type, (ListenerList)
_classListeners.get(c), true, null) == Boolean.TRUE) _classListeners.get(c), true, null) == Boolean.TRUE)
@ -271,7 +270,7 @@ public class LifecycleEventManager
ClassMetaData meta, int type) { ClassMetaData meta, int type) {
boolean reentrant = _firing; boolean reentrant = _firing;
_firing = true; _firing = true;
List exceptions = (reentrant) ? new LinkedList() : _exceps; List<Exception> exceptions = (reentrant) ? new LinkedList<Exception>() : _exceps;
MetaDataDefaults def = meta.getRepository().getMetaDataFactory(). MetaDataDefaults def = meta.getRepository().getMetaDataFactory().
getDefaults(); getDefaults();
@ -283,8 +282,7 @@ public class LifecycleEventManager
type, _listeners, false, exceptions); type, _listeners, false, exceptions);
if (_classListeners != null) { if (_classListeners != null) {
Class c = source == null ? meta.getDescribedType() : Class<?> c = source == null ? meta.getDescribedType() : source.getClass();
source.getClass();
do { do {
ev = (LifecycleEvent) fireEvent(ev, source, related, type, ev = (LifecycleEvent) fireEvent(ev, source, related, type,
(ListenerList) _classListeners.get(c), false, exceptions); (ListenerList) _classListeners.get(c), false, exceptions);
@ -315,10 +313,10 @@ public class LifecycleEventManager
_firing = false; _firing = false;
_fail = false; _fail = false;
if (!_addListeners.isEmpty()) if (!_addListeners.isEmpty())
for (Iterator itr = _addListeners.iterator(); itr.hasNext();) for (Iterator<Object> itr = _addListeners.iterator(); itr.hasNext();)
addListener(itr.next(), (Class[]) itr.next()); addListener(itr.next(), (Class[]) itr.next());
if (!_remListeners.isEmpty()) if (!_remListeners.isEmpty())
for (Iterator itr = _remListeners.iterator(); itr.hasNext();) for (Iterator<Object> itr = _remListeners.iterator(); itr.hasNext();)
removeListener(itr.next()); removeListener(itr.next());
_addListeners.clear(); _addListeners.clear();
_remListeners.clear(); _remListeners.clear();
@ -331,7 +329,7 @@ public class LifecycleEventManager
* Make callbacks, recording any exceptions in the given collection. * Make callbacks, recording any exceptions in the given collection.
*/ */
private void makeCallbacks(Object source, Object related, private void makeCallbacks(Object source, Object related,
ClassMetaData meta, int type, Collection exceptions) { ClassMetaData meta, int type, Collection<Exception> exceptions) {
// make lifecycle callbacks // make lifecycle callbacks
LifecycleCallbacks[] callbacks = meta.getLifecycleMetaData(). LifecycleCallbacks[] callbacks = meta.getLifecycleMetaData().
getCallbacks(type); getCallbacks(type);
@ -351,7 +349,7 @@ public class LifecycleEventManager
* listeners. The event may have already been constructed. * listeners. The event may have already been constructed.
*/ */
private Object fireEvent(LifecycleEvent ev, Object source, Object rel, private Object fireEvent(LifecycleEvent ev, Object source, Object rel,
int type, ListenerList listeners, boolean mock, List exceptions) { int type, ListenerList listeners, boolean mock, List<Exception> exceptions) {
if (listeners == null || !listeners.hasListeners(type)) if (listeners == null || !listeners.hasListeners(type))
return null; return null;
@ -365,9 +363,9 @@ public class LifecycleEventManager
responds = ((ListenerAdapter) listener).respondsTo(type); responds = ((ListenerAdapter) listener).respondsTo(type);
if (!responds) if (!responds)
continue; continue;
} else } else {
responds = false; responds = false;
}
try { try {
switch (type) { switch (type) {
case LifecycleEvent.BEFORE_CLEAR: case LifecycleEvent.BEFORE_CLEAR:
@ -490,14 +488,12 @@ public class LifecycleEventManager
break; break;
case LifecycleEvent.AFTER_PERSIST_PERFORMED: case LifecycleEvent.AFTER_PERSIST_PERFORMED:
if (responds || listener instanceof PostPersistListener) if (responds || listener instanceof PostPersistListener) {
{
if (mock) if (mock)
return Boolean.TRUE; return Boolean.TRUE;
if (ev == null) if (ev == null)
ev = new LifecycleEvent(source, rel, type); ev = new LifecycleEvent(source, rel, type);
((PostPersistListener) listener) ((PostPersistListener) listener).afterPersistPerformed(ev);
.afterPersistPerformed(ev);
} }
break; break;
case LifecycleEvent.BEFORE_UPDATE: case LifecycleEvent.BEFORE_UPDATE:
@ -510,8 +506,7 @@ public class LifecycleEventManager
if (type == LifecycleEvent.BEFORE_UPDATE) if (type == LifecycleEvent.BEFORE_UPDATE)
((UpdateListener) listener).beforeUpdate(ev); ((UpdateListener) listener).beforeUpdate(ev);
else else
((UpdateListener) listener) ((UpdateListener) listener).afterUpdatePerformed(ev);
.afterUpdatePerformed(ev);
} }
break; break;
case LifecycleEvent.AFTER_DELETE_PERFORMED: case LifecycleEvent.AFTER_DELETE_PERFORMED:
@ -520,14 +515,11 @@ public class LifecycleEventManager
return Boolean.TRUE; return Boolean.TRUE;
if (ev == null) if (ev == null)
ev = new LifecycleEvent(source, rel, type); ev = new LifecycleEvent(source, rel, type);
((PostDeleteListener) listener) ((PostDeleteListener) listener).afterDeletePerformed(ev);
.afterDeletePerformed(ev);
} }
break; break;
default: default:
throw new InvalidStateException( throw new InvalidStateException(_loc.get("unknown-lifecycle-event", Integer.toString(type)));
_loc.get("unknown-lifecycle-event",
Integer.toString(type)));
} }
} }
catch (Exception e) { catch (Exception e) {
@ -558,8 +550,7 @@ public class LifecycleEventManager
* Extended list that tracks what event types its elements care about. * Extended list that tracks what event types its elements care about.
* Maintains set semantics as well. * Maintains set semantics as well.
*/ */
public static class ListenerList public static class ListenerList extends ArrayList<Object> {
extends ArrayList {
private int _types = 0; private int _types = 0;