KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > collections > SortedHashTree


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/collections/SortedHashTree.java,v 1.7 2004/03/24 15:23:37 mstover1 Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.collections;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Comparator JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29 //NOTUSED import org.apache.jorphan.logging.LoggingManager;
30
//NOTUSED import org.apache.log.Logger;
31

32 /**
33  * SortedHashTree is a different implementation of the {@link HashTree}
34  * collection class. In the SortedHashTree, the ordering of values in the tree
35  * is made explicit via the compare() function of objects added to the tree.
36  * This works in exactly the same fashion as it does for a SortedSet.
37  *
38  * @see HashTree
39  * @see HashTreeTraverser
40  *
41  * @author mstover1 at apache.org
42  * @version $Revision: 1.7 $
43  */

44 public class SortedHashTree extends HashTree implements Serializable JavaDoc
45 {
46     //NOTUSED private static Logger log = LoggingManager.getLoggerForClass();
47
protected Comparator JavaDoc comparator;
48
49     public SortedHashTree()
50     {
51         data = new TreeMap JavaDoc();
52     }
53     
54     public SortedHashTree(Comparator JavaDoc comper)
55     {
56         comparator = comper;
57         data = new TreeMap JavaDoc(comparator);
58     }
59
60     public SortedHashTree(Object JavaDoc key)
61     {
62         data = new TreeMap JavaDoc();
63         data.put(key, new SortedHashTree());
64     }
65     
66     public SortedHashTree(Object JavaDoc key,Comparator JavaDoc comper)
67     {
68         comparator = comper;
69         data = new TreeMap JavaDoc(comparator);
70         data.put(key, new SortedHashTree(comparator));
71     }
72
73     public SortedHashTree(Collection JavaDoc keys)
74     {
75         data = new TreeMap JavaDoc();
76         Iterator JavaDoc it = keys.iterator();
77         while (it.hasNext())
78         {
79             data.put(it.next(), new SortedHashTree());
80         }
81     }
82     
83     public SortedHashTree(Collection JavaDoc keys,Comparator JavaDoc comper)
84     {
85         comparator = comper;
86         data = new TreeMap JavaDoc(comparator);
87         Iterator JavaDoc it = keys.iterator();
88         while (it.hasNext())
89         {
90             data.put(it.next(), new SortedHashTree(comparator));
91         }
92     }
93
94     public SortedHashTree(Object JavaDoc[] keys)
95     {
96         data = new TreeMap JavaDoc();
97         for (int x = 0; x < keys.length; x++)
98         {
99             data.put(keys[x], new SortedHashTree());
100         }
101     }
102     
103     public SortedHashTree(Object JavaDoc[] keys,Comparator JavaDoc comper)
104     {
105         comparator = comper;
106         data = new TreeMap JavaDoc(comparator);
107         for (int x = 0; x < keys.length; x++)
108         {
109             data.put(keys[x], new SortedHashTree(comparator));
110         }
111     }
112
113     protected HashTree createNewTree()
114     {
115         if(comparator == null)
116         {
117             return new SortedHashTree();
118         }
119         else
120         {
121             return new SortedHashTree(comparator);
122         }
123     }
124
125     protected HashTree createNewTree(Object JavaDoc key)
126     {
127         if(comparator == null)
128         {
129             return new SortedHashTree(key);
130         }
131         else
132         {
133             return new SortedHashTree(key,comparator);
134         }
135     }
136
137     protected HashTree createNewTree(Collection JavaDoc values)
138     {
139         if(comparator == null)
140         {
141             return new SortedHashTree(values);
142         }
143         else
144         {
145             return new SortedHashTree(values,comparator);
146         }
147     }
148
149     public Object JavaDoc clone()
150     {
151         HashTree newTree = null;
152         if(comparator == null)
153         {
154             newTree = new SortedHashTree();
155         }
156         else
157         {
158             newTree = new SortedHashTree(comparator);
159         }
160         newTree.data = (Map JavaDoc) ((HashMap JavaDoc) data).clone();
161         return newTree;
162     }
163     /**
164      * @param comparator The comparator to set.
165      */

166     public void setComparator(Comparator JavaDoc comparator) {
167         this.comparator = comparator;
168     }
169 }
170
Popular Tags