1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.api.cache;
17
18
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.SortedMap;
26 import java.util.TreeMap;
27
28
29
30
31 public final class CacheKeyUtils {
32
33 private CacheKeyUtils() {
34 throw new UnsupportedOperationException("do not call.");
35 }
36
37
38
39
40
41
42
43
44 public static <K extends Comparable<K>> String key(Map<K, ?> map) {
45 if (map == null) {
46 return "";
47 }
48
49 final SortedMap<K, ?> sorted = new TreeMap<K, Object>(map);
50 final StringBuilder b = new StringBuilder("[");
51 for (Map.Entry<K, ?> entry : sorted.entrySet()) {
52 if (entry != null && entry.getKey() != null && entry.getValue() != null) {
53 b.append(entry.getKey());
54 b.append(":");
55 b.append(entry.getValue());
56 b.append(",");
57 }
58 }
59 b.append("]");
60 return b.toString();
61 }
62
63
64
65
66
67
68
69
70 public static <K extends Comparable<K>> String key(Collection<K> col) {
71 if (col == null) {
72 return "";
73 }
74
75 final List<K> sorted = new ArrayList<K>(col);
76 Collections.sort(sorted);
77 final StringBuilder b = new StringBuilder("[");
78 for (K entry : sorted) {
79 if (entry != null) {
80 b.append(entry);
81 b.append(",");
82 }
83 }
84 b.append("]");
85 return b.toString();
86 }
87
88 }