001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.set;
017:
018: import java.util.Comparator;
019: import java.util.SortedSet;
020:
021: import org.apache.commons.collections.Predicate;
022:
023: /**
024: * Decorates another <code>SortedSet</code> to validate that all additions
025: * match a specified predicate.
026: * <p>
027: * This set exists to provide validation for the decorated set.
028: * It is normally created to decorate an empty set.
029: * If an object cannot be added to the set, an IllegalArgumentException is thrown.
030: * <p>
031: * One usage would be to ensure that no null entries are added to the set.
032: * <pre>SortedSet set = PredicatedSortedSet.decorate(new TreeSet(), NotNullPredicate.INSTANCE);</pre>
033: * <p>
034: * This class is Serializable from Commons Collections 3.1.
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: * @author Paul Jack
041: */
042: public class PredicatedSortedSet extends PredicatedSet implements
043: SortedSet {
044:
045: /** Serialization version */
046: private static final long serialVersionUID = -9110948148132275052L;
047:
048: /**
049: * Factory method to create a predicated (validating) sorted set.
050: * <p>
051: * If there are any elements already in the set being decorated, they
052: * are validated.
053: *
054: * @param set the set to decorate, must not be null
055: * @param predicate the predicate to use for validation, must not be null
056: * @throws IllegalArgumentException if set or predicate is null
057: * @throws IllegalArgumentException if the set contains invalid elements
058: */
059: public static SortedSet decorate(SortedSet set, Predicate predicate) {
060: return new PredicatedSortedSet(set, predicate);
061: }
062:
063: //-----------------------------------------------------------------------
064: /**
065: * Constructor that wraps (not copies).
066: * <p>
067: * If there are any elements already in the set being decorated, they
068: * are validated.
069: *
070: * @param set the set to decorate, must not be null
071: * @param predicate the predicate to use for validation, must not be null
072: * @throws IllegalArgumentException if set or predicate is null
073: * @throws IllegalArgumentException if the set contains invalid elements
074: */
075: protected PredicatedSortedSet(SortedSet set, Predicate predicate) {
076: super (set, predicate);
077: }
078:
079: /**
080: * Gets the sorted set being decorated.
081: *
082: * @return the decorated sorted set
083: */
084: private SortedSet getSortedSet() {
085: return (SortedSet) getCollection();
086: }
087:
088: //-----------------------------------------------------------------------
089: public SortedSet subSet(Object fromElement, Object toElement) {
090: SortedSet sub = getSortedSet().subSet(fromElement, toElement);
091: return new PredicatedSortedSet(sub, predicate);
092: }
093:
094: public SortedSet headSet(Object toElement) {
095: SortedSet sub = getSortedSet().headSet(toElement);
096: return new PredicatedSortedSet(sub, predicate);
097: }
098:
099: public SortedSet tailSet(Object fromElement) {
100: SortedSet sub = getSortedSet().tailSet(fromElement);
101: return new PredicatedSortedSet(sub, predicate);
102: }
103:
104: public Object first() {
105: return getSortedSet().first();
106: }
107:
108: public Object last() {
109: return getSortedSet().last();
110: }
111:
112: public Comparator comparator() {
113: return getSortedSet().comparator();
114: }
115:
116: }
|