001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.collections;
020:
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.Comparator;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.TreeMap;
028:
029: // NOTUSED import org.apache.jorphan.logging.LoggingManager;
030: // NOTUSED import org.apache.log.Logger;
031:
032: /**
033: * SortedHashTree is a different implementation of the {@link HashTree}
034: * collection class. In the SortedHashTree, the ordering of values in the tree
035: * is made explicit via the compare() function of objects added to the tree.
036: * This works in exactly the same fashion as it does for a SortedSet.
037: *
038: * @see HashTree
039: * @see HashTreeTraverser
040: *
041: * @author mstover1 at apache.org
042: * @version $Revision: 509831 $
043: */
044: public class SortedHashTree extends HashTree implements Serializable {
045: // NOTUSED private static Logger log = LoggingManager.getLoggerForClass();
046: protected transient Comparator comparator;
047:
048: public SortedHashTree() {
049: data = new TreeMap();
050: }
051:
052: public SortedHashTree(Comparator comper) {
053: comparator = comper;
054: data = new TreeMap(comparator);
055: }
056:
057: public SortedHashTree(Object key) {
058: data = new TreeMap();
059: data.put(key, new SortedHashTree());
060: }
061:
062: public SortedHashTree(Object key, Comparator comper) {
063: comparator = comper;
064: data = new TreeMap(comparator);
065: data.put(key, new SortedHashTree(comparator));
066: }
067:
068: public SortedHashTree(Collection keys) {
069: data = new TreeMap();
070: Iterator it = keys.iterator();
071: while (it.hasNext()) {
072: data.put(it.next(), new SortedHashTree());
073: }
074: }
075:
076: public SortedHashTree(Collection keys, Comparator comper) {
077: comparator = comper;
078: data = new TreeMap(comparator);
079: Iterator it = keys.iterator();
080: while (it.hasNext()) {
081: data.put(it.next(), new SortedHashTree(comparator));
082: }
083: }
084:
085: public SortedHashTree(Object[] keys) {
086: data = new TreeMap();
087: for (int x = 0; x < keys.length; x++) {
088: data.put(keys[x], new SortedHashTree());
089: }
090: }
091:
092: public SortedHashTree(Object[] keys, Comparator comper) {
093: comparator = comper;
094: data = new TreeMap(comparator);
095: for (int x = 0; x < keys.length; x++) {
096: data.put(keys[x], new SortedHashTree(comparator));
097: }
098: }
099:
100: protected HashTree createNewTree() {
101: if (comparator == null) {
102: return new SortedHashTree();
103: } else {
104: return new SortedHashTree(comparator);
105: }
106: }
107:
108: protected HashTree createNewTree(Object key) {
109: if (comparator == null) {
110: return new SortedHashTree(key);
111: } else {
112: return new SortedHashTree(key, comparator);
113: }
114: }
115:
116: protected HashTree createNewTree(Collection values) {
117: if (comparator == null) {
118: return new SortedHashTree(values);
119: } else {
120: return new SortedHashTree(values, comparator);
121: }
122: }
123:
124: public Object clone() {
125: HashTree newTree = null;
126: if (comparator == null) {
127: newTree = new SortedHashTree();
128: } else {
129: newTree = new SortedHashTree(comparator);
130: }
131: newTree.data = (Map) ((HashMap) data).clone();
132: return newTree;
133: }
134:
135: /**
136: * @param comparator
137: * The comparator to set.
138: */
139: public void setComparator(Comparator comparator) {
140: this.comparator = comparator;
141: }
142: }
|