001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.rule.encapsulation;
012:
013: final class TypeSchemeUnion implements TypeSchemeTerm {
014: public static final TypeSchemeUnion PRIMITIVE = new TypeSchemeUnion(
015: PrimitiveScheme.INSTANCE);
016: public static final TypeSchemeUnion NULL = new TypeSchemeUnion(
017: NullScheme.INSTANCE);
018: public static final TypeSchemeUnion THIS = new TypeSchemeUnion(
019: ThisScheme.INSTANCE);
020: public static final TypeSchemeUnion REP = new TypeSchemeUnion(
021: RepScheme.INSTANCE);
022: public static final TypeSchemeUnion PEER = new TypeSchemeUnion(
023: PeerScheme.INSTANCE);
024: public static final TypeSchemeUnion READONLY = new TypeSchemeUnion(
025: ReadonlyScheme.INSTANCE);
026: public static final TypeSchemeUnion READONLYPRIME = new TypeSchemeUnion(
027: ReadonlyPrimeScheme.INSTANCE);
028: public static final TypeSchemeUnion ROOT = new TypeSchemeUnion(
029: RootScheme.INSTANCE);
030: public static final TypeSchemeUnion READONLY_ROOT = new TypeSchemeUnion(
031: new TypeScheme[] { ReadonlyScheme.INSTANCE,
032: RootScheme.INSTANCE });
033: public static final TypeSchemeUnion REP_PEER_ROOT = new TypeSchemeUnion(
034: new TypeScheme[] { RepScheme.INSTANCE, PeerScheme.INSTANCE,
035: RootScheme.INSTANCE });
036: public static final TypeSchemeUnion REP_PEER_READONLY_ROOT = new TypeSchemeUnion(
037: new TypeScheme[] { RepScheme.INSTANCE, PeerScheme.INSTANCE,
038: ReadonlyScheme.INSTANCE, RootScheme.INSTANCE });
039: public static final TypeSchemeUnion PRIMITIVE_REP_PEER_READONLY_ROOT = new TypeSchemeUnion(
040: new TypeScheme[] { PrimitiveScheme.INSTANCE,
041: RepScheme.INSTANCE, PeerScheme.INSTANCE,
042: ReadonlyScheme.INSTANCE, RootScheme.INSTANCE });
043:
044: private SetOfTypeScheme possibilities;
045:
046: public TypeSchemeUnion(TypeScheme ts) {
047: possibilities = SetAsListOfTypeScheme.EMPTY_SET.add(ts);
048: }
049:
050: public TypeSchemeUnion(SetOfTypeScheme possibilities) {
051: this .possibilities = possibilities;
052: }
053:
054: public TypeSchemeUnion(TypeScheme[] schemes) {
055: possibilities = SetAsListOfTypeScheme.EMPTY_SET;
056: for (int i = schemes.length - 1; i >= 0; i--) {
057: possibilities = possibilities.add(schemes[i]);
058: }
059: }
060:
061: public SetOfTypeScheme getPossibilities() {
062: return possibilities;
063: }
064:
065: public boolean isExact() {
066: return possibilities.size() == 1;
067: }
068:
069: public TypeSchemeUnion combineWith(TypeSchemeUnion tsu) {
070: SetOfTypeScheme resultPossibilities = SetAsListOfTypeScheme.EMPTY_SET;
071:
072: IteratorOfTypeScheme it = possibilities.iterator();
073: while (it.hasNext()) {
074: TypeScheme myTs = it.next();
075:
076: IteratorOfTypeScheme it2 = tsu.possibilities.iterator();
077: while (it2.hasNext()) {
078: TypeScheme otherTs = it2.next();
079: TypeScheme combinedTs = myTs.combineWith(otherTs);
080: resultPossibilities = resultPossibilities
081: .add(combinedTs);
082: }
083: }
084:
085: return new TypeSchemeUnion(resultPossibilities);
086: }
087:
088: public boolean subSchemeOf(TypeSchemeUnion tsu) {
089: IteratorOfTypeScheme it = possibilities.iterator();
090: while (it.hasNext()) {
091: TypeScheme myTs = it.next();
092:
093: IteratorOfTypeScheme it2 = tsu.possibilities.iterator();
094: while (it2.hasNext()) {
095: TypeScheme otherTs = it2.next();
096: if (myTs.subSchemeOf(otherTs)) {
097: return true;
098: }
099: }
100: }
101:
102: return false;
103: }
104:
105: public boolean equalTo(TypeSchemeUnion tsu) {
106: IteratorOfTypeScheme it = possibilities.iterator();
107: while (it.hasNext()) {
108: TypeScheme myTs = it.next();
109:
110: IteratorOfTypeScheme it2 = tsu.possibilities.iterator();
111: while (it2.hasNext()) {
112: TypeScheme otherTs = it2.next();
113: if (myTs.equalTo(otherTs)) {
114: return true;
115: }
116: }
117: }
118:
119: return false;
120: }
121:
122: public TypeSchemeUnion evaluate() {
123: return this ;
124: }
125:
126: public SetOfTypeSchemeVariable getFreeVars() {
127: return SetAsListOfTypeSchemeVariable.EMPTY_SET;
128: }
129:
130: public String toString() {
131: String result = "[";
132: IteratorOfTypeScheme it = possibilities.iterator();
133: while (it.hasNext()) {
134: result += it.next() + ",";
135: }
136:
137: if (possibilities.size() > 0) {
138: result = result.substring(0, result.length() - 1);
139: }
140:
141: result += "]";
142:
143: return result;
144: }
145: }
|