Index: threaddemo/apps/index/Index.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/apps/index/Index.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/apps/index/Index.java 19 Aug 2003 21:39:25 -0000 1.3 +++ threaddemo/apps/index/Index.java 16 Dec 2005 18:17:59 -0000 1.4 @@ -46,7 +46,7 @@ * Values are occurrence counts. *

Must be called with lock held, and result may only be accessed with it held. */ - Map/**/ getData(); + Map getData(); /** * Add a listener to changes in the data. Index: threaddemo/apps/index/IndexApp.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/apps/index/IndexApp.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/apps/index/IndexApp.java 19 Aug 2003 21:39:25 -0000 1.3 +++ threaddemo/apps/index/IndexApp.java 16 Dec 2005 18:17:59 -0000 1.4 @@ -60,9 +68,9 @@ } private void refreshTable() { - final SortedMap/**/ data = (SortedMap)index.getLock().read(new LockAction() { - public Object run() { - return new TreeMap(index.getData()); + final SortedMap data = index.getLock().read(new LockAction>() { + public SortedMap run() { + return new TreeMap(index.getData()); } }); SwingUtilities.invokeLater(new Runnable() { @@ -72,9 +80,7 @@ for (int i = 0; i < rows; i++) { tableModel.removeRow(0); } - Iterator it = data.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = (Map.Entry)it.next(); + for (Map.Entry entry : data.entrySet()) { tableModel.addRow(new Object[] { entry.getKey(), entry.getValue(), Index: threaddemo/apps/index/IndexImpl.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/apps/index/IndexImpl.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- threaddemo/apps/index/IndexImpl.java 21 Aug 2003 22:28:10 -0000 1.5 +++ threaddemo/apps/index/IndexImpl.java 16 Dec 2005 18:17:59 -0000 1.6 @@ -37,12 +48,12 @@ private static final Logger logger = Logger.getLogger(IndexImpl.class.getName()); private final Phadhail root; - private final List/**/ listeners = new ArrayList(); + private final List listeners = new ArrayList(); private boolean running = false; - private final LinkedList/**/ toProcess = new LinkedList(); - private final Map/*>*/ processed = new /*Weak*/HashMap(); - private final Map/**/ domProviders2Phadhails = new WeakHashMap(); - private final Map/**/ phadhails2Parents = new WeakHashMap(); + private final LinkedList toProcess = new LinkedList(); + private final Map> processed = new /*Weak*/HashMap>(); + private final Map domProviders2Phadhails = new WeakHashMap(); + private final Map phadhails2Parents = new WeakHashMap(); public IndexImpl(Phadhail root) { this.root = root; @@ -52,13 +63,13 @@ return root.lock(); } - public Map getData() { + public Map getData() { assert getLock().canRead(); - Map/**/ data = (Map)processed.get(root); + Map data = processed.get(root); if (data != null) { return Collections.unmodifiableMap(data); } else { - return Collections.EMPTY_MAP; + return Collections.emptyMap(); } } @@ -79,16 +90,16 @@ } private void fireChange() { - ChangeListener[] ls; + List ls; synchronized (listeners) { if (listeners.isEmpty()) { return; } - ls = (ChangeListener[])listeners.toArray(new ChangeListener[listeners.size()]); + ls = new ArrayList(listeners); } ChangeEvent ev = new ChangeEvent(this); - for (int i = 0; i < ls.length; i++) { - ls[i].stateChanged(ev); + for (ChangeListener l : ls) { + l.stateChanged(ev); } } @@ -124,7 +135,7 @@ assert false : e; } } - next = (Phadhail)toProcess.removeFirst(); + next = toProcess.removeFirst(); } process(next); } @@ -141,7 +152,7 @@ processChildren(ph); } else { // Data, maybe. - final Map computed = compute(ph); + final Map computed = compute(ph); getLock().writeLater(new Runnable() { public void run() { processed.put(ph, computed); @@ -156,10 +167,8 @@ } private void processChildren(Phadhail ph) { - Iterator/**/ kids = ph.getChildren().iterator(); synchronized (toProcess) { - while (kids.hasNext()) { - Phadhail kid = (Phadhail)kids.next(); + for (Phadhail kid : ph.getChildren()) { phadhails2Parents.put(kid, ph); if (!toProcess.contains(kid)) { toProcess.add(kid); @@ -173,40 +182,33 @@ } private int count; - private Map compute(Phadhail ph) { + private Map compute(Phadhail ph) { assert getLock().canRead(); assert !ph.hasChildren(); - logger.log(Level.FINER, "Computing index for {0} [#{1}]", new Object[] {ph, new Integer(++count)}); - DomProvider p = (DomProvider)domProviders2Phadhails.get(ph); + logger.log(Level.FINER, "Computing index for {0} [#{1}]", new Object[] {ph, ++count}); + // XXX technically should listen to lookup changes... + DomProvider p = (DomProvider) PhadhailLookups.getLookup(ph).lookup(DomProvider.class); if (p == null) { - // XXX technically should listen to lookup changes... - p = (DomProvider)PhadhailLookups.getLookup(ph).lookup(DomProvider.class); - if (p == null) { - logger.finer("no DomProvider here"); - return Collections.EMPTY_MAP; - } - domProviders2Phadhails.put(p, ph); + logger.finer("no DomProvider here"); + return Collections.emptyMap(); } + domProviders2Phadhails.put(p, ph); Document d; try { d = p.getDocument(); } catch (IOException e) { logger.log(Level.FINE, "Parsing failed for {0}: {1}", new Object[] {ph.getName(), e.getMessage()}); - return Collections.EMPTY_MAP; + return Collections.emptyMap(); } // Wait till after p.getDocument(), since that will fire stateChanged // the first time it is called (not ready -> ready) p.addChangeListener(WeakListeners.change(this, p)); - Map/**/ m = new HashMap(); + Map m = new HashMap(); NodeList l = d.getElementsByTagName("*"); for (int i = 0; i < l.getLength(); i++) { String name = ((Element)l.item(i)).getTagName(); - Integer x = (Integer)m.get(name); - if (x == null) { - m.put(name, new Integer(1)); - } else { - m.put(name, new Integer(x.intValue() + 1)); - } + Integer old = m.get(name); + m.put(name, old != null ? old + 1 : 1); } logger.log(Level.FINER, "Parse succeeded for {0}", ph); logger.log(Level.FINEST, "Parse results: {0}", m); @@ -215,7 +217,7 @@ private void bubble(Phadhail ph) { assert getLock().canWrite(); - logger.log(Level.FINER, "bubble: {0} data size: {1}", new Object[] {ph, new Integer(processed.size())}); + logger.log(Level.FINER, "bubble: {0} data size: {1}", new Object[] {ph, processed.size()}); logger.log(Level.FINEST, "bubble: {0} data: {1}", new Object[] {ph, processed}); if (ph == root) { getLock().read(new Runnable() { @@ -227,23 +229,18 @@ Phadhail parent = (Phadhail)phadhails2Parents.get(ph); assert parent != null : ph; assert parent.hasChildren(); - Iterator/**/ kids = parent.getChildren().iterator(); - Map/**/ recalc = new HashMap(); - while (kids.hasNext()) { - Phadhail kid = (Phadhail)kids.next(); - Map/**/ subdata = (Map)processed.get(kid); + Map recalc = new HashMap(); + for (Phadhail kid : parent.getChildren()) { + Map subdata = processed.get(kid); if (subdata == null) { // OK, kid is simply not yet calculated, will bubble changes later. continue; } - Iterator it = subdata.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry e = (Map.Entry)it.next(); - String name = (String)e.getKey(); - Integer x1 = (Integer)e.getValue(); + for (Map.Entry e : subdata.entrySet()) { + String name = e.getKey(); + int x1 = e.getValue(); if (recalc.containsKey(name)) { - Integer x2 = (Integer)recalc.get(name); - recalc.put(name, new Integer(x1.intValue() + x2.intValue())); + recalc.put(name, x1 + recalc.get(name)); } else { recalc.put(name, x1); } @@ -280,7 +277,7 @@ public void stateChanged(ChangeEvent e) { DomProvider p = (DomProvider)e.getSource(); - Phadhail ph = (Phadhail)domProviders2Phadhails.get(p); + Phadhail ph = domProviders2Phadhails.get(p); assert ph != null; logger.log(Level.FINER, "stateChanged: {0}", ph); invalidate(ph); Index: threaddemo/apps/refactor/Refactor.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/apps/refactor/Refactor.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- threaddemo/apps/refactor/Refactor.java 21 Aug 2003 22:35:16 -0000 1.6 +++ threaddemo/apps/refactor/Refactor.java 16 Dec 2005 18:17:59 -0000 1.7 @@ -49,7 +63,7 @@ * @param app owner app, or null */ public static void run(final Phadhail root, Frame app) { - final Map/**/ data = collectData(root); + final Map data = collectData(root); final BoundedRangeModel progress = new DefaultBoundedRangeModel(); progress.setMinimum(0); progress.setMaximum(data.size()); @@ -75,18 +89,21 @@ dialog.getContentPane().add(cancel); dialog.pack(); dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); - dialog.show(); + dialog.setVisible(true); new Thread(new Runnable() { public void run() { - final Iterator/*>*/ it = data.entrySet().iterator(); - while (it.hasNext() && !cancelled[0]) { - Map.Entry e = (Map.Entry)it.next(); + Iterator> it = data.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry e = it.next(); + if (cancelled[0]) { + break; + } // Avoid keeping a reference to the old data, since we have // cached DomProvider's and such heavyweight stuff open on them: it.remove(); - final Phadhail ph = (Phadhail)e.getKey(); + final Phadhail ph = e.getKey(); logger.log(Level.FINER, "Refactoring {0}", ph); - final DomProvider p = (DomProvider)e.getValue(); + final DomProvider p = e.getValue(); ph.lock().read(new Runnable() { public void run() { final String path = ph.getPath(); @@ -125,18 +142,17 @@ }, "Refactoring").start(); } - private static Map/**/ collectData(final Phadhail root) { - return (Map)root.lock().read(new LockAction() { - private final Map data = new HashMap(); - public Object run() { + private static Map collectData(final Phadhail root) { + return root.lock().read(new LockAction>() { + private final Map data = new HashMap(); + public Map run() { collect(root); return data; } private void collect(Phadhail ph) { if (ph.hasChildren()) { - Iterator/**/ it = ph.getChildren().iterator(); - while (it.hasNext()) { - collect((Phadhail)it.next()); + for (Phadhail child : ph.getChildren()) { + collect(child); } } else { DomProvider p = (DomProvider)PhadhailLookups.getLookup(ph).lookup(DomProvider.class); @@ -157,15 +173,13 @@ return; } NodeList nl = doc.getElementsByTagName("*"); - List/**/ l = new ArrayList(); + final List l = new ArrayList(); for (int i = 0; i < nl.getLength(); i++) { - l.add(nl.item(i)); + l.add((Element) nl.item(i)); } - final Iterator it = l.iterator(); p.isolatingChange(new Runnable() { public void run() { - while (it.hasNext()) { - Element el = (Element)it.next(); + for (Element el : l) { String tagname = el.getTagName(); if (tagname.startsWith("tag-")) { int n = Integer.parseInt(tagname.substring(4)); Index: threaddemo/data/DomSupport.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/data/DomSupport.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- threaddemo/data/DomSupport.java 28 Aug 2003 20:54:32 -0000 1.11 +++ threaddemo/data/DomSupport.java 16 Dec 2005 18:17:58 -0000 1.12 @@ -45,17 +60,12 @@ * using the {@link TwoWaySupport} semantics. * @author Jesse Glick */ -public final class DomSupport extends DocumentParseSupport implements DomProvider, ErrorHandler, TwoWayListener, EntityResolver, EventListener { +public final class DomSupport extends DocumentParseSupport implements DomProvider, ErrorHandler, TwoWayListener, EntityResolver, EventListener { private static final Logger logger = Logger.getLogger(DomSupport.class.getName()); private final Phadhail ph; - private final Set listeners = new HashSet(); + private final Set listeners = new HashSet(); private boolean inIsolatingChange = false; private boolean madeIsolatedChanges; @@ -66,57 +76,49 @@ } public Document getDocument() throws IOException { - try { - return (Document)getLock().read(new LockExceptionAction() { - public Object run() throws IOException { - assert !inIsolatingChange; - try { - Object v = getValueBlocking(); - logger.log(Level.FINER, "getDocument: {0}", v); - return (Document)v; - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + return getLock().read(new LockExceptionAction() { + public Document run() throws IOException { + assert !inIsolatingChange; + try { + Document v = getValueBlocking(); + logger.log(Level.FINER, "getDocument: {0}", v); + return v; + } catch (InvocationTargetException e) { + throw (IOException) e.getCause(); } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + } + }); } public void setDocument(final Document d) throws IOException { if (d == null) throw new NullPointerException(); - try { - getLock().write(new LockExceptionAction() { - public Object run() throws IOException { - assert !inIsolatingChange; - Document old = (Document)getStaleValueNonBlocking(); - if (old != null && old != d) { - ((EventTarget)old).removeEventListener("DOMSubtreeModified", DomSupport.this, false); - ((EventTarget)d).addEventListener("DOMSubtreeModified", DomSupport.this, false); - } - try { - mutate(d); - return null; - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } catch (ClobberException e) { - throw (IOException)new IOException(e.toString()).initCause(e); - } + getLock().write(new LockExceptionAction() { + public Void run() throws IOException { + assert !inIsolatingChange; + Document old = (Document)getStaleValueNonBlocking(); + if (old != null && old != d) { + ((EventTarget)old).removeEventListener("DOMSubtreeModified", DomSupport.this, false); + ((EventTarget)d).addEventListener("DOMSubtreeModified", DomSupport.this, false); } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + try { + mutate(d); + return null; + } catch (InvocationTargetException e) { + throw (IOException) e.getCause(); + } catch (ClobberException e) { + throw (IOException) new IOException(e.toString()).initCause(e); + } + } + }); } public boolean isReady() { - return ((Boolean)getLock().read(new LockAction() { - public Object run() { + return getLock().read(new LockAction() { + public Boolean run() { assert !inIsolatingChange; - return getValueNonBlocking() != null ? Boolean.TRUE : Boolean.FALSE; + return getValueNonBlocking() != null; } - })).booleanValue(); + }); } public void start() { @@ -140,21 +142,21 @@ } private void fireChange() { - final ChangeListener[] ls; + final List ls; synchronized (listeners) { if (listeners.isEmpty()) { logger.log(Level.FINER, "DomSupport change with no listeners: {0}", ph); return; } - ls = (ChangeListener[])listeners.toArray(new ChangeListener[listeners.size()]); + ls = new ArrayList(listeners); } final ChangeEvent ev = new ChangeEvent(this); getLock().read(new Runnable() { public void run() { assert !inIsolatingChange; logger.log(Level.FINER, "DomSupport change: {0}", ph); - for (int i = 0; i < ls.length; i++) { - ls[i].stateChanged(ev); + for (ChangeListener l : ls) { + l.stateChanged(ev); } } }); @@ -164,7 +166,7 @@ return false; } - protected final DerivationResult doDerive(StyledDocument document, List documentEvents, Object oldValue) throws IOException { + protected final DerivationResult doDerive(StyledDocument document, List documentEvents, Document oldValue) throws IOException { assert !inIsolatingChange; // ignoring documentEvents logger.log(Level.FINER, "DomSupport doDerive: {0}", ph); @@ -193,13 +195,12 @@ } ((EventTarget)newValue).addEventListener("DOMSubtreeModified", this, false); // This impl does not compute structural diffs, so newValue == derivedDelta when modified: - return new DerivationResult(newValue, oldValue != null ? newValue : null); + return new DerivationResult(newValue, oldValue != null ? newValue : null); } - protected final Object doRecreate(StyledDocument document, Object oldValue, Object derivedDelta) throws IOException { + protected final Document doRecreate(StyledDocument document, Document oldValue, Document newDom) throws IOException { assert !inIsolatingChange; logger.log(Level.FINER, "DomSupport doRecreate: {0}", ph); // ignoring oldValue, returning same newDom ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { @@ -316,7 +317,7 @@ inIsolatingChange = false; logger.log(Level.FINER, "Finished isolatingChange on {0}; madeIsolatedChanges={1}", new Object[] {ph, madeIsolatedChanges ? Boolean.TRUE : Boolean.FALSE}); if (madeIsolatedChanges) { - Document d = (Document)getValueNonBlocking(); + Document d = getValueNonBlocking(); if (d != null) { try { setDocument(d); Index: threaddemo/data/PhadhailLookups.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/data/PhadhailLookups.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- threaddemo/data/PhadhailLookups.java 21 Aug 2003 22:32:48 -0000 1.6 +++ threaddemo/data/PhadhailLookups.java 16 Dec 2005 18:17:58 -0000 1.7 @@ -13,11 +13,12 @@ package threaddemo.data; -import java.lang.ref.*; -import java.util.*; +import java.util.Map; +import java.util.WeakHashMap; import org.openide.cookies.SaveCookie; import org.openide.util.Lookup; -import org.openide.util.lookup.*; +import org.openide.util.lookup.AbstractLookup; +import org.openide.util.lookup.InstanceContent; import threaddemo.locking.Lock; import threaddemo.model.Phadhail; @@ -35,11 +36,11 @@ /** no instances */ private PhadhailLookups() {} - private static final Map lookups = new WeakHashMap(); // Map + private static final Map lookups = new WeakHashMap(); // XXX rather than being synch, should be readAccess, and modified/saved should be writeAccess public static synchronized Lookup getLookup(Phadhail ph) { - Lookup l = (Lookup)lookups.get(ph); + PhadhailLookup l = lookups.get(ph); if (l == null) { l = new PhadhailLookup(ph); lookups.put(ph, l); Index: threaddemo/locking/EventHybridLock.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/EventHybridLock.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- threaddemo/locking/EventHybridLock.java 28 Aug 2003 20:54:33 -0000 1.2 +++ threaddemo/locking/EventHybridLock.java 16 Dec 2005 18:17:55 -0000 1.3 @@ -33,7 +36,7 @@ private EventHybridLock() {} - public Object read(LockAction action) { + public T read(LockAction action) { if (EventLock.isDispatchThread()) { // Fine, go ahead. if (semaphore == -1) { @@ -85,29 +88,43 @@ } } - private static LockAction convertExceptionAction(final LockExceptionAction action) { - return new LockAction() { - public Object run() { + private static final class Holder { + public final T object; + public final E exception; + public Holder(T object) { + this.object = object; + this.exception = null; + } + public Holder(E exception) { + this.object = null; + this.exception = exception; + } + } + + @SuppressWarnings("unchecked") + private static LockAction> convertExceptionAction(final LockExceptionAction action) { + return new LockAction>() { + public Holder run() { try { - return new Object[] {action.run()}; + return new Holder(action.run()); } catch (RuntimeException e) { throw e; } catch (Exception e) { - return e; + return new Holder((E) e); } } }; } - private static Object finishExceptionAction(Object result) throws InvocationTargetException { - if (result instanceof Object[]) { - return ((Object[])result)[0]; + private static T finishExceptionAction(Holder result) throws E { + if (result.exception != null) { + throw result.exception; } else { - throw new InvocationTargetException((Exception)result); + return result.object; } } - public Object read(LockExceptionAction action) throws InvocationTargetException { + public T read(LockExceptionAction action) throws E { return finishExceptionAction(read(convertExceptionAction(action))); } @@ -115,19 +132,19 @@ read(convertRunnable(action)); } - public Object write(final LockAction action) { + public T write(final LockAction action) { if (!EventLock.isDispatchThread()) { // Try again in AWT. if (canRead()) { throw new IllegalStateException("Cannot go R -> W"); // NOI18N } try { - final Object[] o = new Object[1]; + final List o = new ArrayList(1); final Error[] err = new Error[1]; EventLock.invokeAndWaitLowPriority(this, new Runnable() { public void run() { try { - o[0] = write(action); + o.add(write(action)); } catch (Error e) { err[0] = e; } @@ -136,7 +153,7 @@ if (err[0] != null) { throw err[0]; } - return o[0]; + return o.get(0); } catch (InterruptedException e) { throw new IllegalStateException(e.toString()); } catch (InvocationTargetException e) { @@ -187,7 +204,7 @@ } } - public Object write(LockExceptionAction action) throws InvocationTargetException { + public T write(LockExceptionAction action) throws E { return finishExceptionAction(write(convertExceptionAction(action))); } @@ -195,9 +212,9 @@ write(convertRunnable(action)); } - private static LockAction convertRunnable(final Runnable action) { - return new LockAction() { - public Object run() { + private static LockAction convertRunnable(final Runnable action) { + return new LockAction() { + public T run() { action.run(); return null; } @@ -260,6 +277,6 @@ /** * Set of readers which are running. */ - private final Set readers = new HashSet(); // Set + private final Set readers = new HashSet(); } Index: threaddemo/locking/EventLock.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/EventLock.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- threaddemo/locking/EventLock.java 20 Aug 2003 21:08:03 -0000 1.2 +++ threaddemo/locking/EventLock.java 16 Dec 2005 18:17:54 -0000 1.3 @@ -33,16 +34,16 @@ private EventLock() {} - public Object read(final LockAction action) { + public T read(final LockAction action) { if (isDispatchThread()) { return action.run(); } else { ReadWriteLock.enteringOther(this); - final Object[] result = new Object[1]; + final List result = new ArrayList(1); try { invokeAndWaitLowPriority(this, new Runnable() { public void run() { - result[0] = action.run(); + result.add(action.run()); } }); } catch (InterruptedException e) { @@ -57,28 +58,22 @@ throw new IllegalStateException(t.toString()); } } - return result[0]; + return result.get(0); } } - public Object read(final LockExceptionAction action) throws InvocationTargetException { + public T read(final LockExceptionAction action) throws E { if (isDispatchThread()) { - try { - return action.run(); - } catch (RuntimeException e) { - throw e; - } catch (Exception e) { - throw new InvocationTargetException(e); - } + return action.run(); } else { ReadWriteLock.enteringOther(this); final Throwable[] exc = new Throwable[1]; - final Object[] result = new Object[1]; + final List result = new ArrayList(1); try { invokeAndWaitLowPriority(this, new Runnable() { public void run() { try { - result[0] = action.run(); + result.add(action.run()); } catch (Throwable t) { exc[0] = t; } @@ -95,9 +90,11 @@ } else if (exc[0] instanceof Error) { throw (Error)exc[0]; } else if (exc[0] != null) { - throw new InvocationTargetException((Exception)exc[0]); + @SuppressWarnings("unchecked") + E e = (E) exc[0]; + throw e; } else { - return result[0]; + return result.get(0); } } } @@ -106,11 +103,11 @@ invokeLaterLowPriority(this, action); } - public Object write(LockAction action) { + public T write(LockAction action) { return read(action); } - public Object write(LockExceptionAction action) throws InvocationTargetException { + public T write(LockExceptionAction action) throws E { return read(action); } Index: threaddemo/locking/Lock.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/Attic/Lock.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/locking/Lock.java 23 Sep 2003 18:25:02 -0000 1.3 +++ threaddemo/locking/Lock.java 16 Dec 2005 18:17:55 -0000 1.4 @@ -110,31 +89,19 @@ * @param action the action to perform * @return the result of {@link LockAction#run} */ - Object write(LockAction action); + T write(LockAction action); - Object write(LockExceptionAction action) throws InvocationTargetException; + T write(LockExceptionAction action) throws E; /** * Run an action with write access and return no result. Index: threaddemo/locking/LockAction.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/LockAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- threaddemo/locking/LockAction.java 19 Aug 2003 21:39:29 -0000 1.1 +++ threaddemo/locking/LockAction.java 16 Dec 2005 18:17:55 -0000 1.2 @@ -17,13 +17,13 @@ * Unchecked exceptions will be propagated to calling code. * */ -public interface LockAction { +public interface LockAction { /** * Execute the action. * @return any object, then returned from {@link Lock#read(LockAction)} or {@link Lock#write(LockAction)} */ - public Object run(); + public T run(); } Index: threaddemo/locking/LockExceptionAction.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/LockExceptionAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- threaddemo/locking/LockExceptionAction.java 19 Aug 2003 21:39:29 -0000 1.1 +++ threaddemo/locking/LockExceptionAction.java 16 Dec 2005 18:17:54 -0000 1.2 @@ -14,21 +14,19 @@ package threaddemo.locking; -public interface LockExceptionAction { +public interface LockExceptionAction { - public Object run() throws Exception; + public T run() throws E; } Index: threaddemo/locking/MonitorLock.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/MonitorLock.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- threaddemo/locking/MonitorLock.java 20 Aug 2003 21:08:04 -0000 1.2 +++ threaddemo/locking/MonitorLock.java 16 Dec 2005 18:17:54 -0000 1.3 @@ -34,19 +32,13 @@ return Thread.holdsLock(monitor); } - public Object read(LockExceptionAction action) throws InvocationTargetException { + public T read(LockExceptionAction action) throws E { synchronized (monitor) { - try { - return action.run(); - } catch (RuntimeException e) { - throw e; - } catch (Exception e) { - throw new InvocationTargetException(e); - } + return action.run(); } } - public Object read(LockAction action) { + public T read(LockAction action) { synchronized (monitor) { return action.run(); } @@ -74,11 +66,11 @@ return canRead(); } - public Object write(LockAction action) { + public T write(LockAction action) { return read(action); } - public Object write(LockExceptionAction action) throws InvocationTargetException { + public T write(LockExceptionAction action) throws E { return read(action); } Index: threaddemo/locking/ReadWriteLock.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/locking/Attic/ReadWriteLock.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/locking/ReadWriteLock.java 28 Aug 2003 20:54:34 -0000 1.3 +++ threaddemo/locking/ReadWriteLock.java 16 Dec 2005 18:17:55 -0000 1.4 @@ -54,7 +60,7 @@ return name; } - public Object read(LockAction action) { + public T read(LockAction action) { enterRead(); try { return action.run(); @@ -63,14 +69,10 @@ } } - public Object read(LockExceptionAction action) throws InvocationTargetException { + public T read(LockExceptionAction action) throws E { enterRead(); try { return action.run(); - } catch (RuntimeException e) { - throw (RuntimeException)e; - } catch (Exception e) { - throw new InvocationTargetException(e); } finally { exitRead(); } @@ -84,7 +86,7 @@ }); } - public Object write(LockAction action) { + public T write(LockAction action) { enterWrite(); try { return action.run(); @@ -93,14 +95,10 @@ } } - public Object write(LockExceptionAction action) throws InvocationTargetException { + public T write(LockExceptionAction action) throws E { enterWrite(); try { return action.run(); - } catch (RuntimeException e) { - throw (RuntimeException)e; - } catch (Exception e) { - throw new InvocationTargetException(e); } finally { exitWrite(); } @@ -268,9 +266,7 @@ synchronized (lock()) { StringBuffer b = new StringBuffer("ReadWriteLock + private final Map threads = new WeakHashMap(); /** * Get information about the current thread. @@ -304,7 +300,7 @@ if (ti == null) { ti = new ThreadInfo(t); threads.put(t, ti); - ((Set)relevantLocks.get()).add(this); + relevantLocks.get().add(this); } return ti; } @@ -416,9 +412,9 @@ * lock, of course, but this information is represented by extraReaders and * extraWriters. */ - private static final ThreadLocal orderedLocksHeld = new ThreadLocal() { // ThreadLocal> - protected Object initialValue() { - return new ArrayList(); + private static final ThreadLocal> orderedLocksHeld = new ThreadLocal>() { + protected List initialValue() { + return new ArrayList(); } }; @@ -427,9 +423,11 @@ * with or without definite order (not counting EVENT) which have ever been * held or might have been held and which still exist. */ - private static final ThreadLocal relevantLocks = new ThreadLocal() { // ThreadLocal> - protected Object initialValue() { - return new WeakSet(); + private static final ThreadLocal> relevantLocks = new ThreadLocal>() { + protected Set initialValue() { + @SuppressWarnings("unchecked") + Set s = new WeakSet(); + return s; } }; @@ -440,7 +438,7 @@ * First checks that it is legal to enter. */ private static void entering(ThreadInfo ti) throws IllegalStateException { - List l = (List)orderedLocksHeld.get(); + List l = orderedLocksHeld.get(); // It is ordered, check it. ThreadInfo last; if (!l.isEmpty()) { Index: threaddemo/model/AbstractPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/AbstractPhadhail.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- threaddemo/model/AbstractPhadhail.java 19 Aug 2003 21:39:32 -0000 1.6 +++ threaddemo/model/AbstractPhadhail.java 16 Dec 2005 18:17:53 -0000 1.7 @@ -28,17 +40,17 @@ */ public abstract class AbstractPhadhail implements Phadhail { - private static final Map instances = new WeakHashMap(); // Map>> + private static final Map>> instances = new WeakHashMap>>(); protected interface Factory { AbstractPhadhail create(File f); } - private static Map instancesForFactory(Factory y) { // Map> + private static Map> instancesForFactory(Factory y) { assert Thread.holdsLock(AbstractPhadhail.class); - Map instances2 = (Map)instances.get(y); + Map> instances2 = instances.get(y); if (instances2 == null) { - instances2 = new WeakHashMap(); + instances2 = new WeakHashMap>(); instances.put(y, instances2); } return instances2; @@ -46,20 +58,20 @@ /** factory */ protected static synchronized AbstractPhadhail forFile(File f, Factory y) { - Map instances2 = instancesForFactory(y); - Reference r = (Reference)instances2.get(f); - AbstractPhadhail ph = (r != null) ? (AbstractPhadhail)r.get() : null; + Map> instances2 = instancesForFactory(y); + Reference r = instances2.get(f); + AbstractPhadhail ph = (r != null) ? r.get() : null; if (ph == null) { // XXX could also yield lock while calling create, but don't bother ph = y.create(f); - instances2.put(f, new WeakReference(ph)); + instances2.put(f, new WeakReference(ph)); } return ph; } private File f; - private List listeners = null; // List - private Reference kids; // Reference> + private List listeners = null; + private Reference> kids; private static boolean firing = false; protected AbstractPhadhail(File f) { @@ -69,11 +81,11 @@ /** factory to create new instances of this class; should be a constant */ protected abstract Factory factory(); - public List getChildren() { + public List getChildren() { assert lock().canRead(); - List phs = null; // List + List phs = null; if (kids != null) { - phs = (List)kids.get(); + phs = kids.get(); } if (phs == null) { // Need to (re)calculate the children. @@ -82,14 +94,14 @@ Arrays.sort(fs); phs = new ChildrenList(fs); } else { - phs = Collections.EMPTY_LIST; + phs = Collections.emptyList(); } - kids = new WeakReference(phs); + kids = new WeakReference>(phs); } return phs; } - private final class ChildrenList extends AbstractList { + private final class ChildrenList extends AbstractList { private final File[] files; private final Phadhail[] kids; public ChildrenList(File[] files) { @@ -98,7 +110,7 @@ } // These methods need not be called with the read lock held // (see Phadhail.getChildren Javadoc). - public Object get(int i) { + public Phadhail get(int i) { Phadhail ph = kids[i]; if (ph == null) { ph = forFile(files[i], factory()); @@ -133,7 +145,7 @@ public final void addPhadhailListener(PhadhailListener l) { synchronized (LISTENER_LOCK) { if (listeners == null) { - listeners = new ArrayList(); + listeners = new ArrayList(); } listeners.add(l); } @@ -153,7 +165,7 @@ private final PhadhailListener[] listeners() { synchronized (LISTENER_LOCK) { if (listeners != null) { - return (PhadhailListener[])listeners.toArray(new PhadhailListener[listeners.size()]); + return listeners.toArray(new PhadhailListener[listeners.size()]); } else { return null; } @@ -168,8 +180,8 @@ firing = true; try { PhadhailEvent ev = PhadhailEvent.create(AbstractPhadhail.this); - for (int i = 0; i < l.length; i++) { - l[i].childrenChanged(ev); + for (PhadhailListener listener : l) { + listener.childrenChanged(ev); } } finally { firing = false; @@ -187,8 +199,8 @@ firing = true; try { PhadhailNameEvent ev = PhadhailNameEvent.create(AbstractPhadhail.this, oldName, newName); - for (int i = 0; i < l.length; i++) { - l[i].nameChanged(ev); + for (PhadhailListener listener : l) { + listener.nameChanged(ev); } } finally { firing = false; @@ -212,19 +224,18 @@ File oldFile = f; f = newFile; synchronized (AbstractPhadhail.class) { - Map instances2 = instancesForFactory(factory()); + Map> instances2 = instancesForFactory(factory()); instances2.remove(oldFile); - instances2.put(newFile, new WeakReference(this)); + instances2.put(newFile, new WeakReference(this)); } fireNameChanged(oldName, nue); if (hasChildren()) { // Fire changes in path of children too. - List recChildren = new ArrayList(100); // List + List recChildren = new ArrayList(100); String prefix = oldFile.getAbsolutePath() + File.separatorChar; synchronized (AbstractPhadhail.class) { - Iterator it = instancesForFactory(factory()).values().iterator(); - while (it.hasNext()) { - AbstractPhadhail ph = (AbstractPhadhail)((Reference)it.next()).get(); + for (Reference r : instancesForFactory(factory()).values()) { + AbstractPhadhail ph = r.get(); if (ph != null && ph != this && ph.getPath().startsWith(prefix)) { recChildren.add(ph); } @@ -232,9 +243,8 @@ } // Do the notification after traversing the instances map, since // we cannot mutate the map while an iterator is active. - Iterator it = recChildren.iterator(); - while (it.hasNext()) { - ((AbstractPhadhail)it.next()).parentRenamed(oldFile, newFile); + for (AbstractPhadhail ph : recChildren) { + ph.parentRenamed(oldFile, newFile); } } } @@ -249,9 +259,9 @@ File oldFile = f; f = new File(prefix + suffix); synchronized (AbstractPhadhail.class) { - Map instances2 = instancesForFactory(factory()); + Map> instances2 = instancesForFactory(factory()); instances2.remove(oldFile); - instances2.put(f, new WeakReference(this)); + instances2.put(f, new WeakReference(this)); } fireNameChanged(null, null); } Index: threaddemo/model/BufferedPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/BufferedPhadhail.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- threaddemo/model/BufferedPhadhail.java 28 Aug 2003 20:54:34 -0000 1.11 +++ threaddemo/model/BufferedPhadhail.java 16 Dec 2005 18:17:53 -0000 1.12 @@ -24,15 +31,15 @@ */ final class BufferedPhadhail implements Phadhail, PhadhailListener { - private static final Map instances = new WeakHashMap(); // Map> + private static final Map> instances = new WeakHashMap>(); public static Phadhail forPhadhail(Phadhail ph) { if (ph.hasChildren() && !(ph instanceof BufferedPhadhail)) { - Reference r = (Reference)instances.get(ph); - BufferedPhadhail bph = (r != null) ? (BufferedPhadhail)r.get() : null; + Reference r = instances.get(ph); + BufferedPhadhail bph = (r != null) ? r.get() : null; if (bph == null) { bph = new BufferedPhadhail(ph); - instances.put(ph, new WeakReference(bph)); + instances.put(ph, new WeakReference(bph)); } return bph; } else { @@ -41,36 +48,36 @@ } private final Phadhail ph; - private Reference kids; // Reference> - private List listeners = null; // List + private Reference> kids; + private List listeners = null; private BufferedPhadhail(Phadhail ph) { this.ph = ph; } - public List getChildren() { - List phs = null; // List + public List getChildren() { + List phs = null; if (kids != null) { - phs = (List)kids.get(); + phs = kids.get(); } if (phs == null) { // Need to (re)calculate the children. phs = new BufferedChildrenList(ph.getChildren()); - kids = new WeakReference(phs); + kids = new WeakReference>(phs); } return phs; } - private static final class BufferedChildrenList extends AbstractList { - private final List orig; // List + private static final class BufferedChildrenList extends AbstractList { + private final List orig; private final Phadhail[] kids; - public BufferedChildrenList(List orig) { + public BufferedChildrenList(List orig) { this.orig = orig; kids = new Phadhail[orig.size()]; } - public Object get(int i) { + public Phadhail get(int i) { if (kids[i] == null) { - kids[i] = forPhadhail((Phadhail)orig.get(i)); + kids[i] = forPhadhail(orig.get(i)); } return kids[i]; } @@ -94,7 +101,7 @@ public void addPhadhailListener(PhadhailListener l) { if (listeners == null) { ph.addPhadhailListener(this); - listeners = new ArrayList(); + listeners = new ArrayList(); } listeners.add(l); } @@ -138,9 +145,8 @@ kids = null; if (listeners != null) { PhadhailEvent ev2 = PhadhailEvent.create(this); - Iterator it = listeners.iterator(); - while (it.hasNext()) { - ((PhadhailListener)it.next()).childrenChanged(ev2); + for (PhadhailListener l : listeners) { + l.childrenChanged(ev2); } } } @@ -148,9 +154,8 @@ public void nameChanged(PhadhailNameEvent ev) { if (listeners != null) { PhadhailNameEvent ev2 = PhadhailNameEvent.create(this, ev.getOldName(), ev.getNewName()); - Iterator it = listeners.iterator(); - while (it.hasNext()) { - ((PhadhailListener)it.next()).nameChanged(ev2); + for (PhadhailListener l : listeners) { + l.nameChanged(ev2); } } } Index: threaddemo/model/EventHybridLockedPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/EventHybridLockedPhadhail.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- threaddemo/model/EventHybridLockedPhadhail.java 20 Aug 2003 21:08:06 -0000 1.4 +++ threaddemo/model/EventHybridLockedPhadhail.java 16 Dec 2005 18:17:53 -0000 1.5 @@ -28,7 +29,7 @@ */ final class EventHybridLockedPhadhail extends AbstractPhadhail { - private static final Factory FACTORY = new Factory() { + private static final AbstractPhadhail.Factory FACTORY = new AbstractPhadhail.Factory() { public AbstractPhadhail create(File f) { return new EventHybridLockedPhadhail(f); } @@ -46,111 +47,87 @@ return FACTORY; } - public List getChildren() { - return (List)Locks.eventHybrid().read(new LockAction() { - public Object run() { + public List getChildren() { + return Locks.eventHybrid().read(new LockAction>() { + public List run() { return EventHybridLockedPhadhail.super.getChildren(); } }); } public String getName() { - return (String)Locks.eventHybrid().read(new LockAction() { - public Object run() { + return Locks.eventHybrid().read(new LockAction() { + public String run() { return EventHybridLockedPhadhail.super.getName(); } }); } public String getPath() { - return (String)Locks.eventHybrid().read(new LockAction() { - public Object run() { + return Locks.eventHybrid().read(new LockAction() { + public String run() { return EventHybridLockedPhadhail.super.getPath(); } }); } public boolean hasChildren() { - return ((Boolean)Locks.eventHybrid().read(new LockAction() { - public Object run() { - return EventHybridLockedPhadhail.super.hasChildren() ? Boolean.TRUE : Boolean.FALSE; + return Locks.eventHybrid().read(new LockAction() { + public Boolean run() { + return EventHybridLockedPhadhail.super.hasChildren(); } - })).booleanValue(); + }); } public void rename(final String nue) throws IOException { - try { - Locks.eventHybrid().write(new LockExceptionAction() { - public Object run() throws IOException { - EventHybridLockedPhadhail.super.rename(nue); - return null; - } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + Locks.eventHybrid().write(new LockExceptionAction() { + public Void run() throws IOException { + EventHybridLockedPhadhail.super.rename(nue); + return null; + } + }); } public Phadhail createContainerPhadhail(final String name) throws IOException { - try { - return (Phadhail)Locks.eventHybrid().write(new LockExceptionAction() { - public Object run() throws IOException { - return EventHybridLockedPhadhail.super.createContainerPhadhail(name); - } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + return Locks.eventHybrid().write(new LockExceptionAction() { + public Phadhail run() throws IOException { + return EventHybridLockedPhadhail.super.createContainerPhadhail(name); + } + }); } public Phadhail createLeafPhadhail(final String name) throws IOException { - try { - return (Phadhail)Locks.eventHybrid().write(new LockExceptionAction() { - public Object run() throws IOException { - return EventHybridLockedPhadhail.super.createLeafPhadhail(name); - } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + return Locks.eventHybrid().write(new LockExceptionAction() { + public Phadhail run() throws IOException { + return EventHybridLockedPhadhail.super.createLeafPhadhail(name); + } + }); } public void delete() throws IOException { - try { - Locks.eventHybrid().write(new LockExceptionAction() { - public Object run() throws IOException { - EventHybridLockedPhadhail.super.delete(); - return null; - } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + Locks.eventHybrid().write(new LockExceptionAction() { + public Void run() throws IOException { + EventHybridLockedPhadhail.super.delete(); + return null; + } + }); } public InputStream getInputStream() throws IOException { - try { - return (InputStream)Locks.eventHybrid().read(new LockExceptionAction() { - public Object run() throws IOException { - return EventHybridLockedPhadhail.super.getInputStream(); - } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + return Locks.eventHybrid().read(new LockExceptionAction() { + public InputStream run() throws IOException { + return EventHybridLockedPhadhail.super.getInputStream(); + } + }); } public OutputStream getOutputStream() throws IOException { // See comments in AbstractPhadhail.getOutputStream. - try { - return (OutputStream)Locks.eventHybrid().read(new LockExceptionAction() { - public Object run() throws IOException { - return EventHybridLockedPhadhail.super.getOutputStream(); - } - }); - } catch (InvocationTargetException e) { - throw (IOException)e.getCause(); - } + return Locks.eventHybrid().read(new LockExceptionAction() { + public OutputStream run() throws IOException { + return EventHybridLockedPhadhail.super.getOutputStream(); + } + }); } public Lock lock() { Index: threaddemo/model/LockedPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/LockedPhadhail.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- threaddemo/model/LockedPhadhail.java 20 Aug 2003 21:08:07 -0000 1.11 +++ threaddemo/model/LockedPhadhail.java 16 Dec 2005 18:17:52 -0000 1.12 @@ -33,7 +35,7 @@ Locks.readWrite("LP", PLOCK, 0); } - private static final Factory FACTORY = new Factory() { + private static final AbstractPhadhail.Factory FACTORY = new AbstractPhadhail.Factory() { public AbstractPhadhail create(File f) { return new LockedPhadhail(f); } @@ -51,7 +53,7 @@ return FACTORY; } - public List getChildren() { + public List getChildren() { PLOCK.enterRead(); try { return super.getChildren(); Index: threaddemo/model/MonitoredPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/MonitoredPhadhail.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/model/MonitoredPhadhail.java 20 Aug 2003 21:08:07 -0000 1.3 +++ threaddemo/model/MonitoredPhadhail.java 16 Dec 2005 18:17:52 -0000 1.4 @@ -31,7 +33,7 @@ private static final Object LOCK = new LOCK(); private static final Lock MLOCK = Locks.monitor(LOCK, 0); - private static final Factory FACTORY = new Factory() { + private static final AbstractPhadhail.Factory FACTORY = new AbstractPhadhail.Factory() { public AbstractPhadhail create(File f) { return new MonitoredPhadhail(f); } @@ -49,7 +51,7 @@ return FACTORY; } - public List getChildren() { + public List getChildren() { synchronized (LOCK) { return super.getChildren(); } Index: threaddemo/model/Phadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/Phadhail.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- threaddemo/model/Phadhail.java 21 Aug 2003 22:35:17 -0000 1.7 +++ threaddemo/model/Phadhail.java 16 Dec 2005 18:17:53 -0000 1.8 @@ -44,13 +44,12 @@ - List getChildren(); + List getChildren(); /** delete this phadhail (must not have children) */ void delete() throws IOException; Index: threaddemo/model/SpunPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/SpunPhadhail.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- threaddemo/model/SpunPhadhail.java 28 Aug 2003 20:54:35 -0000 1.13 +++ threaddemo/model/SpunPhadhail.java 16 Dec 2005 18:17:53 -0000 1.14 @@ -35,17 +40,17 @@ } }; - private static final Map instances = new WeakHashMap(); // Map> + private static final Map> instances = new WeakHashMap>(); /** factory */ public static Phadhail forPhadhail(Phadhail _ph) { assert EventQueue.isDispatchThread(); - Reference r = (Reference)instances.get(_ph); - Phadhail ph = (r != null) ? (Phadhail)r.get() : null; + Reference r = instances.get(_ph); + Phadhail ph = (r != null) ? r.get() : null; if (ph == null) { Spin spin = new SpunPhadhail(_ph); ph = BufferedPhadhail.forPhadhail((Phadhail)spin.getProxy()); - instances.put(_ph, new WeakReference(ph)); + instances.put(_ph, new WeakReference(ph)); } return ph; } @@ -89,8 +94,9 @@ return forPhadhail((Phadhail)result); } else if (result instanceof List) { // I.e. from getChildren(). Need to wrap result phadhails. - List phs = (List)result; // List - return new SpunChildrenList(phs); + @SuppressWarnings("unchecked") + List l = (List) result; + return new SpunChildrenList(l); } else { // Just pass on the call. return result; @@ -98,17 +104,17 @@ } } - private static final class SpunChildrenList extends AbstractList { - private final List orig; // List + private static final class SpunChildrenList extends AbstractList { + private final List orig; private final Phadhail[] kids; - public SpunChildrenList(List orig) { + public SpunChildrenList(List orig) { this.orig = orig; kids = new Phadhail[orig.size()]; } - public Object get(int i) { + public Phadhail get(int i) { assert EventQueue.isDispatchThread(); if (kids[i] == null) { - kids[i] = forPhadhail((Phadhail)orig.get(i)); + kids[i] = forPhadhail(orig.get(i)); } return kids[i]; } Index: threaddemo/model/SwungPhadhail.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/SwungPhadhail.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- threaddemo/model/SwungPhadhail.java 21 Aug 2003 22:33:30 -0000 1.6 +++ threaddemo/model/SwungPhadhail.java 16 Dec 2005 18:17:54 -0000 1.7 @@ -42,16 +50,16 @@ private static final Logger logger = Logger.getLogger(SwungPhadhail.class.getName()); - private static final Map instances = new WeakHashMap(); // Map> + private static final Map> instances = new WeakHashMap>(); /** factory */ public static Phadhail forPhadhail(Phadhail _ph) { assert EventQueue.isDispatchThread(); - Reference r = (Reference)instances.get(_ph); - Phadhail ph = (r != null) ? (Phadhail)r.get() : null; + Reference r = instances.get(_ph); + Phadhail ph = (r != null) ? r.get() : null; if (ph == null) { ph = new SwungPhadhail(_ph); - instances.put(_ph, new WeakReference(ph)); + instances.put(_ph, new WeakReference(ph)); } return ph; } @@ -60,10 +68,10 @@ private String name = null; private String path = null; private boolean computingName = false; - private List children = null; // List + private List children = null; private boolean computingChildren = false; private Boolean leaf = null; - private List listeners = null; // List + private List listeners = null; private SwungPhadhail(Phadhail ph) { this.ph = ph; @@ -74,9 +82,7 @@ // XXX synch on listeners to get them, then release if (listeners != null) { PhadhailNameEvent ev = PhadhailNameEvent.create(this, null, null); - Iterator it = listeners.iterator(); - while (it.hasNext()) { - PhadhailListener l = (PhadhailListener)it.next(); + for (PhadhailListener l : listeners) { logger.log(Level.FINER, "fireNameChanged for {0} to {1}", new Object[] {this, l}); l.nameChanged(ev); } @@ -123,23 +129,15 @@ private Phadhail createPhadhail(final String name, final boolean container) throws IOException { assert EventQueue.isDispatchThread(); - Phadhail orig; - try { - orig = (Phadhail)Worker.block(new LockExceptionAction() { - public Object run() throws IOException { - if (container) { - return ph.createContainerPhadhail(name); - } else { - return ph.createLeafPhadhail(name); - } + return forPhadhail(Worker.block(new LockExceptionAction() { + public Phadhail run() throws IOException { + if (container) { + return ph.createContainerPhadhail(name); + } else { + return ph.createLeafPhadhail(name); } - }); - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new Error(e); - } - return forPhadhail(orig); + } + })); } public Phadhail createContainerPhadhail(String name) throws IOException { @@ -152,34 +150,22 @@ public void rename(final String nue) throws IOException { assert EventQueue.isDispatchThread(); - try { - Worker.block(new LockExceptionAction() { - public Object run() throws IOException { - ph.rename(nue); - return null; - } - }); - } catch (IOException e) { - throw e; - } catch (Exception e) { - assert false : e; - } + Worker.block(new LockExceptionAction() { + public Void run() throws IOException { + ph.rename(nue); + return null; + } + }); } public void delete() throws IOException { assert EventQueue.isDispatchThread(); - try { - Worker.block(new LockExceptionAction() { - public Object run() throws IOException { - ph.delete(); - return null; - } - }); - } catch (IOException e) { - throw e; - } catch (Exception e) { - assert false : e; - } + Worker.block(new LockExceptionAction() { + public Void run() throws IOException { + ph.delete(); + return null; + } + }); } private void fireChildrenChanged() { @@ -188,14 +174,13 @@ if (listeners != null) { logger.finer("fireChildrenChanged"); PhadhailEvent ev = PhadhailEvent.create(this); - Iterator it = listeners.iterator(); - while (it.hasNext()) { - ((PhadhailListener)it.next()).childrenChanged(ev); + for (PhadhailListener l : listeners) { + l.childrenChanged(ev); } } } - public List getChildren() { + public List getChildren() { assert EventQueue.isDispatchThread(); if (children != null) { return children; @@ -204,7 +189,7 @@ computingChildren = true; Worker.start(new Runnable() { public void run() { - final List ch = ph.getChildren(); + final List ch = ph.getChildren(); SwingUtilities.invokeLater(new Runnable() { public void run() { children = new SwungChildrenList(ch); @@ -215,21 +200,21 @@ } }); } - return Collections.EMPTY_LIST; + return Collections.emptyList(); } } - private static final class SwungChildrenList extends AbstractList { - private final List orig; // List + private static final class SwungChildrenList extends AbstractList { + private final List orig; private final Phadhail[] kids; - public SwungChildrenList(List orig) { + public SwungChildrenList(List orig) { this.orig = orig; kids = new Phadhail[orig.size()]; } - public Object get(int i) { + public Phadhail get(int i) { assert EventQueue.isDispatchThread(); if (kids[i] == null) { - kids[i] = forPhadhail((Phadhail)orig.get(i)); + kids[i] = forPhadhail(orig.get(i)); } return kids[i]; } @@ -241,32 +226,20 @@ public InputStream getInputStream() throws IOException { assert EventQueue.isDispatchThread(); - try { - return (InputStream)Worker.block(new LockExceptionAction() { - public Object run() throws IOException { - return ph.getInputStream(); - } - }); - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new Error(e); - } + return Worker.block(new LockExceptionAction() { + public InputStream run() throws IOException { + return ph.getInputStream(); + } + }); } public OutputStream getOutputStream() throws IOException { assert EventQueue.isDispatchThread(); - try { - return (OutputStream)Worker.block(new LockExceptionAction() { - public Object run() throws IOException { - return ph.getOutputStream(); - } - }); - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new Error(e); - } + return Worker.block(new LockExceptionAction() { + public OutputStream run() throws IOException { + return ph.getOutputStream(); + } + }); } public boolean hasChildren() { @@ -274,10 +247,10 @@ logger.log(Level.FINER, "hasChildren on {0}", this); if (leaf == null) { logger.finer("not cached"); - leaf = (Boolean)Worker.block(new LockAction() { - public Object run() { + leaf = Worker.block(new LockAction() { + public Boolean run() { logger.finer("hasChildren: working..."); - return ph.hasChildren() ? Boolean.FALSE : Boolean.TRUE; + return ph.hasChildren(); } }); logger.log(Level.FINER, "leaf={0}", leaf); @@ -287,7 +260,7 @@ public synchronized void addPhadhailListener(PhadhailListener l) { if (listeners == null) { - listeners = new ArrayList(); + listeners = new ArrayList(); ph.addPhadhailListener(SwungPhadhail.this); } listeners.add(l); Index: threaddemo/model/Worker.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/model/Attic/Worker.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- threaddemo/model/Worker.java 21 Aug 2003 22:19:52 -0000 1.4 +++ threaddemo/model/Worker.java 16 Dec 2005 18:17:54 -0000 1.5 @@ -31,7 +33,7 @@ private static final Worker DEFAULT = new Worker(); - private final LinkedList tasks = new LinkedList(); // List + private final LinkedList tasks = new LinkedList(); private Worker() { super("threaddemo.model.Worker"); @@ -51,7 +53,7 @@ ie.printStackTrace(); } } - next = (Runnable)tasks.removeFirst(); + next = tasks.removeFirst(); logger.log(Level.FINER, "fetching {0}", next); } try { @@ -82,14 +84,13 @@ /** * Do something and wait for it to finish. */ - public static Object block(final LockAction act) { - final Object[] result = new Object[2]; + public static T block(final LockAction act) { + final List result = new ArrayList(1); start(new Runnable() { public void run() { - Object val = act.run(); + T val = act.run(); synchronized (result) { - result[0] = val; - result[1] = Boolean.TRUE; + result.add(val); result.notify(); } } @@ -98,14 +99,27 @@ } }); synchronized (result) { - while (result[1] == null) { + while (result.isEmpty()) { try { result.wait(); } catch (InterruptedException ie) { ie.printStackTrace(); } } - return result[0]; + return result.get(0); + } + } + + private static final class Holder { + public final T object; + public final E exception; + public Holder(T object) { + this.object = object; + this.exception = null; + } + public Holder(E exception) { + this.object = null; + this.exception = exception; } } @@ -113,23 +127,20 @@ * Do something and wait for it to finish. * May throw exceptions. */ - public static Object block(final LockExceptionAction act) throws Exception { - final Object[] result = new Object[3]; + public static T block(final LockExceptionAction act) throws E { + final List> result = new ArrayList>(1); start(new Runnable() { public void run() { - Object val; - Exception ex; + Holder h; try { - val = act.run(); - ex = null; + h = new Holder(act.run()); } catch (Exception e) { - val = null; - ex = e; + @SuppressWarnings("unchecked") + E _e = (E) e; + h = new Holder(_e); } synchronized (result) { - result[0] = val; - result[1] = ex; - result[2] = Boolean.TRUE; + result.add(h); result.notify(); } } @@ -138,20 +149,20 @@ } }); synchronized (result) { - while (result[2] == null) { + while (result.isEmpty()) { try { result.wait(); } catch (InterruptedException ie) { ie.printStackTrace(); } } - if (result[1] != null) { - throw (Exception)result[1]; + Holder h = result.get(0); + if (h.exception != null) { + throw h.exception; } else { - return result[0]; + return h.object; } } } } - Index: threaddemo/util/DocumentParseSupport.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/util/DocumentParseSupport.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- threaddemo/util/DocumentParseSupport.java 21 Aug 2003 22:19:52 -0000 1.11 +++ threaddemo/util/DocumentParseSupport.java 16 Dec 2005 18:17:52 -0000 1.12 @@ -33,11 +35,11 @@ * writing to the text document from the model. *

The underlying model is a text document. The deltas to the underlying model * in this implementation are document text changes or reload events, though that - * fact is not visible to subclasses. The derived model must be defined by the + * fact should not matter to subclasses. The derived model must be defined by the * subclass. * @author Jesse Glick */ -public abstract class DocumentParseSupport extends TwoWaySupport { +public abstract class DocumentParseSupport extends TwoWaySupport { private static final Logger logger = Logger.getLogger(DocumentParseSupport.class.getName()); @@ -67,18 +69,16 @@ * of {@link org.openide.cookies.EditorCookie.Observable#PROP_DOCUMENT} indicating that the whole * document changed (was reloaded, for example), or lists of {@link DocumentEvent}s. */ - protected final Object composeUnderlyingDeltas(Object underlyingDelta1, Object underlyingDelta2) { - assert underlyingDelta1 instanceof PropertyChangeEvent || underlyingDelta1 instanceof List; - assert underlyingDelta2 instanceof PropertyChangeEvent || underlyingDelta2 instanceof List; - if (underlyingDelta1 instanceof PropertyChangeEvent) { + protected final DocumentParseSupportDelta composeUnderlyingDeltas(DocumentParseSupportDelta underlyingDelta1, DocumentParseSupportDelta underlyingDelta2) { + if (underlyingDelta1.changeEvent != null) { // PROP_DOCUMENT that is. Need to recreate the whole thing generally. return underlyingDelta1; - } else if (underlyingDelta2 instanceof PropertyChangeEvent) { + } else if (underlyingDelta2.changeEvent != null) { // Ditto. return underlyingDelta2; } else { // Append changes. - ((List)underlyingDelta1).addAll((List)underlyingDelta2); + underlyingDelta1.documentEvents.addAll(underlyingDelta2.documentEvents); return underlyingDelta1; } } @@ -132,22 +132,16 @@ * Parse the document. * Calls {@link #doDerive(StyledDocument, List, Object)}. */ - protected final DerivationResult doDerive(final Object oldValue, Object underlyingDelta) throws Exception { + protected final DerivationResult doDerive(final DM oldValue, final DocumentParseSupportDelta underlyingDelta) throws Exception { if (document == null) { refreshDocument(requiresUnmodifiedDocument()); } - final List documentEvents; // List - if (underlyingDelta instanceof List) { - documentEvents = (List)underlyingDelta; - } else { - documentEvents = null; - } - final DerivationResult[] val = new DerivationResult[1]; + final List> val = new ArrayList>(1); final Exception[] exc = new Exception[1]; Runnable r = new Runnable() { public void run() { try { - val[0] = doDerive(document, documentEvents, oldValue); + val.add(doDerive(document, underlyingDelta != null ? underlyingDelta.documentEvents : null, oldValue)); } catch (Exception e) { exc[0] = e; } @@ -161,7 +155,7 @@ if (exc[0] != null) { throw exc[0]; } - return val[0]; + return val.get(0); } /** @@ -198,24 +192,24 @@ * @return the new derived model value plus the change made to it * @throws Exception (checked) in case of parsing problems */ - protected abstract DerivationResult doDerive(StyledDocument document, List documentEvents, Object oldValue) throws Exception; + protected abstract DerivationResult doDerive(StyledDocument document, List documentEvents, DM oldValue) throws Exception; /** * Regenerates the document. * Calls {@link #doRecreate(StyledDocument, Object, Object)}. */ - protected final Object doRecreate(final Object oldValue, final Object derivedDelta) throws Exception { + protected final DM doRecreate(final DM oldValue, final DMD derivedDelta) throws Exception { if (document == null) { refreshDocument(true); } - final Object[] val = new Object[1]; + final List val = new ArrayList(1); final Exception[] exc = new Exception[1]; Runnable r = new Runnable() { public void run() { document.removeDocumentListener(listener); assert --listenerCount == 0; try { - val[0] = doRecreate(document, oldValue, derivedDelta); + val.add(doRecreate(document, oldValue, derivedDelta)); } catch (Exception e) { exc[0] = e; } finally { @@ -232,7 +226,7 @@ if (exc[0] != null) { throw exc[0]; } - return val[0]; + return val.get(0); } /** @@ -255,7 +249,7 @@ * @return the new derived model * @see org.openide.text.NbDocument.WriteLockable */ - protected abstract Object doRecreate(StyledDocument document, Object oldValue, Object derivedDelta) throws Exception; + protected abstract DM doRecreate(StyledDocument document, DM oldValue, DMD derivedDelta) throws Exception; /** * Listens to changes in identity or content of the text document. @@ -275,11 +269,11 @@ } private void documentUpdate(DocumentEvent e) { - final List l = new ArrayList(1); // List + final List l = new ArrayList(1); l.add(e); - getLock().read(new LockAction() { - public Object run() { - invalidate(l); + getLock().read(new LockAction() { + public Void run() { + invalidate(new DocumentParseSupportDelta(l)); return null; } }); @@ -303,7 +297,7 @@ // the EQ with CES.open or .openDocument. getLock().readLater(new Runnable() { public void run() { - invalidate(evt); + invalidate(new DocumentParseSupportDelta(evt)); } }); } Index: threaddemo/util/TwoWayEvent.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/util/TwoWayEvent.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- threaddemo/util/TwoWayEvent.java 1 Jul 2003 01:14:31 -0000 1.6 +++ threaddemo/util/TwoWayEvent.java 16 Dec 2005 18:17:51 -0000 1.7 @@ -21,9 +21,9 @@ * @author Jesse Glick * @see TwoWaySupport */ -public abstract class TwoWayEvent extends EventObject { +public abstract class TwoWayEvent extends EventObject { - private TwoWayEvent(TwoWaySupport s) { + private TwoWayEvent(TwoWaySupport s) { super(s); assert s != null; } @@ -32,18 +32,22 @@ * Get the associated two-way support. * @return the support */ - public TwoWaySupport getTwoWaySupport() { - return (TwoWaySupport)getSource(); + public TwoWaySupport getTwoWaySupport() { + @SuppressWarnings("unchecked") + TwoWaySupport source = (TwoWaySupport) getSource(); + return source; } /** * Event indicating a derived value has been produced. */ - public static final class Derived extends TwoWayEvent { + public static final class Derived extends TwoWayEvent { - private final Object oldValue, newValue, derivedDelta, underlyingDelta; + private final DM oldValue, newValue; + private final DMD derivedDelta; + private final UMD underlyingDelta; - Derived(TwoWaySupport s, Object oldValue, Object newValue, Object derivedDelta, Object underlyingDelta) { + Derived(TwoWaySupport s, DM oldValue, DM newValue, DMD derivedDelta, UMD underlyingDelta) { super(s); this.oldValue = oldValue; assert newValue != null; @@ -57,7 +61,7 @@ * Get the old value of the derived model. * @return the old value, or null if it was never calculated */ - public Object getOldValue() { + public DM getOldValue() { return oldValue; } @@ -65,7 +69,7 @@ * Get the new value of the derived model. * @return the new value */ - public Object getNewValue() { + public DM getNewValue() { return newValue; } @@ -73,7 +77,7 @@ * Get the change to the derived model. * @return the delta, or null if the old value was null */ - public Object getDerivedDelta() { + public DMD getDerivedDelta() { return derivedDelta; } @@ -84,7 +88,7 @@ * @return the invalidating change to the underlying model, or null if * the derived model is simply being computed for the first time */ - public Object getUnderlyingDelta() { + public UMD getUnderlyingDelta() { return underlyingDelta; } @@ -97,11 +101,12 @@ /** * Event indicating a derived model has been invalidated. */ - public static final class Invalidated extends TwoWayEvent { + public static final class Invalidated extends TwoWayEvent { - private final Object oldValue, underlyingDelta; + private final DM oldValue; + private final UMD underlyingDelta; - Invalidated(TwoWaySupport s, Object oldValue, Object underlyingDelta) { + Invalidated(TwoWaySupport s, DM oldValue, UMD underlyingDelta) { super(s); assert oldValue != null; this.oldValue = oldValue; @@ -113,7 +118,7 @@ * Get the old value of the derived model that is now invalid. * @return the old value */ - public Object getOldValue() { + public DM getOldValue() { return oldValue; } @@ -121,7 +126,7 @@ * Get the change to the underlying model that triggered this invalidation. * @return the invalidating change to the underlying model */ - public Object getUnderlyingDelta() { + public UMD getUnderlyingDelta() { return underlyingDelta; } @@ -134,11 +139,12 @@ /** * Event indicating the derived model was changed and the underlying model recreated. */ - public static final class Recreated extends TwoWayEvent { + public static final class Recreated extends TwoWayEvent { - private final Object oldValue, newValue, derivedDelta; + private final DM oldValue, newValue; + private final DMD derivedDelta; - Recreated(TwoWaySupport s, Object oldValue, Object newValue, Object derivedDelta) { + Recreated(TwoWaySupport s, DM oldValue, DM newValue, DMD derivedDelta) { super(s); assert oldValue != null; this.oldValue = oldValue; @@ -152,7 +158,7 @@ * Get the old value of the derived model that is now invalid. * @return the old value */ - public Object getOldValue() { + public DM getOldValue() { return oldValue; } @@ -160,7 +166,7 @@ * Get the new value of the derived model. * @return the new value */ - public Object getNewValue() { + public DM getNewValue() { return newValue; } @@ -169,7 +175,7 @@ * model as well. * @return the delta to the derived model */ - public Object getDerivedDelta() { + public DMD getDerivedDelta() { return derivedDelta; } @@ -183,11 +189,12 @@ * Event indicating changes in the underlying model were clobbered by changes to * the derived model. */ - public static final class Clobbered extends TwoWayEvent { + public static final class Clobbered extends TwoWayEvent { - private final Object oldValue, newValue, derivedDelta; + private final DM oldValue, newValue; + private final DMD derivedDelta; - Clobbered(TwoWaySupport s, Object oldValue, Object newValue, Object derivedDelta) { + Clobbered(TwoWaySupport s, DM oldValue, DM newValue, DMD derivedDelta) { super(s); this.oldValue = oldValue; assert newValue != null; @@ -200,7 +207,7 @@ * Get the old value of the derived model that is now invalid. * @return the old value, or null if it was never calculated */ - public Object getOldValue() { + public DM getOldValue() { return oldValue; } @@ -208,7 +215,7 @@ * Get the new value of the derived model. * @return the new value */ - public Object getNewValue() { + public DM getNewValue() { return newValue; } @@ -217,7 +224,7 @@ * model as well whether it is applicable or not. * @return the delta to the derived model */ - public Object getDerivedDelta() { + public DMD getDerivedDelta() { return derivedDelta; } @@ -234,9 +241,9 @@ * ever fire this event, since the default implementation creates a strong * reference that cannot be collected. */ - public static final class Forgotten extends TwoWayEvent { + public static final class Forgotten extends TwoWayEvent { - Forgotten(TwoWaySupport s) { + Forgotten(TwoWaySupport s) { super(s); } @@ -250,13 +257,14 @@ * Event indicating an attempted derivation failed with an exception. * The underlying model is thus considered to be in an inconsistent state. */ - public static final class Broken extends TwoWayEvent { + public static final class Broken extends TwoWayEvent { - private final Object oldValue, underlyingDelta; + private final DM oldValue; + private final UMD underlyingDelta; private final Exception exception; - Broken(TwoWaySupport s, Object oldValue, Object underlyingDelta, Exception exception) { + Broken(TwoWaySupport s, DM oldValue, UMD underlyingDelta, Exception exception) { super(s); this.oldValue = oldValue; this.underlyingDelta = underlyingDelta; @@ -268,7 +276,7 @@ * Get the old value of the derived model that is now invalid. * @return the old value, or null if it was never calculated */ - public Object getOldValue() { + public DM getOldValue() { return oldValue; } @@ -279,7 +287,7 @@ * @return the invalidating change to the underlying model, or null if * the derived model is simply being computed for the first time */ - public Object getUnderlyingDelta() { + public UMD getUnderlyingDelta() { return underlyingDelta; } Index: threaddemo/util/TwoWayListener.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/util/TwoWayListener.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/util/TwoWayListener.java 8 May 2003 21:16:50 -0000 1.3 +++ threaddemo/util/TwoWayListener.java 16 Dec 2005 18:17:52 -0000 1.4 @@ -20,45 +20,45 @@ * @author Jesse Glick * @see TwoWaySupport */ -public interface TwoWayListener extends EventListener { +public interface TwoWayListener extends EventListener { /** * Called when a new derived value has been produced. * May have been a result of a synchronous or asynchronous derivation. * @param evt the associated event with more information */ - void derived(TwoWayEvent.Derived evt); + void derived(TwoWayEvent.Derived evt); /** * Called when a derived model has been invalidated. * @param evt the associated event with more information */ - void invalidated(TwoWayEvent.Invalidated evt); + void invalidated(TwoWayEvent.Invalidated evt); /** * Called when the derived model was changed and the underlying model recreated. * @param evt the associated event with more information */ - void recreated(TwoWayEvent.Recreated evt); + void recreated(TwoWayEvent.Recreated evt); /** * Called when changes in the underlying model were clobbered by changes to * the derived model. * @param evt the associated event with more information */ - void clobbered(TwoWayEvent.Clobbered evt); + void clobbered(TwoWayEvent.Clobbered evt); /** * Called when the reference to the derived model was garbage collected. * @param evt the associated event */ - void forgotten(TwoWayEvent.Forgotten evt); + void forgotten(TwoWayEvent.Forgotten evt); /** * Called when an attempted derivation failed with an exception. * The underlying model is thus considered to be in an inconsistent state. * @param evt the associated event with more information */ - void broken(TwoWayEvent.Broken evt); + void broken(TwoWayEvent.Broken evt); } Index: threaddemo/util/TwoWaySupport.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/util/TwoWaySupport.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- threaddemo/util/TwoWaySupport.java 6 Nov 2003 09:25:44 -0000 1.16 +++ threaddemo/util/TwoWaySupport.java 16 Dec 2005 18:17:52 -0000 1.17 @@ -151,7 +159,7 @@ * * @author Jesse Glick */ -public abstract class TwoWaySupport { +public abstract class TwoWaySupport { /** logging support */ private static final Logger logger = Logger.getLogger(TwoWaySupport.class.getName()); @@ -160,10 +168,10 @@ private static final Object LOCK = new String("TwoWaySupport"); /** supports which are scheduled to be derived but haven't been yet */ - private static final SortedSet toDerive = new TreeSet(); // SortedSet + private static final SortedSet toDerive = new TreeSet(); /** derivation tasks indexed by support */ - private static final Map tasks = new WeakHashMap(); // Map + private static final Map tasks = new WeakHashMap(); /** derivation thread when it has been started */ private static boolean startedThread = false; @@ -172,16 +180,16 @@ private static ReferenceQueue queue = null; /** reverse lookup for model field to support queue collector */ - private static final Map referencesToSupports = new WeakHashMap(); // Map,Reference> + private static final Map,Reference> referencesToSupports = new WeakHashMap,Reference>(); /** associated lock */ private final Lock lock; /** listener list */ - private final List listeners; // List + private final List> listeners; /** current derived model, if any */ - private Reference model = null; // Reference + private Reference model = null; /** current derivation problem, if any */ private Exception problem = null; // XXX should perhaps be Reference? @@ -193,7 +201,7 @@ private boolean active = false; /** underlying delta, if one is being processed thru initiate + doDerive */ - private Object underlyingDelta = null; + private UMD underlyingDelta = null; /** currently in doRecreate() */ private boolean mutating = false; @@ -208,7 +216,7 @@ */ protected TwoWaySupport(Lock lock) { this.lock = lock; - listeners = new ArrayList(); + listeners = new ArrayList>(); } /** @@ -249,7 +257,7 @@ * the old value) plus the derived delta * @throws Exception (checked only!) if derivation of the model failed */ - protected abstract DerivationResult doDerive(Object oldValue, Object underlyingDelta) throws Exception; + protected abstract DerivationResult doDerive(DM oldValue, UMD underlyingDelta) throws Exception; /** * Result of a derivation. Includes both the final resulting value, and the @@ -257,9 +265,9 @@ * to pass to {@link TwoWayEvent.Derived}, which may be useful for subclasses * firing changes. */ - protected final static class DerivationResult { - final Object newValue; - final Object derivedDelta; + protected final static class DerivationResult { + final DM newValue; + final DMD derivedDelta; /** * Create a derivation result wrapper object. * @param newValue the new value of the derived model @@ -267,7 +275,7 @@ * model; must be null if the old derived value was null, * and only then */ - public DerivationResult(Object newValue, Object derivedDelta) { + public DerivationResult(DM newValue, DMD derivedDelta) { if (newValue == null) throw new NullPointerException(); this.newValue = newValue; this.derivedDelta = derivedDelta; @@ -286,7 +294,7 @@ * @param underlyingDelta2 the newer delta * @return a delta representing those two changes applied in sequence */ - protected abstract Object composeUnderlyingDeltas(Object underlyingDelta1, Object underlyingDelta2); + protected abstract UMD composeUnderlyingDeltas(UMD underlyingDelta1, UMD underlyingDelta2); /** * Recreate the underlying model from the derived model. @@ -307,7 +315,7 @@ * the old value) * @throws Exception (checked only!) if recreation of the underlying model failed */ - protected abstract Object doRecreate(Object oldValue, Object derivedDelta) throws Exception; + protected abstract DM doRecreate(DM oldValue, DMD derivedDelta) throws Exception; private void assertStateConsistent() { assert Thread.holdsLock(LOCK); @@ -332,9 +340,9 @@ * and threw an exception (possibly from an * earlier derivation run that is still broken) */ - public final Object getValueBlocking() throws InvocationTargetException { + public final DM getValueBlocking() throws InvocationTargetException { assert lock.canRead(); - Object old; + DM old; synchronized (LOCK) { assertStateConsistent(); assert !mutating; @@ -346,7 +354,7 @@ } catch (InterruptedException e) {/* OK */} } if (fresh) { - Object o = model.get(); + DM o = model.get(); if (o != null) { logger.log(Level.FINER, "fresh value: {0}", o); return o; @@ -362,8 +370,8 @@ fresh = false; } // Getting the value: - DerivationResult result; - Object newValue = null; + DerivationResult result; + DM newValue = null; try { result = doDerive(old, null); if (result == null) { @@ -385,7 +393,7 @@ } catch (Exception e) { problem = e; fresh = false; - fireChange(new TwoWayEvent.Broken(this, old, underlyingDelta, e)); + fireChange(new TwoWayEvent.Broken(this, old, underlyingDelta, e)); throw new InvocationTargetException(e); } finally { synchronized (LOCK) { @@ -397,7 +405,7 @@ deactivate(); } } - fireChange(new TwoWayEvent.Derived(this, old, result.newValue, result.derivedDelta, underlyingDelta)); + fireChange(new TwoWayEvent.Derived(this, old, result.newValue, result.derivedDelta, underlyingDelta)); return result.newValue; } @@ -406,7 +414,7 @@ if (active) { // No longer need to run this. active = false; - DeriveTask t = (DeriveTask)tasks.remove(this); + DeriveTask t = tasks.remove(this); assert t != null; toDerive.remove(t); } @@ -421,14 +429,16 @@ } } - private void setModel(Object result) { + private void setModel(DM result) { assert Thread.holdsLock(LOCK); assert result != null; if (model != null) { referencesToSupports.remove(model); } model = createEnqueuedReference(result); - referencesToSupports.put(model, new WeakReference(this)); + @SuppressWarnings("unchecked") + Reference _model = (Reference) model; + referencesToSupports.put(_model, new WeakReference(this)); } /** @@ -437,7 +447,7 @@ * @return the value of the derived model, or null if it is stale or has never * been computed at all */ - public final Object getValueNonBlocking() { + public final DM getValueNonBlocking() { assert lock.canRead(); synchronized (LOCK) { assertStateConsistent(); @@ -452,7 +462,7 @@ * @return the value of the derived model, or null if it has never been * computed at all */ - public final Object getStaleValueNonBlocking() { + public final DM getStaleValueNonBlocking() { assert lock.canRead(); synchronized (LOCK) { assertStateConsistent(); @@ -474,10 +484,10 @@ * @throws InvocationTargetException if doRecreate throws an * exception */ - public final Object mutate(Object derivedDelta) throws ClobberException, InvocationTargetException { + public final DM mutate(DMD derivedDelta) throws ClobberException, InvocationTargetException { if (derivedDelta == null) throw new NullPointerException(); assert lock.canWrite(); - Object oldValue; + DM oldValue; synchronized (LOCK) { assertStateConsistent(); assert !mutating; @@ -488,7 +498,7 @@ } mutating = true; } - Object result = null; + DM result = null; try { // XXX should also dequeue if necessary to avoid sequence: // invalidate -> initiate -> [pause] -> mutate -> [pause] -> invalidate -> [pause] -> derive @@ -508,9 +518,9 @@ } } if (fresh) { - fireChange(new TwoWayEvent.Recreated(this, oldValue, result, derivedDelta)); + fireChange(new TwoWayEvent.Recreated(this, oldValue, result, derivedDelta)); } else { - fireChange(new TwoWayEvent.Clobbered(this, oldValue, result, derivedDelta)); + fireChange(new TwoWayEvent.Clobbered(this, oldValue, result, derivedDelta)); } return result; } @@ -522,11 +532,11 @@ * except to call {@link #composeUnderlyingDeltas}. * @param underlyingDelta a change to the underlying model */ - public final void invalidate(Object underlyingDelta) { + public final void invalidate(UMD underlyingDelta) { if (underlyingDelta == null) throw new NullPointerException(); assert lock.canRead(); boolean wasInited; - Object oldValue; + DM oldValue; synchronized (LOCK) { assertStateConsistent(); assert !mutating; @@ -545,7 +555,7 @@ problem = null; } if (wasInited && oldValue != null) { - fireChange(new TwoWayEvent.Invalidated(this, oldValue, underlyingDelta)); + fireChange(new TwoWayEvent.Invalidated(this, oldValue, underlyingDelta)); } } @@ -561,8 +571,8 @@ synchronized (LOCK) { assertStateConsistent(); if (!active && !fresh) { - Object oldValue = (model != null) ? model.get() : null; - DeriveTask t = new DeriveTask(this, oldValue != null || problem != null); + DM oldValue = (model != null) ? model.get() : null; + DeriveTask t = new DeriveTask(this, oldValue != null || problem != null); toDerive.add(t); tasks.put(this, t); active = true; @@ -595,7 +605,7 @@ *

This method may be called from any thread and will not block. * @param l a listener to add */ - public final void addTwoWayListener(TwoWayListener l) { + public final void addTwoWayListener(TwoWayListener l) { synchronized (listeners) { listeners.add(l); } @@ -606,7 +616,7 @@ *

This method may be called from any thread and will not block. * @param l a listener to remove */ - public final void removeTwoWayListener(TwoWayListener l) { + public final void removeTwoWayListener(TwoWayListener l) { synchronized (listeners) { listeners.remove(l); } @@ -615,30 +625,30 @@ /** * Fire an event to all listeners in the read lock. */ - private void fireChange(final TwoWayEvent e) { - final TwoWayListener[] ls; + private void fireChange(final TwoWayEvent e) { + final List> ls; synchronized (listeners) { if (listeners.isEmpty()) { return; } - ls = (TwoWayListener[])listeners.toArray(new TwoWayListener[listeners.size()]); + ls = new ArrayList>(listeners); } lock.read(new Runnable() { public void run() { - for (int i = 0; i < ls.length; i++) { + for (TwoWayListener l : ls) { if (e instanceof TwoWayEvent.Derived) { - ls[i].derived((TwoWayEvent.Derived)e); + l.derived((TwoWayEvent.Derived) e); } else if (e instanceof TwoWayEvent.Invalidated) { - ls[i].invalidated((TwoWayEvent.Invalidated)e); + l.invalidated((TwoWayEvent.Invalidated) e); } else if (e instanceof TwoWayEvent.Recreated) { - ls[i].recreated((TwoWayEvent.Recreated)e); + l.recreated((TwoWayEvent.Recreated) e); } else if (e instanceof TwoWayEvent.Clobbered) { - ls[i].clobbered((TwoWayEvent.Clobbered)e); + l.clobbered((TwoWayEvent.Clobbered) e); } else if (e instanceof TwoWayEvent.Forgotten) { - ls[i].forgotten((TwoWayEvent.Forgotten)e); + l.forgotten((TwoWayEvent.Forgotten) e); } else { assert e instanceof TwoWayEvent.Broken; - ls[i].broken((TwoWayEvent.Broken)e); + l.broken((TwoWayEvent.Broken) e); } } } @@ -668,8 +678,8 @@ return false; } - private Reference createEnqueuedReference(Object value) { - Reference r = createReference(value, queue); + private Reference createEnqueuedReference(DM value) { + Reference r = createReference(value, queue); if (!(r instanceof StrongReference) && queue == null) { // Well discard that one; optimistically assumed that // createReference is not overridden, in which case we @@ -692,11 +702,11 @@ Reference r = queue.remove(); TwoWaySupport s; synchronized (LOCK) { - Reference r2 = (Reference)referencesToSupports.remove(r); - s = (r2 != null) ? (TwoWaySupport)r2.get() : null; + Reference r2 = referencesToSupports.remove(r); + s = (r2 != null) ? r2.get() : null; } if (s != null) { - s.fireChange(new TwoWayEvent.Forgotten(s)); + notify(s); } } catch (InterruptedException e) { assert false : e; @@ -704,6 +714,11 @@ } } + @SuppressWarnings("unchecked") + private void notify(TwoWaySupport s) { + s.fireChange(new TwoWayEvent.Forgotten(s)); + } + } /** @@ -718,23 +733,23 @@ * @param q a reference queue supplied by the support * @return a reference to the model enqueued on that reference queue */ - protected Reference createReference(Object value, ReferenceQueue q) { + protected Reference createReference(DM value, ReferenceQueue q) { // Does not matter what the queue is. - return new StrongReference(value); + return new StrongReference(value); } /** * A strong reference whose referent will not be collected unless the * reference is too. */ - private static final class StrongReference extends WeakReference { - private Object value; - public StrongReference(Object value) { + private static final class StrongReference extends WeakReference { + private DM value; + public StrongReference(DM value) { super(value); assert value != null; this.value = value; } - public Object get() { + public DM get() { return value; } public void clear() { @@ -743,19 +758,18 @@ } } - private static final class DeriveTask implements Comparable { + static final class DeriveTask implements Comparable> { - public final Reference support; // Reference + public final Reference> support; public final long schedule; - public DeriveTask(TwoWaySupport support, boolean delay) { - this.support = new WeakReference(support); + public DeriveTask(TwoWaySupport support, boolean delay) { + this.support = new WeakReference>(support); schedule = System.currentTimeMillis() + (delay ? support.delay() : 0L); } - public int compareTo(Object o) { - DeriveTask t = (DeriveTask)o; + public int compareTo(DeriveTask t) { if (t == this) return 0; if (schedule > t.schedule) return 1; if (schedule < t.schedule) return -1; @@ -792,9 +806,9 @@ assert false : e; } } - Iterator it = toDerive.iterator(); - DeriveTask t = (DeriveTask)it.next(); - s[0] = (TwoWaySupport)t.support.get(); + Iterator it = toDerive.iterator(); + DeriveTask t = it.next(); + s[0] = (TwoWaySupport) t.support.get(); logger.log(Level.FINER, "derivation thread found: {0}", s[0]); if (s[0] == null) { // Dead - support was collected before we got to it. @@ -803,7 +817,7 @@ } long now = System.currentTimeMillis(); if (t.schedule > now) { - logger.log(Level.FINER, "derivation thread deferring: {0} for {1}msec", new Object[] {s[0], new Long(t.schedule - now)}); + logger.log(Level.FINER, "derivation thread deferring: {0} for {1}msec", new Object[] {s[0], t.schedule - now}); try { LOCK.wait(t.schedule - now); } catch (InterruptedException e) { Index: threaddemo/views/ElementLook.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/views/ElementLook.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- threaddemo/views/ElementLook.java 28 Aug 2003 20:54:35 -0000 1.12 +++ threaddemo/views/ElementLook.java 16 Dec 2005 18:17:56 -0000 1.13 @@ -120,11 +127,11 @@ public List getChildObjects(Object o, Lookup env) { NodeList nl = ((Element)o).getChildNodes(); - List l = new ArrayList(Math.max(nl.getLength(), 1)); + List l = new ArrayList(Math.max(nl.getLength(), 1)); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n instanceof Element) { - l.add(n); + l.add((Element) n); } } return l; Index: threaddemo/views/PhadhailLook.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/views/PhadhailLook.java,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- threaddemo/views/PhadhailLook.java 11 Aug 2004 18:26:23 -0000 1.32 +++ threaddemo/views/PhadhailLook.java 16 Dec 2005 18:17:56 -0000 1.33 @@ -52,10 +52,10 @@ private static final Logger logger = Logger.getLogger(PhadhailLook.class.getName()); - private static final Map phadhails2Results = new IdentityHashMap(); // Map - private static final Map results2Phadhails = new IdentityHashMap(); // Map - private static final Map phadhails2DomProviders = new IdentityHashMap(); // Map - private static final Map domProviders2Phadhails = new IdentityHashMap(); // Map + private static final Map phadhails2Results = new IdentityHashMap(); + private static final Map results2Phadhails = new IdentityHashMap(); + private static final Map phadhails2DomProviders = new IdentityHashMap(); + private static final Map domProviders2Phadhails = new IdentityHashMap(); PhadhailLook() { super("PhadhailLook"); @@ -74,13 +74,13 @@ protected void detachFrom(Object o) { Phadhail ph = (Phadhail)o; ph.removePhadhailListener(this); - Lookup.Result r = (Lookup.Result)phadhails2Results.remove(ph); + Lookup.Result r = phadhails2Results.remove(ph); if (r != null) { r.removeLookupListener(this); assert results2Phadhails.containsKey(r); results2Phadhails.remove(r); } - DomProvider p = (DomProvider)phadhails2DomProviders.remove(ph); + DomProvider p = phadhails2DomProviders.remove(ph); if (p != null) { p.removeChangeListener(this); assert domProviders2Phadhails.containsKey(p); @@ -234,7 +234,7 @@ public void stateChanged(ChangeEvent e) { logger.finer("got change"); DomProvider p = (DomProvider)e.getSource(); - final Phadhail ph = (Phadhail)domProviders2Phadhails.get(p); + final Phadhail ph = domProviders2Phadhails.get(p); assert ph != null; Locks.event().readLater(new Runnable() { public void run() { Index: threaddemo/views/PhadhailViews.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/views/PhadhailViews.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- threaddemo/views/PhadhailViews.java 21 Nov 2003 22:26:03 -0000 1.18 +++ threaddemo/views/PhadhailViews.java 16 Dec 2005 18:17:56 -0000 1.19 @@ -114,15 +114,15 @@ super(n, n.isLeaf() ? Children.LEAF : new EQReplannedChildren(n)); } public String getName() { - return (String)Locks.event().read(new LockAction() { - public Object run() { + return Locks.event().read(new LockAction() { + public String run() { return EQReplannedNode.super.getName(); } }); } public String getDisplayName() { - return (String)Locks.event().read(new LockAction() { - public Object run() { + return Locks.event().read(new LockAction() { + public String run() { return EQReplannedNode.super.getDisplayName(); } }); @@ -137,15 +137,15 @@ return new EQReplannedNode(n); } public Node findChild(final String name) { - return (Node)Locks.event().read(new LockAction() { - public Object run() { + return Locks.event().read(new LockAction() { + public Node run() { return EQReplannedChildren.super.findChild(name); } }); } public Node[] getNodes(final boolean optimalResult) { - return (Node[])Locks.event().read(new LockAction() { - public Object run() { + return Locks.event().read(new LockAction() { + public Node[] run() { return EQReplannedChildren.super.getNodes(optimalResult); } }); Index: threaddemo/views/looktree/LookTreeModel.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/views/looktree/LookTreeModel.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- threaddemo/views/looktree/LookTreeModel.java 28 Aug 2003 20:54:37 -0000 1.4 +++ threaddemo/views/looktree/LookTreeModel.java 16 Dec 2005 18:17:50 -0000 1.5 @@ -33,10 +35,10 @@ private final Object rootObject; private final LookSelector sel; private LookTreeNode root; - private final List listeners; // List + private final List listeners; public LookTreeModel(Object root, LookSelector sel) { - listeners = new ArrayList(); + listeners = new ArrayList(); this.rootObject = root; this.sel = sel; } @@ -104,9 +106,7 @@ int[] childIndices = parent != null ? new int[] {getIndexOfChild(parent, source)} : null; Object[] children = parent != null ? new Object[] {source} : null; TreeModelEvent ev = new TreeModelEvent(this, path, childIndices, children); - Iterator it = listeners.iterator(); - while (it.hasNext()) { - TreeModelListener l = (TreeModelListener)it.next(); + for (TreeModelListener l : listeners) { l.treeNodesChanged(ev); } } @@ -119,9 +119,7 @@ // XXX this is crude, could try to actually compute added/removed children... TreePath path = (source == root) ? null : findPath(source.getParent()); TreeModelEvent ev = new TreeModelEvent(this, path, null, null); - Iterator it = listeners.iterator(); - while (it.hasNext()) { - TreeModelListener l = (TreeModelListener)it.next(); + for (TreeModelListener l : listeners) { logger.log(Level.FINER, "firing: {0} to {1}", new Object[] {ev, l}); l.treeStructureChanged(ev); } Index: threaddemo/views/looktree/LookTreeNode.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/views/looktree/LookTreeNode.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- threaddemo/views/looktree/LookTreeNode.java 28 Aug 2003 20:54:37 -0000 1.9 +++ threaddemo/views/looktree/LookTreeNode.java 16 Dec 2005 18:17:50 -0000 1.10 @@ -32,7 +34,7 @@ private final Object representedObject; private final Look look; - // private Map children = null; // Map + // private Map children = null; private LookTreeNode[] children; private List childrenList; protected int index = -1; @@ -85,7 +87,7 @@ private LookTreeNode(Look l, Object o) { this.representedObject = o; this.look = l; - org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( l, representedObject, this ); + Accessor.DEFAULT.addLookListener( l, representedObject, this ); } private static Look findLook(Object o, LookSelector s) { @@ -114,9 +116,8 @@ void forgetChildren() { /* if (children != null) { - Iterator it = children.values().iterator(); - while (it.hasNext()) { - ((LookTreeNode)it.next()).forgetEverything(); + for (LookTreeNode child : children) { + child.forgetEverything(); } children = null; } Index: threaddemo/views/looktree/PopupHandler.java =================================================================== RCS file: /cvs/performance/threaddemo/src/threaddemo/views/looktree/PopupHandler.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- threaddemo/views/looktree/PopupHandler.java 11 Aug 2004 18:26:23 -0000 1.3 +++ threaddemo/views/looktree/PopupHandler.java 16 Dec 2005 18:17:50 -0000 1.4 @@ -43,7 +43,7 @@ private final LookTreeView view; private final ActionMap actionMap; - private Reference clicked; // Reference + private Reference clicked; public PopupHandler(LookTreeView view) { this.view = view; @@ -58,7 +58,7 @@ TreePath path = view.getClosestPathForLocation(x, y); if (path != null) { LookTreeNode n = (LookTreeNode)path.getLastPathComponent(); - clicked = new WeakReference(n); + clicked = new WeakReference(n); Action[] actions = n.getLook().getActions(n.getData(), n.getLookup() ); // XXX handle multiselects... Node selection = makeNode( n ); @@ -87,7 +87,7 @@ public void actionPerformed(ActionEvent e) { // XXX should confirm deletion first - LookTreeNode n = (LookTreeNode)clicked.get(); + LookTreeNode n = clicked.get(); try { n.getLook().destroy(n.getData(), n.getLookup() ); } catch (IOException ex) { ADDED: package threaddemo.util; public final class DocumentParseSupportDelta { public final PropertyChangeEvent changeEvent; public final List documentEvents; public DocumentParseSupportDelta(PropertyChangeEvent e) { changeEvent = e; documentEvents = null; } public DocumentParseSupportDelta(List e) { changeEvent = null; documentEvents = e; } }