From ad2cfb201d37857a51b350bf6b2e5e3ab7adf19a Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 12 Mar 2020 15:49:01 -0400 Subject: Move code from drools-apps to common Extracted code from ControlLoopUtils to create a new class in common, PropertyObjectUtils. Fixed a deprecated method invocation in a junit. Fixed an object casting (i.e., sonar) issue in SCO. Issue-ID: POLICY-2305 Signed-off-by: Jim Hahn Change-Id: I331a47297f67097ea6986be125ef93cd1954b5ff --- .../utils/properties/PropertyObjectUtils.java | 243 +++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java (limited to 'utils/src/main/java') diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java new file mode 100644 index 00000000..996f1b87 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java @@ -0,0 +1,243 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.properties; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utilities for generating POJOs from Properties. + */ +public class PropertyObjectUtils { + + public static final Logger logger = LoggerFactory.getLogger(PropertyObjectUtils.class); + private static final Pattern NAME_PAT = Pattern.compile("\\[(\\d{1,3})\\]$"); + + private PropertyObjectUtils() { + // do nothing + } + + /** + * Converts a set of properties to a Map. Supports json-path style property names with + * "." separating components, where components may have an optional subscript. + * + * @param properties properties to be converted + * @param prefix properties whose names begin with this prefix are included. The + * prefix is stripped from the name before adding the value to the map + * @return a hierarchical map representing the properties + */ + public static Map toObject(Properties properties, String prefix) { + String dottedPrefix = prefix + (prefix.isEmpty() || prefix.endsWith(".") ? "" : "."); + int pfxlen = dottedPrefix.length(); + + Map map = new LinkedHashMap<>(); + + for (String name : properties.stringPropertyNames()) { + if (name.startsWith(dottedPrefix)) { + String[] components = name.substring(pfxlen).split("[.]"); + setProperty(map, components, properties.getProperty(name)); + } + } + + return map; + } + + /** + * Sets a property within a hierarchical map. + * + * @param map map into which the value should be placed + * @param names property name components + * @param value value to be placed into the map + */ + private static void setProperty(Map map, String[] names, String value) { + Map node = map; + + final int lastComp = names.length - 1; + + // process all but the final component + for (int comp = 0; comp < lastComp; ++comp) { + node = getNode(node, names[comp]); + } + + // process the final component + String name = names[lastComp]; + Matcher matcher = NAME_PAT.matcher(name); + + if (!matcher.find()) { + // no subscript + node.put(name, value); + return; + } + + // subscripted + List array = getArray(node, name.substring(0, matcher.start())); + int index = Integer.parseInt(matcher.group(1)); + expand(array, index); + array.set(index, value); + } + + /** + * Gets a node. + * + * @param map map from which to get the object + * @param name name of the element to get from the map, with an optional subscript + * @return a Map + */ + @SuppressWarnings("unchecked") + private static Map getNode(Map map, String name) { + Matcher matcher = NAME_PAT.matcher(name); + + if (!matcher.find()) { + // no subscript + return getObject(map, name); + } + + // subscripted + List array = getArray(map, name.substring(0, matcher.start())); + int index = Integer.parseInt(matcher.group(1)); + expand(array, index); + + Object item = array.get(index); + if (item instanceof Map) { + return (Map) item; + + } else { + LinkedHashMap result = new LinkedHashMap<>(); + array.set(index, result); + return result; + } + } + + /** + * Ensures that an array's size is large enough to hold the specified element. + * + * @param array array to be expanded + * @param index index of the desired element + */ + private static void expand(List array, int index) { + while (array.size() <= index) { + array.add(null); + } + } + + /** + * Gets an object (i.e., Map) from a map. If the particular element is not a Map, then + * it is replaced with an empty Map. + * + * @param map map from which to get the object + * @param name name of the element to get from the map, without any subscript + * @return a Map + */ + private static Map getObject(Map map, String name) { + @SuppressWarnings("unchecked") + Map result = (Map) map.compute(name, (key, value) -> { + if (value instanceof Map) { + return value; + } else { + return new LinkedHashMap<>(); + } + }); + + return result; + } + + /** + * Gets an array from a map. If the particular element is not an array, then it is + * replaced with an empty array. + * + * @param map map from which to get the array + * @param name name of the element to get from the map, without any subscript + * @return an array + */ + private static List getArray(Map map, String name) { + @SuppressWarnings("unchecked") + List result = (List) map.compute(name, (key, value) -> { + if (value instanceof List) { + return value; + } else { + return new ArrayList<>(); + } + }); + + return result; + } + + /** + * Compresses lists contained within a generic object, removing all {@code null} + * items. + * + * @param object object to be compressed + * @return the original object, modified in place + */ + public static Object compressLists(Object object) { + if (object instanceof Map) { + @SuppressWarnings("unchecked") + Map asMap = (Map) object; + compressMapValues(asMap); + + } else if (object instanceof List) { + @SuppressWarnings("unchecked") + List asList = (List) object; + compressListItems(asList); + } + + // else: ignore anything else + + return object; + } + + /** + * Walks a hierarchical map and removes {@code null} items found in any Lists. + * + * @param map map whose lists are to be compressed + */ + private static void compressMapValues(Map map) { + for (Object value : map.values()) { + compressLists(value); + } + } + + /** + * Removes {@code null} items from the list. In addition, it walks the items within + * the list, compressing them, as well. + * + * @param list the list to be compressed + */ + private static void compressListItems(List list) { + Iterator iter = list.iterator(); + while (iter.hasNext()) { + Object item = iter.next(); + if (item == null) { + // null item - remove it + iter.remove(); + + } else { + compressLists(item); + } + } + } +} -- cgit 1.2.3-korg