001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package com.tc.asm.tree.analysis;
030:
031: import java.util.AbstractSet;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Set;
035:
036: /**
037: * A set of at most two elements.
038: *
039: * @author Eric Bruneton
040: */
041: class SmallSet extends AbstractSet implements Iterator {
042:
043: // if e1 is null, e2 must be null; otherwise e2 must be different from e1
044:
045: Object e1, e2;
046:
047: final static SmallSet EMPTY_SET = new SmallSet(null, null);
048:
049: SmallSet(final Object e1, final Object e2) {
050: this .e1 = e1;
051: this .e2 = e2;
052: }
053:
054: // -------------------------------------------------------------------------
055: // Implementation of inherited abstract methods
056: // -------------------------------------------------------------------------
057:
058: public Iterator iterator() {
059: return new SmallSet(e1, e2);
060: }
061:
062: public int size() {
063: return e1 == null ? 0 : (e2 == null ? 1 : 2);
064: }
065:
066: // -------------------------------------------------------------------------
067: // Implementation of the Iterator interface
068: // -------------------------------------------------------------------------
069:
070: public boolean hasNext() {
071: return e1 != null;
072: }
073:
074: public Object next() {
075: Object e = e1;
076: e1 = e2;
077: e2 = null;
078: return e;
079: }
080:
081: public void remove() {
082: }
083:
084: // -------------------------------------------------------------------------
085: // Utility methods
086: // -------------------------------------------------------------------------
087:
088: Set union(final SmallSet s) {
089: if ((s.e1 == e1 && s.e2 == e2) || (s.e1 == e2 && s.e2 == e1)) {
090: return this ; // if the two sets are equal, return this
091: }
092: if (s.e1 == null) {
093: return this ; // if s is empty, return this
094: }
095: if (e1 == null) {
096: return s; // if this is empty, return s
097: }
098: if (s.e2 == null) { // s contains exactly one element
099: if (e2 == null) {
100: return new SmallSet(e1, s.e1); // necessarily e1 != s.e1
101: } else if (s.e1 == e1 || s.e1 == e2) { // s is included in this
102: return this ;
103: }
104: }
105: if (e2 == null) { // this contains exactly one element
106: // if (s.e2 == null) { // cannot happen
107: // return new SmallSet(e1, s.e1); // necessarily e1 != s.e1
108: // } else
109: if (e1 == s.e1 || e1 == s.e2) { // this in included in s
110: return s;
111: }
112: }
113: // here we know that there are at least 3 distinct elements
114: HashSet r = new HashSet(4);
115: r.add(e1);
116: if (e2 != null) {
117: r.add(e2);
118: }
119: r.add(s.e1);
120: if (s.e2 != null) {
121: r.add(s.e2);
122: }
123: return r;
124: }
125: }
|