001: /*
002: * FilterMap
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import java.util.*;
012:
013: class FilterMap extends AbstractMap {
014: private Map m;
015: boolean dualArgs;
016:
017: protected FilterMap() {
018: }
019:
020: public FilterMap(Map m, boolean dualArgs) {
021: this .m = m;
022: this .dualArgs = dualArgs;
023: }
024:
025: protected boolean shouldInclude(Object key) {
026: return true;
027: }
028:
029: protected boolean shouldInclude(Object key, Object value) {
030: return true;
031: }
032:
033: public Object get(Object key) {
034: Object value = m.get(key);
035: if ((dualArgs && shouldInclude(key, value))
036: || shouldInclude(key)) {
037: return value;
038: } else {
039: return null;
040: }
041: }
042:
043: public boolean containsKey(Object key) {
044: if (dualArgs) {
045: return (m.containsKey(key) && shouldInclude(key, get(key)));
046: } else {
047: return (m.containsKey(key) && shouldInclude(key));
048: }
049: }
050:
051: public Object put(Object key, Object value) {
052: throw new UnsupportedOperationException();
053: }
054:
055: public void putAll(Map m) {
056: throw new UnsupportedOperationException();
057: }
058:
059: public Object remove(Object obj) {
060: throw new UnsupportedOperationException();
061: }
062:
063: public void clear() {
064: throw new UnsupportedOperationException();
065: }
066:
067: public Set keySet() {
068: return new FilterSet(m.keySet());
069: }
070:
071: public Set entrySet() {
072: return new EntrySet(m.entrySet());
073: }
074:
075: public Collection values() {
076: return new AbstractCollection() {
077: public Iterator iterator() {
078: return new ProjectionIterator(entrySet().iterator()) {
079: protected Object project(Object obj) {
080: Map.Entry entry = (Map.Entry) obj;
081: return entry.getValue();
082: }
083: };
084: }
085:
086: public int size() {
087: return entrySet().size();
088: }
089: };
090: }
091:
092: final class FilterSet extends AbstractSet {
093: private Set set;
094:
095: FilterSet(Set set) {
096: this .set = set;
097: }
098:
099: public Iterator iterator() {
100: if (dualArgs) {
101: return new FilterIterator(set.iterator()) {
102: protected boolean shouldInclude(Object element) {
103: Object value = FilterMap.this .get(element);
104: return FilterMap.this .shouldInclude(element,
105: value);
106: }
107: };
108: } else {
109: return new FilterIterator(set.iterator()) {
110: protected boolean shouldInclude(Object element) {
111: return FilterMap.this .shouldInclude(element);
112: }
113: };
114: }
115: }
116:
117: public int size() {
118: // scan
119: int c = 0;
120: for (Iterator it = iterator(); it.hasNext(); it.next()) {
121: c++;
122: }
123: return c;
124: }
125: }
126:
127: private final class EntrySet extends AbstractSet {
128: Set entrySet;
129:
130: EntrySet(Set entrySet) {
131: this .entrySet = entrySet;
132: }
133:
134: public Iterator iterator() {
135: if (dualArgs) {
136: return new FilterIterator(entrySet.iterator()) {
137: protected boolean shouldInclude(Object element) {
138: Map.Entry entry = (Map.Entry) element;
139: return FilterMap.this .shouldInclude(entry
140: .getKey(), entry.getValue());
141: }
142: };
143: } else {
144: return new FilterIterator(entrySet.iterator()) {
145: protected boolean shouldInclude(Object element) {
146: Map.Entry entry = (Map.Entry) element;
147: return FilterMap.this .shouldInclude(entry
148: .getKey());
149: }
150: };
151: }
152: }
153:
154: public boolean contains(Object o) {
155: if (o instanceof Map.Entry) {
156: Map.Entry entry = (Map.Entry) o;
157: Object key = entry.getKey();
158: Object value = entry.getValue();
159: if (dualArgs) {
160: return (shouldInclude(key, value) && containsKey(key));
161: } else {
162: return (shouldInclude(key) && containsKey(key));
163: }
164: }
165: return false;
166: }
167:
168: public boolean remove(Object o) {
169: return false;
170: }
171:
172: public int size() {
173: // scan
174: int c = 0;
175: for (Iterator it = iterator(); it.hasNext(); it.next()) {
176: c++;
177: }
178: return c;
179: }
180:
181: public void clear() {
182: FilterMap.this.clear();
183: }
184: }
185: }
|