diff options
author | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-07-10 15:28:25 +0200 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-07-10 15:28:25 +0200 |
commit | d774a81910498c9ee1277c1611d57b07bf069fbd (patch) | |
tree | fe4e2d9ce1b1d4b3bd63c0802947d8fc1ed20124 /id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils | |
parent | ff9703e221414e9840638911b53f441eb86afb72 (diff) | |
download | moa-id-spss-d774a81910498c9ee1277c1611d57b07bf069fbd.tar.gz moa-id-spss-d774a81910498c9ee1277c1611d57b07bf069fbd.tar.bz2 moa-id-spss-d774a81910498c9ee1277c1611d57b07bf069fbd.zip |
First parts of the new MOA-ID configuration module
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils')
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils/KeyValueUtils.java | 134 |
1 files changed, 125 insertions, 9 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils/KeyValueUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils/KeyValueUtils.java index 626db2167..0e4616825 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils/KeyValueUtils.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/utils/KeyValueUtils.java @@ -22,23 +22,35 @@ */ package at.gv.egovernment.moa.id.commons.utils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import at.gv.egovernment.moa.util.MiscUtil; + /** * @author tlenz * */ public class KeyValueUtils { + public static final String KEY_DELIMITER = "."; + /** * Extract the first child of an input key after a the prefix * - * @param key: Full input key - * @param prefix: Prefix + * @param key Full input key + * @param prefix Prefix * @return Child key {String} if it exists or null */ public static String getFirstChildAfterPrefix(String key, String prefix) { String idAfterPrefix = removePrefixFromKey(key, prefix); if (idAfterPrefix != null) { - int index = idAfterPrefix.indexOf("."); + int index = idAfterPrefix.indexOf(KEY_DELIMITER); if (index > 0) { String adding = idAfterPrefix.substring(0, index); if (!(adding.isEmpty())) { @@ -57,14 +69,14 @@ public class KeyValueUtils { /** * Extract the prefix from an input key * - * @param key: Full input key - * @param suffix: Suffix of this key + * @param key Full input key + * @param suffix Suffix of this key * @return Prefix {String} of the key or null if input key does not ends with postfix string */ public static String getPrefixFromKey(String key, String suffix) { if (key != null && key.endsWith(suffix)) { String idPreforeSuffix = key.substring(0, key.length()-suffix.length()); - if (idPreforeSuffix.endsWith(".")) + if (idPreforeSuffix.endsWith(KEY_DELIMITER)) return idPreforeSuffix.substring(0, idPreforeSuffix.length()-1); else return idPreforeSuffix; @@ -76,8 +88,8 @@ public class KeyValueUtils { /** * Remove a prefix string from a key * - * @param key: Full input key - * @param prefix: Prefix, which should be removed + * @param key Full input key + * @param prefix Prefix, which should be removed * @return The suffix of the input key or null if the input does not starts with the prefix */ public static String removePrefixFromKey(String key, String prefix) { @@ -86,7 +98,7 @@ public class KeyValueUtils { if (key!=null && key.startsWith(prefix)) { String afterPrefix = key.substring(prefix.length()); - int index = afterPrefix.indexOf("."); + int index = afterPrefix.indexOf(KEY_DELIMITER); if (index == 0) { afterPrefix = afterPrefix.substring(1); @@ -98,4 +110,108 @@ public class KeyValueUtils { return null; } + /** + * Remove a prefix string from all keys in {Map<String, String>} of key/value pairs + * + * @param keys Input data of key/value pairs + * @param prefix Prefix which should be removed + * @return {Map<String, String>} of key/value pairs without prefix in key, but never null + */ + public static Map<String, String> removePrefixFromKeys(Map<String, String> keys, String prefix) { + Map<String, String> result = new HashMap<String, String>(); + Iterator<Entry<String, String>> interator = keys.entrySet().iterator(); + while(interator.hasNext()) { + Entry<String, String> el = interator.next(); + String newKey = removePrefixFromKey(el.getKey(), prefix); + if (MiscUtil.isNotEmpty(newKey)) { + result.put(newKey, el.getValue()); + } + } + + return result; + } + + /** + * Get a subset of key/value pairs which starts with a prefix string + * The Prefix is removed from the key + * + * @param keys Input data of key/value pairs + * @param prefix Prefix string + * @return {Map<String, String>} of key/value pairs without prefix in key, but never null + */ + public static Map<String, String> getSubSetWithPrefix(Map<String, String> keys, String prefix) { + return removePrefixFromKeys(keys, prefix); + } + + + /** + * Add a prefix to key/value pairs to make the key absolute according to key namespace convention + * + * @param input Input key/value pairs which should be updated + * @param prefix Key prefix, which should be added if the key is not absolute + * @param absolutIdentifier Key identifier, which indicates an absolute key + * @return {Map<String, String>} of key/value pairs in which all keys are absolute but never null + */ + public static Map<String, String> makeKeysAbsolut(Map<String, String> input, String prefix, String absolutIdentifier) { + Map<String, String> result = new HashMap<String, String>(); + Iterator<Entry<String, String>> interator = input.entrySet().iterator(); + while(interator.hasNext()) { + Entry<String, String> el = interator.next(); + if (!el.getKey().startsWith(absolutIdentifier)) { + //key is not absolute -> add prefix + result.put(prefix + + KEY_DELIMITER + + el.getKey(), + el.getValue()); + + } else { + //key is absolute + result.put(el.getKey(), el.getValue()); + } + } + return result; + } + + /** + * Get the parent key string from an input key + * + * @param key input key + * @return parent key or the empty String if no parent exists + */ + public static String getParentKey(String key) { + if (MiscUtil.isNotEmpty(key)) { + int index = key.lastIndexOf(KEY_DELIMITER); + if (index > 0) { + return key.substring(0, index); + + } + } + + return new String(); + } + + /** + * Find the highest free list counter + * + * @param input Array of list keys + * @param listPrefix {String} prefix of the list + * @return {int} highest free list counter + */ + public static int findNextFreeListCounter(String[] input, + String listPrefix) { + List<Integer> counters = new ArrayList<Integer>(); + if (input == null || input.length == 0) + return 0; + + else { + for (String key : input) { + String listIndex = getFirstChildAfterPrefix(key, listPrefix); + counters.add(Integer.parseInt(listIndex)); + + } + Collections.sort(counters); + return counters.get(counters.size()-1) + 1; + } + } + } |