BrightSide Workbench Full Report + Source Code
UsualUsageComparator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.usual;
19 
20 import java.util.Comparator;
21 import java.util.Map;
22 import java.util.TreeMap;
23 import org.turro.util.CompareUtil;
24 
29 public abstract class UsualUsageComparator<V> implements Comparator<V> {
30 
31  private final String path;
32  private final Map<String, Long> cache = new TreeMap<>();
33 
34  public UsualUsageComparator(String path) {
35  this.path = path;
36  }
37 
38  @Override
39  public int compare(V o1, V o2) {
40  Long i1 = cache.get(path + getIdentifier(o1)),
41  i2 = cache.get(path + getIdentifier(o2));
42  if(i1 == null) {
43  i1 = UsualUsages.getUsageCount(path + getIdentifier(o1));
44  cache.put(path + getIdentifier(o1), i1);
45  }
46  if(i2 == null) {
47  i2 = UsualUsages.getUsageCount(path + getIdentifier(o2));
48  cache.put(path + getIdentifier(o2), i2);
49  }
50  return CompareUtil.compare(i2, i1);
51  }
52 
53  public abstract String getIdentifier(V v);
54 
55 }
abstract String getIdentifier(V v)
static long getUsageCount(String path)