This repository was archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSortMerge.java
More file actions
254 lines (220 loc) · 8.01 KB
/
SortMerge.java
File metadata and controls
254 lines (220 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* @(#)SortMerge.java 1.12 06/10/30
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package javax.help;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.*;
import java.text.*;
/**
* Sort merge type
*
* @author Richard Gregor
* @version 1.12 10/30/06
*/
public class SortMerge extends Merge{
/**
* Constructs SortMerge
*
* @param master The master NavigatorView
* @param slave The slave NavigatorView
*/
public SortMerge(NavigatorView master, NavigatorView slave) {
super(master,slave);
}
/**
* Processes sort merge
*
* @param node The master node (This node must be sorted)
* @return Merged master node
*/
public TreeNode processMerge(TreeNode node) {
debug("processMerge started");
DefaultMutableTreeNode masterNode = (DefaultMutableTreeNode) node;
//if master and slave are the same object return the
// masterNode
if (masterNode.equals(slaveTopNode)) {
return masterNode;
}
// If there are no children in slaveTopNode return the
// masterNode
if (slaveTopNode.getChildCount() == 0) {
return masterNode;
}
// If there are no children in the masterNode process the slaveTopNode
// and return the slaveTopNode
if (masterNode.getChildCount() == 0) {
MergeHelpUtilities.mergeNodeChildren("javax.help.SortMerge",
slaveTopNode);
// It would be better to return the slaveNode but that causes
// problems in the UIs so return the masterNode after adding
// all the slavNodes
while (slaveTopNode.getChildCount() > 0) {
masterNode.add((DefaultMutableTreeNode) slaveTopNode.getFirstChild());
}
return masterNode;
}
mergeNodes(masterNode, slaveTopNode);
debug("process merge ended");
return masterNode;
}
/**
* Merge Nodes. Merge two nodes according to the Sort merging rules
*
* @param masterNode The master node to merge with
* @param slaveNode The node to merge into the master
*/
public static void mergeNodes(TreeNode master, TreeNode slave) {
debug("mergeNodes started");
DefaultMutableTreeNode masterNode = (DefaultMutableTreeNode)master;
DefaultMutableTreeNode slaveNode = (DefaultMutableTreeNode)slave;
// It is important that the masterNode already be sorted
sortNode(slaveNode, MergeHelpUtilities.getLocale(slaveNode));
int masterCnt = masterNode.getChildCount();
int m = 0;
DefaultMutableTreeNode masterAtM=null;
if (masterCnt > 0) {
masterAtM = (DefaultMutableTreeNode)masterNode.getChildAt(m);
}
DefaultMutableTreeNode slaveNodeChild = null;
//Loop through the slaves
while (slaveNode.getChildCount() > 0 && masterAtM != null) {
slaveNodeChild = (DefaultMutableTreeNode)slaveNode.getFirstChild();
// compare this slaveChild to the masterChild
int compareVal = MergeHelpUtilities.compareNames(masterAtM,
slaveNodeChild);
// if < 0 get the next master child
if (compareVal < 0 ) {
++m;
// break out if we've done all we can do.
if (m >= masterCnt) {
break;
}
masterAtM = (DefaultMutableTreeNode)masterNode.getChildAt(m);
continue;
} else if (compareVal > 0) {
// slaveNodeChild is lexically procedes the masterNodeChild
// add the slaveNodeChild and merge in the node children
// and then continue. You automatically get the next
// slaveNodeChild
masterNode.add(slaveNodeChild);
MergeHelpUtilities.mergeNodeChildren("javax.help.SortMerge",
slaveNodeChild);
continue;
} else {
// The masterChild and slaveChild are equal
if (MergeHelpUtilities.haveEqualID(masterAtM, slaveNodeChild)) {
// Merge the masterchild and slaveNodeChild together
// based on the mergetype of the masterChild
MergeHelpUtilities.mergeNodes("javax.help.SortMerge",
masterAtM, slaveNodeChild);
// Need to remove the slaveNodeChild from the list
slaveNodeChild.removeFromParent();
slaveNodeChild = null;
} else {
// Names are the same but the ID are not
// Mark the nodes and add the slaveChild
MergeHelpUtilities.markNodes(masterAtM, slaveNodeChild);
masterNode.add(slaveNodeChild);
MergeHelpUtilities.mergeNodeChildren("javax.help.SortMerge",
slaveNodeChild);
}
}
}
// Add the remainong slave node children
while (slaveNode.getChildCount() > 0) {
slaveNodeChild = (DefaultMutableTreeNode)slaveNode.getFirstChild();
masterNode.add(slaveNodeChild);
MergeHelpUtilities.mergeNodeChildren("javax.help.SortMerge",
slaveNodeChild);
}
// ok make sure the masterNode is sorted
mergeNodeChildren(masterNode);
debug ("mergeNode ended");
}
/**
* Merge Node Children. Merge the children of a node according to the
* Sort merging rules.
*
* @param node The parent node from which the children are merged
*/
public static void mergeNodeChildren(TreeNode node) {
DefaultMutableTreeNode masterNode = (DefaultMutableTreeNode)node;
debug("mergeNodeChildren master=" + MergeHelpUtilities.getNodeName(masterNode));
// Sort the children
sortNode(masterNode, MergeHelpUtilities.getLocale(masterNode));
// merge the children's children
for (int i=0; i < masterNode.getChildCount(); i++) {
DefaultMutableTreeNode child =
(DefaultMutableTreeNode)masterNode.getChildAt(i);
if (!child.isLeaf()) {
MergeHelpUtilities.mergeNodeChildren("javax.help.SortMerge",
child);
}
}
}
/**
* Sorts children of node using Array.sort
*
* @param node The node to sort
* @param locale The locale
* @return Sorted node
*/
public static void sortNode(DefaultMutableTreeNode node, Locale locale) {
debug ("sortNode");
if (locale == null) {
locale = Locale.getDefault();
}
//Locale locale = MergeHelpUtilities.getLocale(node);
CollationKey temp;
int size = node.getChildCount();
DefaultMutableTreeNode sortedNode = new DefaultMutableTreeNode();
Collator collator = Collator.getInstance(locale);
CollationKey[] keys = new CollationKey[size];
for (int i = 0; i < size; i++) {
String string = MergeHelpUtilities.getNodeName((DefaultMutableTreeNode)node.getChildAt(i));
debug("String , i:"+string+" , "+i);
keys[i] = collator.getCollationKey(string);
}
Arrays.sort(keys);
for (int i = 0; i < size; i++) {
DefaultMutableTreeNode child = MergeHelpUtilities.getChildWithName(node,keys[i].getSourceString());
if (child != null) {
sortedNode.add(child);
}
}
while (sortedNode.getChildCount() > 0) {
node.add((DefaultMutableTreeNode) sortedNode.getFirstChild());
}
debug ("end sortNode");
}
private static boolean debug = false;
private static void debug(String msg) {
if (debug) {
System.out.println("SortMerge :"+msg);
}
}
}