001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.model.extended;
010:
011: public class PatternSet {
012: private PatternSetEntry first = null;
013:
014: public PatternSet() {
015: }
016:
017: public PatternSet(PatternIterator pattern) {
018: while (pattern.hasNext())
019: addPattern(pattern.next());
020: }
021:
022: public boolean addPattern(Pattern pattern) {
023: if (pattern == null)
024: throw new NullPointerException();
025:
026: for (PatternSetEntry entry = first; entry != null; entry = entry.next)
027: if (entry.pattern == pattern)
028: return false;
029:
030: first = new PatternSetEntry(pattern, first);
031: return true;
032: }
033:
034: public boolean addPattern(PatternSet set) {
035: boolean modified = false;
036: for (PatternSetEntry entry = set.first; entry != null; entry = entry.next)
037: modified |= addPattern(entry.pattern);
038:
039: return modified;
040: }
041:
042: public PatternIterator getPattern() {
043: return new PatternSetEntryIterator(first);
044: }
045:
046: public boolean contains(Pattern pattern) {
047: for (PatternSetEntry entry = first; entry != null; entry = entry.next)
048: if (entry.pattern == pattern)
049: return true;
050:
051: return false;
052: }
053:
054: public int getPatternCount() {
055: int count = 0;
056: for (PatternSetEntry entry = first; entry != null; entry = entry.next)
057: count++;
058:
059: return count;
060: }
061:
062: public void clear() {
063: first = null;
064: }
065:
066: public boolean equals(Object o) {
067: if (o instanceof PatternSet) {
068: PatternSet set = (PatternSet) o;
069:
070: if (set.getPatternCount() != getPatternCount())
071: return false;
072:
073: for (PatternSetEntry entry = first; entry != null; entry = entry.next)
074: for (PatternSetEntry foreignentry = set.first; foreignentry != null; foreignentry = foreignentry.next) {
075: if (entry.pattern == foreignentry.pattern)
076: break;
077:
078: if (foreignentry.next == null)
079: return false;
080: }
081:
082: return true;
083: }
084:
085: return false;
086: }
087:
088: public String toString() {
089: StringBuffer buffer = new StringBuffer();
090:
091: buffer.append("{");
092: for (PatternSetEntry entry = first; entry != null; entry = entry.next) {
093: if (entry != first)
094: buffer.append(",");
095:
096: buffer.append(entry.pattern.toString());
097: }
098:
099: buffer.append("}");
100: return buffer.toString();
101: }
102:
103: private class PatternSetEntry {
104: public final Pattern pattern;
105: public final PatternSetEntry next;
106:
107: private PatternSetEntry(Pattern pattern, PatternSetEntry next) {
108: this .pattern = pattern;
109: this .next = next;
110: }
111: }
112:
113: public class PatternSetEntryIterator implements PatternIterator {
114: private PatternSetEntry entry = null;
115:
116: private PatternSetEntryIterator(PatternSetEntry entry) {
117: this .entry = entry;
118: }
119:
120: public boolean hasNext() {
121: return entry != null;
122: }
123:
124: public Pattern next() {
125: Pattern pattern = entry.pattern;
126: this.entry = entry.next;
127: return pattern;
128: }
129: }
130: }
|