001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2005, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.ba.heap;
021:
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import edu.umd.cs.findbugs.ba.XField;
026:
027: /**
028: * @author David Hovemeyer
029: */
030: public class FieldSet {
031: private boolean isTop, isBottom;
032: private Set<XField> fieldSet;
033:
034: public FieldSet() {
035: fieldSet = new HashSet<XField>();
036: }
037:
038: public void setTop() {
039: clear();
040: isTop = true;
041: }
042:
043: public boolean isTop() {
044: return isTop;
045: }
046:
047: public void setBottom() {
048: clear();
049: isBottom = true;
050: }
051:
052: public boolean isBottom() {
053: return isBottom;
054: }
055:
056: public boolean isValid() {
057: return !isTop && !isBottom;
058: }
059:
060: public boolean isEmpty() {
061: return !isTop && !isBottom && fieldSet.isEmpty();
062: }
063:
064: public void clear() {
065: isTop = isBottom = false;
066: fieldSet.clear();
067: }
068:
069: public void addField(XField field) {
070: if (!isValid())
071: throw new IllegalStateException();
072: fieldSet.add(field);
073: }
074:
075: public boolean contains(XField field) {
076: return fieldSet.contains(field);
077: }
078:
079: public void mergeWith(FieldSet other) {
080: if (other.isTop() || this .isBottom())
081: return;
082:
083: if (other.isBottom() || this .isTop()) {
084: this .copyFrom(other);
085: return;
086: }
087:
088: fieldSet.addAll(other.fieldSet);
089: }
090:
091: public boolean sameAs(FieldSet other) {
092: return this .isTop == other.isTop
093: && this .isBottom == other.isBottom
094: && this .fieldSet.equals(other.fieldSet);
095: }
096:
097: public void copyFrom(FieldSet other) {
098: this .isTop = other.isTop;
099: this .isBottom = other.isBottom;
100: this .fieldSet.clear();
101: this .fieldSet.addAll(other.fieldSet);
102: }
103:
104: public boolean isIntersectionNonEmpty(FieldSet other) {
105: for (XField field : fieldSet) {
106: if (other.fieldSet.contains(field))
107: return true;
108: }
109: return false;
110: }
111:
112: @Override
113: public String toString() {
114: if (isTop)
115: return "TOP";
116: else if (isBottom)
117: return "BOTTOM";
118: else
119: return fieldSet.toString();
120: }
121: }
|