1 cananian 1.1.2.1 // HandlerSet.java, created Wed Dec 23 02:37:47 1998 by cananian
 2 cananian 1.1.2.6 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
 3 cananian 1.1.2.6 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 cananian 1.1.2.1 package harpoon.IR.Quads;
 5 cananian 1.1.2.1 
 6 cananian 1.6     import net.cscott.jutil.UnmodifiableIterator;
 7 cananian 1.6     import net.cscott.jutil.IteratorEnumerator;
 8 cananian 1.1.2.1 import harpoon.Util.Util;
 9 cananian 1.1.2.1 
10 cananian 1.1.2.1 import java.util.Enumeration;
11 cananian 1.1.2.3 import java.util.Iterator;
12 cananian 1.1.2.1 import java.util.NoSuchElementException;
13 cananian 1.1.2.1 /**
14 cananian 1.1.2.1  * A <code>HandlerSet</code> is a linked list of handlers, for purposes
15 cananian 1.1.2.1  * of comparison.  See the <code>equals()</code> method.  Used by
16 cananian 1.1.2.1  * both <code>Translate</code> and <code>Print</code>.
17 cananian 1.1.2.1  * 
18 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
19 cananian 1.6      * @version $Id: HandlerSet.java,v 1.6 2004/02/08 01:55:25 cananian Exp $
20 cananian 1.1.2.1  */
21 cananian 1.1.2.2 final public class HandlerSet {
22 cananian 1.1.2.2     final HANDLER h;
23 cananian 1.1.2.2     final HandlerSet next;
24 cananian 1.1.2.1     HandlerSet(HANDLER h, HandlerSet next) {
25 cananian 1.3.2.1         assert h!=null /*&& !contains(next, h)*/;
26 cananian 1.1.2.1         this.h = h; this.next = next;
27 cananian 1.1.2.1     }
28 cananian 1.1.2.1     /** Determines if this <code>HandlerSet</code> contains a given
29 cananian 1.1.2.1      *  <code>HANDLER</code>. */
30 cananian 1.1.2.2     final boolean contains(HANDLER h) { return contains(this, h); }
31 cananian 1.1.2.1     /** Returns an enumeration of the <code>HANDLER</code>s in this
32 cananian 1.1.2.3      *  <code>HandlerSet</code>.
33 cananian 1.1.2.3      * @deprecated Use iterator() instead. */
34 cananian 1.1.2.2     final Enumeration elements() { return elements(this); }
35 cananian 1.1.2.3     /** Returns an iteration over the <code>HANDLER</code>s in this
36 cananian 1.1.2.3      *  <code>HandlerSet</code>. */
37 cananian 1.1.2.3     final Iterator iterator() { return iterator(this); }
38 cananian 1.1.2.1     /** Determines if an object is equal to this <code>HandlerSet</code>. */
39 cananian 1.1.2.1     public final boolean equals(Object o) {
40 cananian 1.1.2.5         HandlerSet hs;
41 cananian 1.1.2.5         try{ hs=(HandlerSet)o; } catch (ClassCastException e) { return false; }
42 cananian 1.1.2.5         return equals(this, hs);
43 cananian 1.1.2.1     }
44 cananian 1.1.2.1     /** Computes a hashcode for this <code>HandlerSet</code>. */
45 cananian 1.1.2.1     public final int hashCode() {
46 cananian 1.1.2.1         return h.hashCode() ^ ((next!=null)?(next.hashCode()<<1):0);
47 cananian 1.1.2.1     }
48 cananian 1.1.2.1     /** Determines if two <code>HandlerSet</code>s are equal. */
49 cananian 1.1.2.1     public static final boolean equals(HandlerSet h1, HandlerSet h2) {
50 cananian 1.1.2.1         if (h1==null || h2==null) return (h1==h2);
51 cananian 1.1.2.1         else return (h1.h==h2.h) ? equals(h1.next,h2.next) : false;
52 cananian 1.1.2.1     }
53 cananian 1.1.2.3     /** Returns an enumeration of the <code>HANDLER</code>s in the given
54 cananian 1.1.2.3      *  <code>HandlerSet</code>. 
55 cananian 1.1.2.3      *  @deprecated Use iterator(hs) instead.
56 cananian 1.1.2.3      */
57 cananian 1.1.2.1     public static final Enumeration elements(final HandlerSet hs) {
58 cananian 1.1.2.3         return new IteratorEnumerator(iterator(hs));
59 cananian 1.1.2.3     }
60 cananian 1.1.2.3     /** Returns an iterator over the <code>HANDLER</code>s in the given
61 cananian 1.1.2.3      *  <code>HandlerSet</code>. */
62 cananian 1.1.2.3     public static final Iterator iterator(final HandlerSet hs) {
63 cananian 1.1.2.4         return new UnmodifiableIterator() {
64 cananian 1.1.2.1             HandlerSet hsp = hs;
65 cananian 1.1.2.3             public boolean hasNext() { return (hsp!=null); }
66 cananian 1.1.2.3             public Object  next() {
67 cananian 1.1.2.1                 try { HANDLER h=hsp.h; hsp=hsp.next; return h; }
68 cananian 1.1.2.1                 catch (NullPointerException e)
69 cananian 1.1.2.1                 { throw new NoSuchElementException(); }
70 cananian 1.1.2.1             }
71 cananian 1.1.2.1         };
72 cananian 1.1.2.1     }
73 cananian 1.1.2.1     /** Determines if a given <code>HandlerSet</code> contains a given
74 cananian 1.1.2.1      *  <code>HANDLER</code>. */
75 cananian 1.1.2.1     public static final boolean contains(HandlerSet hs, HANDLER h) {
76 cananian 1.1.2.1         return (hs==null)?false:(hs.h==h)?true:contains(hs.next, h);
77 cananian 1.1.2.1     }
78 cananian 1.2     }