KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > TransformedSortedBag


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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: 1.7 $ $Date: 2004/06/03 22:02:12 $
35  *
36  * @author Stephen Colebourne
37  */

38 public class TransformedSortedBag
39         extends TransformedBag implements 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, Transformer transformer) {
56         return new TransformedSortedBag(bag, transformer);
57     }
58     
59     //-----------------------------------------------------------------------
60
/**
61      * Constructor that wraps (not copies).
62      * <p>
63      * If there are any elements already in the bag being decorated, they
64      * are NOT transformed.
65      *
66      * @param bag the bag to decorate, must not be null
67      * @param transformer the transformer to use for conversion, must not be null
68      * @throws IllegalArgumentException if bag or transformer is null
69      */

70     protected TransformedSortedBag(SortedBag bag, Transformer transformer) {
71         super(bag, transformer);
72     }
73
74     /**
75      * Gets the decorated bag.
76      *
77      * @return the decorated bag
78      */

79     protected SortedBag getSortedBag() {
80         return (SortedBag) collection;
81     }
82
83     //-----------------------------------------------------------------------
84
public Object JavaDoc first() {
85         return getSortedBag().first();
86     }
87
88     public Object JavaDoc last() {
89         return getSortedBag().last();
90     }
91
92     public Comparator JavaDoc comparator() {
93         return getSortedBag().comparator();
94     }
95
96 }
97
Popular Tags