01: /*
02: * Copyright 2003-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.collections.bag;
17:
18: import java.util.Comparator;
19:
20: import org.apache.commons.collections.SortedBag;
21: import org.apache.commons.collections.Transformer;
22:
23: /**
24: * Decorates another <code>SortedBag</code> to transform objects that are added.
25: * <p>
26: * The add methods are affected by this class.
27: * Thus objects must be removed or searched for using their transformed form.
28: * For example, if the transformation converts Strings to Integers, you must
29: * use the Integer form to remove objects.
30: * <p>
31: * This class is Serializable from Commons Collections 3.1.
32: *
33: * @since Commons Collections 3.0
34: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
35: *
36: * @author Stephen Colebourne
37: */
38: public class TransformedSortedBag extends TransformedBag implements
39: SortedBag {
40:
41: /** Serialization version */
42: private static final long serialVersionUID = -251737742649401930L;
43:
44: /**
45: * Factory method to create a transforming sorted bag.
46: * <p>
47: * If there are any elements already in the bag being decorated, they
48: * are NOT transformed.
49: *
50: * @param bag the bag to decorate, must not be null
51: * @param transformer the transformer to use for conversion, must not be null
52: * @return a new transformed SortedBag
53: * @throws IllegalArgumentException if bag or transformer is null
54: */
55: public static SortedBag decorate(SortedBag bag,
56: Transformer transformer) {
57: return new TransformedSortedBag(bag, transformer);
58: }
59:
60: //-----------------------------------------------------------------------
61: /**
62: * Constructor that wraps (not copies).
63: * <p>
64: * If there are any elements already in the bag being decorated, they
65: * are NOT transformed.
66: *
67: * @param bag the bag to decorate, must not be null
68: * @param transformer the transformer to use for conversion, must not be null
69: * @throws IllegalArgumentException if bag or transformer is null
70: */
71: protected TransformedSortedBag(SortedBag bag,
72: Transformer transformer) {
73: super (bag, transformer);
74: }
75:
76: /**
77: * Gets the decorated bag.
78: *
79: * @return the decorated bag
80: */
81: protected SortedBag getSortedBag() {
82: return (SortedBag) collection;
83: }
84:
85: //-----------------------------------------------------------------------
86: public Object first() {
87: return getSortedBag().first();
88: }
89:
90: public Object last() {
91: return getSortedBag().last();
92: }
93:
94: public Comparator comparator() {
95: return getSortedBag().comparator();
96: }
97:
98: }
|