1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.eos.trie;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map.Entry;
21
22 import net.sf.eos.trie.Trie.Cursor;
23
24
25
26
27
28
29
30
31
32
33 public final class TrieUtils {
34
35 private TrieUtils() {}
36
37 public static <K,V> List<V> select(Trie<K,V> trie, K key, int count) {
38 return select(trie, key, count, null);
39 }
40
41 public static <K,V> List<V> select(Trie<K,V> trie, K key,
42 int count, final Cursor<K,V> cursor) {
43
44 final int size = Math.min(trie.size(), count);
45 final List<V> values = new ArrayList<V>(size);
46
47 trie.select(key, new Cursor<K,V>() {
48 public Cursor.SelectStatus select(Entry<? extends K, ? extends V> entry) {
49 if (cursor == null || cursor.select(entry) == Cursor.SelectStatus.EXIT) {
50 values.add(entry.getValue());
51 }
52
53 return values.size() >= size ? Cursor.SelectStatus.EXIT : Cursor.SelectStatus.CONTINUE;
54 }
55 });
56
57 return values;
58 }
59 }