summaryrefslogtreecommitdiff
path: root/platform/structure-view-api/src/com/intellij/ide/util/treeView/AlphaComparator.java
blob: c97bfe5c929646cfe6a7797d9f434d119a9dd7f0 (plain)
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
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide.util.treeView;

import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;

import java.util.Comparator;

public class AlphaComparator implements Comparator<NodeDescriptor>{
  public static final AlphaComparator INSTANCE = new AlphaComparator();

  protected AlphaComparator() {
  }

  @Override
  public int compare(NodeDescriptor nodeDescriptor1, NodeDescriptor nodeDescriptor2) {
    int weight1 = nodeDescriptor1.getWeight();
    int weight2 = nodeDescriptor2.getWeight();
    if (weight1 != weight2) {
      return weight1 - weight2;
    }
    String s1 = nodeDescriptor1.toString();
    String s2 = nodeDescriptor2.toString();
    if (s1 == null) return s2 == null ? 0 : -1;
    if (s2 == null) return +1;

    //for super natural comparison (IDEA-80435)
    Pair<String,String> normalized = normalize(s1, s2);
    return StringUtil.naturalCompare(normalized.first, normalized.second);
  }

  private static Pair<String, String> normalize(String s1, String s2) {
    final int minLen = Math.min(s1.length(), s2.length());
    final StringBuilder sb1 = new StringBuilder(s1);
    final StringBuilder sb2 = new StringBuilder(s2);
    for (int i = 0; i < minLen; i++) {
      final char ch1 = s1.charAt(i);
      final char ch2 = sb2.charAt(i);
      if (ch1 == ch2 && ch1 == '-') {
        sb1.setCharAt(i, '_');
        sb2.setCharAt(i, '_');
      } else if (ch1 == '-' && ch2 != '_') {
        sb1.setCharAt(i, '_');
      } else if (ch2 == '-' && ch1 != '_') {
        sb2.setCharAt(i, '_');
      }
    }

    s1 = sb1.toString();
    s2 = sb2.toString();
    return Pair.create(s1, s2);
  }
}