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: */
030: package org.objectweb.asm.tree.analysis;
031:
032: import java.util.AbstractSet;
033: import java.util.HashSet;
034: import java.util.Iterator;
035: import java.util.Set;
036:
037: /**
038: * A set of at most two elements.
039: *
040: * @author Eric Bruneton
041: */
042: @SuppressWarnings("unchecked")
043: class SmallSet extends AbstractSet implements Iterator {
044:
045: // if e1 is null, e2 must be null; otherwise e2 must be different from e1
046:
047: Object e1, e2;
048:
049: final static SmallSet EMPTY_SET = new SmallSet(null, null);
050:
051: SmallSet(final Object e1, final Object e2) {
052: this .e1 = e1;
053: this .e2 = e2;
054: }
055:
056: // -------------------------------------------------------------------------
057: // Implementation of inherited abstract methods
058: // -------------------------------------------------------------------------
059:
060: public Iterator iterator() {
061: return new SmallSet(e1, e2);
062: }
063:
064: public int size() {
065: return e1 == null ? 0 : (e2 == null ? 1 : 2);
066: }
067:
068: // -------------------------------------------------------------------------
069: // Implementation of the Iterator interface
070: // -------------------------------------------------------------------------
071:
072: public boolean hasNext() {
073: return e1 != null;
074: }
075:
076: public Object next() {
077: Object e = e1;
078: e1 = e2;
079: e2 = null;
080: return e;
081: }
082:
083: public void remove() {
084: }
085:
086: // -------------------------------------------------------------------------
087: // Utility methods
088: // -------------------------------------------------------------------------
089:
090: Set union(final SmallSet s) {
091: if ((s.e1 == e1 && s.e2 == e2) || (s.e1 == e2 && s.e2 == e1)) {
092: return this ; // if the two sets are equal, return this
093: }
094: if (s.e1 == null) {
095: return this ; // if s is empty, return this
096: }
097: if (e1 == null) {
098: return s; // if this is empty, return s
099: }
100: if (s.e2 == null) { // s contains exactly one element
101: if (e2 == null) {
102: return new SmallSet(e1, s.e1); // necessarily e1 != s.e1
103: } else if (s.e1 == e1 || s.e1 == e2) { // s is included in this
104: return this ;
105: }
106: }
107: if (e2 == null) { // this contains exactly one element
108: // if (s.e2 == null) { // cannot happen
109: // return new SmallSet(e1, s.e1); // necessarily e1 != s.e1
110: // } else
111: if (e1 == s.e1 || e1 == s.e2) { // this in included in s
112: return s;
113: }
114: }
115: // here we know that there are at least 3 distinct elements
116: HashSet r = new HashSet(4);
117: r.add(e1);
118: if (e2 != null) {
119: r.add(e2);
120: }
121: r.add(s.e1);
122: if (s.e2 != null) {
123: r.add(s.e2);
124: }
125: return r;
126: }
127: }
|