| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CacheKeyUtils |
|
| 4.666666666666667;4.667 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community 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 | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 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. | |
| 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 | * This is a utility class that generates cache keys for compound objects. | |
| 30 | */ | |
| 31 | public final class CacheKeyUtils { | |
| 32 | ||
| 33 | 0 | private CacheKeyUtils() { |
| 34 | 0 | throw new UnsupportedOperationException("do not call."); |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * Create a String key value out of a Map. The Map should really just contain | |
| 39 | * simple types. | |
| 40 | * @param map the map. if null will return "" | |
| 41 | * @param <K> the map key type | |
| 42 | * @return the map as a string value | |
| 43 | */ | |
| 44 | public static <K extends Comparable<K>> String key(Map<K, ?> map) { | |
| 45 | 0 | if (map == null) { |
| 46 | 0 | return ""; |
| 47 | } | |
| 48 | ||
| 49 | 0 | final SortedMap<K, ?> sorted = new TreeMap<K, Object>(map); |
| 50 | 0 | final StringBuilder b = new StringBuilder("["); |
| 51 | 0 | for (Map.Entry<K, ?> entry : sorted.entrySet()) { |
| 52 | 0 | if (entry != null && entry.getKey() != null && entry.getValue() != null) { |
| 53 | 0 | b.append(entry.getKey()); |
| 54 | 0 | b.append(":"); |
| 55 | 0 | b.append(entry.getValue()); |
| 56 | 0 | b.append(","); |
| 57 | } | |
| 58 | } | |
| 59 | 0 | b.append("]"); |
| 60 | 0 | return b.toString(); |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * Create a String key value out of a Collection. The Collection should really just contain | |
| 65 | * simple types. | |
| 66 | * @param col the collection. if null will return "" | |
| 67 | * @param <K> the col type | |
| 68 | * @return the collection as a string value | |
| 69 | */ | |
| 70 | public static <K extends Comparable<K>> String key(Collection<K> col) { | |
| 71 | 0 | if (col == null) { |
| 72 | 0 | return ""; |
| 73 | } | |
| 74 | ||
| 75 | 0 | final List<K> sorted = new ArrayList<K>(col); |
| 76 | 0 | Collections.sort(sorted); |
| 77 | 0 | final StringBuilder b = new StringBuilder("["); |
| 78 | 0 | for (K entry : sorted) { |
| 79 | 0 | if (entry != null) { |
| 80 | 0 | b.append(entry); |
| 81 | 0 | b.append(","); |
| 82 | } | |
| 83 | } | |
| 84 | 0 | b.append("]"); |
| 85 | 0 | return b.toString(); |
| 86 | } | |
| 87 | ||
| 88 | } |