2 * Copyright 2000-2007 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.intellij
.ui
.treeStructure
;
18 import com
.intellij
.ide
.util
.treeView
.NodeDescriptor
;
20 import java
.util
.Comparator
;
22 public class WeightBasedComparator
implements Comparator
<NodeDescriptor
> {
24 private boolean myCompareToString
;
26 public static final WeightBasedComparator INSTANCE
= new WeightBasedComparator();
28 public WeightBasedComparator() {
32 public WeightBasedComparator(final boolean compareToString
) {
33 myCompareToString
= compareToString
;
36 public int compare(NodeDescriptor o1
, NodeDescriptor o2
) {
37 SimpleNode first
= (SimpleNode
) o1
;
38 SimpleNode second
= (SimpleNode
) o2
;
40 if (myCompareToString
&& first
.getWeight() == second
.getWeight()) {
41 String s1
= first
.toString();
42 String s2
= second
.toString();
43 if (s1
== null) return s2
== null ?
0 : -1;
44 if (s2
== null) return +1;
45 return s1
.compareToIgnoreCase(s2
);
47 return first
.getWeight() - second
.getWeight();