diff options
Diffstat (limited to 'catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils')
8 files changed, 247 insertions, 487 deletions
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/CollectionUtils.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/CollectionUtils.java index 7177e4c9fa..7336859fd7 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/CollectionUtils.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/CollectionUtils.java @@ -7,9 +7,9 @@ * 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. @@ -20,106 +20,110 @@ package org.openecomp.sdc.be.dao.utils; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; public final class CollectionUtils { - private CollectionUtils() { - } + private CollectionUtils() { + } - /** - * Add the content of the 'source' Set to the 'target' set and return the - * union set. - * - * If 'source' is null then a new set is created and returned. If 'target' - * is null then no content is added to the 'source' Set or newly created - * set. - * - * @param source - * The Set to merge in the target Set. - * @param target - * The Set in which the source set will be merged (through - * addAll). - * @return The target Set with addition of source Set elements, or a new Set - * (including content of source set) if target was null. - */ - public static <T> Set<T> merge(Set<T> source, Set<T> target) { - Set<T> merged = new HashSet<T>(); - if (target != null) { - merged.addAll(target); - } - if (source != null) { - merged.addAll(source); - } - return merged.isEmpty() ? null : merged; - } + /** + * Add the content of the 'source' Set to the 'target' set and return the + * union set. + * <p> + * If 'source' is null then a new set is created and returned. If 'target' + * is null then no content is added to the 'source' Set or newly created + * set. + * + * @param source The Set to merge in the target Set. + * @param target The Set in which the source set will be merged (through + * addAll). + * @return The target Set with addition of source Set elements, or a new Set + * (including content of source set) if target was null. + */ + public static <T> Set<T> merge(Set<T> source, Set<T> target) { + Set<T> merged = new HashSet<>(); + if (target != null) { + merged.addAll(target); + } + if (source != null) { + merged.addAll(source); + } + return merged.isEmpty() ? null : merged; + } - /** - * <p> - * Add the content of the 'source' Map to the 'target' set and return the - * union Map. - * </p> - * <p> - * If 'source' is null then a new Map is created and returned. If 'target' - * is null then no content is added to the 'source' Map or newly created - * Map. - * </p> - * - * @param source - * The Map to merge in the target Map. - * @param target - * The Map in which the source Map will be merged (through - * addAll). - * @param override - * If an key from the source map already exists in the target - * map, should it override (true) or not (false) the value. - * @return The target Map with addition of source Map elements, or a new Map - * (including content of source set) if target was null. - */ - public static <T, V> Map<T, V> merge(Map<T, ? extends V> source, Map<T, V> target, boolean override) { - if (target == null) { - target = new HashMap(); - } + /** + * <p> + * Add the content of the 'source' Map to the 'target' set and return the + * union Map. + * </p> + * <p> + * If 'source' is null then a new Map is created and returned. If 'target' + * is null then no content is added to the 'source' Map or newly created + * Map. + * </p> + * + * @param source The Map to merge in the target Map. + * @param target The Map in which the source Map will be merged (through + * addAll). + * @param override If an key from the source map already exists in the target + * map, should it override (true) or not (false) the value. + * @return The target Map with addition of source Map elements, or a new Map + * (including content of source set) if target was null. + */ + public static <T, V> Map<T, V> merge(Map<T, ? extends V> source, Map<T, V> target, boolean override) { + if (target == null) { + target = new HashMap(); + } - if (source != null) { - for (Entry<T, ? extends V> entry : source.entrySet()) { - if (override || !target.containsKey(entry.getKey())) { - target.put(entry.getKey(), entry.getValue()); - } - } - } - return target.isEmpty() ? null : target; - } + if (source != null) { + for (Entry<T, ? extends V> entry : source.entrySet()) { + if (override || !target.containsKey(entry.getKey())) { + target.put(entry.getKey(), entry.getValue()); + } + } + } + return target.isEmpty() ? null : target; + } - /** - * Merge two lists, the merge is performed based on the contains method so - * elements presents both in source and target are not added twice to the - * list. - * - * @param source - * The source list. - * @param target - * The target list. - * @return A list that represents the merged collections. - */ - public static <T> List<T> merge(List<T> source, List<T> target) { - List<T> merged = target == null ? new ArrayList<T>() : target; + /** + * Merge two lists, the merge is performed based on the contains method so + * elements presents both in source and target are not added twice to the + * list. + * + * @param source The source list. + * @param target The target list. + * @return A list that represents the merged collections. + */ + public static <T> List<T> merge(List<T> source, List<T> target) { + List<T> merged = target == null ? new ArrayList<>() : target; - if (source == null) { - return merged; - } + if (source == null) { + return merged; + } - for (T t : source) { - if (!merged.contains(t)) { - merged.add(t); - } - } + for (T t : source) { + if (!merged.contains(t)) { + merged.add(t); + } + } - return merged; - } + return merged; + } + + /** + * Returns a new list containing the second list appended to the + * first list. The {@link List#addAll(Collection)} operation is + * used to append the two given lists into a new list. + * + * @param list1 the first list + * @param list2 the second list + * @return a new list containing the union of those lists + * @throws NullPointerException if either list is null + */ + public static <T> List<T> union(List<T> list1, List<T> list2) { + List<T> result = new ArrayList<>(list1); + result.addAll(list2); + return result; + } } diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageQuality.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageQuality.java deleted file mode 100644 index 4eb691a4b7..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageQuality.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 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.openecomp.sdc.be.dao.utils; - -/** - * Available qualities for images in Alien 4 Cloud. - * - * @author luc boutier - */ -public enum ImageQuality { - QUALITY_16(16), QUALITY_32(32), QUALITY_64(64), QUALITY_128(128), QUALITY_512(512), QUALITY_BEST(-1); - - private final int size; - - private ImageQuality(int size) { - this.size = size; - } - - public int getSize() { - return size; - } -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageResizeUtil.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageResizeUtil.java deleted file mode 100644 index 4db8c72e5a..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageResizeUtil.java +++ /dev/null @@ -1,142 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 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.openecomp.sdc.be.dao.utils; - -import java.awt.Graphics2D; -import java.awt.Image; -import java.awt.RenderingHints; -import java.awt.image.BufferedImage; - -/** - * Utility to resize images. - * - * @author luc boutier - */ -public final class ImageResizeUtil { - private ImageResizeUtil() { - } - - /** - * Resize an image with default quality settings. - * - * @param originalImage - * The image to resize. - * @param width - * The target width. - * @param height - * The target height. - * @param preserveDimensions - * Flag to know if we should preserve original image dimensions. - * @return The resized image. - */ - public static BufferedImage resizeImage(final BufferedImage originalImage, final int width, final int height, - final boolean preserveDimensions) { - return resizeImage(originalImage, width, height, preserveDimensions, false); - } - - /** - * <p> - * Resize an image with high quality settings. - * </p> - * <ul> - * <li>g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - * RenderingHints.VALUE_INTERPOLATION_BILINEAR);</li> - * <li>g.setRenderingHint(RenderingHints.KEY_RENDERING, - * RenderingHints.VALUE_RENDER_QUALITY);</li> - * <li>g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - * RenderingHints.VALUE_ANTIALIAS_ON);</li> - * </ul> - * - * @param originalImage - * The image to resize. - * @param width - * The target width. - * @param height - * The target height. - * @param preserveDimensions - * Flag to know if we should preserve original image dimensions. - * @return The resized image. - */ - public static BufferedImage resizeImageWithHint(BufferedImage originalImage, final int width, final int height, - final boolean preserveDimensions) { - return resizeImage(originalImage, width, height, preserveDimensions, true); - } - - private static BufferedImage resizeImage(BufferedImage originalImage, final int width, final int height, - final boolean preserveDimensions, final boolean enableHighQuality) { - int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); - - int targetWidth = width; - int targetHeight = height; - - if (preserveDimensions) { - int[] targetDimentions = computeDimensions(width, height, originalImage.getWidth(), - originalImage.getHeight()); - targetWidth = targetDimentions[0]; - targetHeight = targetDimentions[1]; - } - - BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, type); - - Graphics2D g = resizedImage.createGraphics(); - if (enableHighQuality) { - g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); - g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - } - - g.drawImage(originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0, 0, null); - g.dispose(); - - return resizedImage; - } - - /** - * Compute target width and height based on requested width and height but - * making sure the original dimensions of the image will be preserved. - * - * @param width - * The ideal (and max) target width. - * @param height - * The ideal (and max) target height. - * @param originalWidth - * The original width. - * @param originalHeight - * The original height. - * @return An array of int that contains the ideal width and height to - * preserve dimensions. - */ - public static int[] computeDimensions(final int width, final int height, final int originalWidth, - final int originalHeight) { - int targetWidth = width; - int targetHeight = height; - - float targetDimensions = Float.valueOf(width).floatValue() / Float.valueOf(height).floatValue(); - float sourceDimensions = Float.valueOf(originalWidth).floatValue() / Float.valueOf(originalHeight).floatValue(); - if (targetDimensions > sourceDimensions) { - targetWidth = Float.valueOf(width * sourceDimensions / targetDimensions).intValue(); - } else { - targetHeight = Float.valueOf(height * targetDimensions / sourceDimensions).intValue(); - } - - return new int[] { targetWidth, targetHeight }; - } -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java index 00b6d5e894..92ba61bcc7 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java @@ -20,12 +20,6 @@ package org.openecomp.sdc.be.dao.utils; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; @@ -34,6 +28,12 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * Simple utility for JSon processing. */ @@ -47,33 +47,9 @@ public final class JsonUtil { } private JsonUtil() { + // No instances allowed } - // /** - // * Parse a {@link RestResponse} by using the specified dataType as the - // expected data object's class. - // * - // * @param responseAsString - // * The {@link RestResponse} as a JSon String - // * @param dataType - // * The type of the data object. - // * @return The parsed {@link RestResponse} object matching the given JSon. - // * @throws JsonParseException - // * In case of a JSon parsing issue. - // * @throws JsonMappingException - // * In case of a JSon parsing issue. - // * @throws IOException - // * In case of an IO error. - // */ - // public static <T> RestResponse<T> read(String responseAsString, Class<T> - // dataType) throws IOException { - // ObjectMapper mapper = getOneObjectMapper(); - // JavaType restResponseType = - // mapper.getTypeFactory().constructParametricType(RestResponse.class, - // dataType); - // return mapper.readValue(responseAsString, restResponseType); - // } - /** * Deserialize json text to object * @@ -117,36 +93,6 @@ public final class JsonUtil { return getOneObjectMapper().readValue(objectText, typeRef); } - // /** - // * Parse a {@link RestResponse} without being interested in parameterized - // type - // * - // * @param responseAsString - // * @return - // * @throws JsonParseException - // * @throws JsonMappingException - // * @throws IOException - // */ - // public static RestResponse<?> read(String responseAsString) throws - // IOException { - // return getOneObjectMapper().readValue(responseAsString, - // RestResponse.class); - // } - - // /** - // * Serialize the given object in a JSon String. - // * - // * @param obj - // * The object to serialize. - // * @return The JSon serialization of the given object. - // * @throws JsonProcessingException - // * In case of a failure in serialization. - // */ - // public static String toString(Object obj) throws JsonProcessingException - // { - // return getOneObjectMapper().writeValueAsString(obj); - // } - /** * Deserialize the given json string to a map * diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapEntry.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapEntry.java deleted file mode 100644 index 13561f60a0..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapEntry.java +++ /dev/null @@ -1,55 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 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.openecomp.sdc.be.dao.utils; - -/** - * A map/set entry as a key/value object to be serialized as is. - * - * @author luc boutier - */ -public class MapEntry<T, V> { - private T key; - private V value; - - public MapEntry() { - } - - public MapEntry(T key, V value) { - this.key = key; - this.value = value; - } - - public T getKey() { - return key; - } - - public void setKey(T key) { - this.key = key; - } - - public V getValue() { - return value; - } - - public void setValue(V value) { - this.value = value; - } -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapUtil.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapUtil.java index e8b2e0c9ad..5b0ad9cc24 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapUtil.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/MapUtil.java @@ -7,9 +7,9 @@ * 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. @@ -20,105 +20,153 @@ package org.openecomp.sdc.be.dao.utils; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; +import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; +import static java.util.stream.Collectors.toList; /** * Utility class to ease map manipulation. */ public final class MapUtil { - private MapUtil() { - } + private MapUtil() { + } - /** - * Try to get a value following a path in the map. For example : - * MapUtil.get(map, "a.b.c") correspond to: map.get(a).get(b).get(c) - * - * @param map - * the map to search for path - * @param path - * keys in the map separated by '.' - */ - public static Object get(Map<String, ? extends Object> map, String path) { - String[] tokens = path.split("\\."); - if (tokens.length == 0) { - return null; - } else { - Object value = map; - for (String token : tokens) { - if (!(value instanceof Map)) { - return null; - } else { - @SuppressWarnings("unchecked") - Map<String, Object> nested = (Map<String, Object>) value; - if (nested.containsKey(token)) { - value = nested.get(token); - } else { - return null; - } - } - } - return value; - } - } + /** + * Try to get a value following a path in the map. For example : + * MapUtil.get(map, "a.b.c") correspond to: map.get(a).get(b).get(c) + * + * @param map the map to search for path + * @param path keys in the map separated by '.' + */ + public static Object get(Map<String, ? extends Object> map, String path) { + String[] tokens = path.split("\\."); + if (tokens.length == 0) { + return null; + } else { + Object value = map; + for (String token : tokens) { + if (!(value instanceof Map)) { + return null; + } else { + @SuppressWarnings("unchecked") + Map<String, Object> nested = (Map<String, Object>) value; + if (nested.containsKey(token)) { + value = nested.get(token); + } else { + return null; + } + } + } + return value; + } + } - /** - * - * @param valuesToMap the list of values to group - * @param groupingFunction the function to group the list values by - * @return a map of list of values grouped by a key, as specified in the {@code groupingFunction} - */ - public static <K,V> Map<K,List<V>> groupListBy(Collection<V> valuesToMap, Function<V,K> groupingFunction) { - return valuesToMap.stream().collect(Collectors.groupingBy(groupingFunction)); - } + /** + * @param valuesToMap the list of values to group + * @param groupingFunction the function to group the list values by + * @return a map of list of values grouped by a key, as specified in the {@code groupingFunction} + */ + public static <K, V> Map<K, List<V>> groupListBy(Collection<V> valuesToMap, Function<V, K> groupingFunction) { + return valuesToMap.stream().collect(Collectors.groupingBy(groupingFunction)); + } - /** - * - * @param valuesToMap list of values to map - * @param mappingFunction a function which specifies how to map each element on the list - * @return a map created by mapping each element from the {@code valuesToMap} as specified in the {@code mappingFunction} - */ - public static <K,V> Map<K,V> toMap(Collection<V> valuesToMap, Function<V,K> mappingFunction) { - return Optional.ofNullable(valuesToMap).orElse(new ArrayList<>()).stream().collect(Collectors.toMap(mappingFunction, Function.identity())); - } + /** + * @param valuesToMap list of values to map + * @param mappingFunction a function which specifies how to map each element on the list + * @return a map created by mapping each element from the {@code valuesToMap} as specified in the {@code mappingFunction} + */ + public static <K, V> Map<K, V> toMap(Collection<V> valuesToMap, Function<V, K> mappingFunction) { + return toMap(valuesToMap, mappingFunction, throwingMerger()); + } + + public static <K, V> Map<K, V> toMap(Collection<V> valuesToMap, Function<V, K> mappingFunction, BinaryOperator<V> mergeFunction) { + return streamOfNullable(valuesToMap).collect(Collectors.toMap(mappingFunction, Function.identity(), mergeFunction)); + } - /** - * - * @param map the map of which it keys to convert - * @param keyMappingFunction a function which converts the key object - * @return a map with converted keys. - */ - public static <K,U, V> Map<U, List<V>> convertMapKeys(Map<K, List<V>> map, Function<K,U> keyMappingFunction) { - return map.entrySet().stream() - .collect(Collectors.toMap(entry -> keyMappingFunction.apply(entry.getKey()), - Map.Entry::getValue)); - } - /** - * Create a new hash map and fills it from the given keys and values - * (keys[index] -> values[index]. - * - * @param keys - * The array of keys. - * @param values - * The array of values. - * @return A map that contains for each key element in the keys array a - * value from the values array at the same index. - */ - public static <K, V> Map<K, V> newHashMap(K[] keys, V[] values) { - Map<K, V> map = new HashMap<K, V>(); - if (keys == null || values == null || keys.length != values.length) { - throw new IllegalArgumentException("keys and values must be non-null and have the same size."); - } - for (int i = 0; i < keys.length; i++) { - map.put(keys[i], values[i]); - } - return map; - } + /** + * merge two maps. if a key exists in both maps, takes the value from {@code first} + * + * @param first the first map to merge + * @param second the second map to merge + * @return the merged map + */ + public static <K, V> Map<K, V> mergeMaps(Map<K, V> first, Map<K, V> second) { + if (first == null && second == null) { + return new HashMap<>(); + } + if (first != null && second == null) { + return new HashMap<>(first); + } + if (first == null) { + return new HashMap<>(second); + } + Map<K, V> mergedMap = new HashMap<>(first); + second.forEach(mergedMap::putIfAbsent); + return mergedMap; + } + + public static <K, V> List<V> flattenMapValues(Map<K, List<V>> mapToFlatten) { + if (mapToFlatten == null) { + return new ArrayList<>(); + } + return mapToFlatten.values().stream().flatMap(Collection::stream).collect(toList()); + } + + /** + * @param map the map of which it keys to convert + * @param keyMappingFunction a function which converts the key object + * @return a map with converted keys. + */ + public static <K, U, V> Map<U, List<V>> convertMapKeys(Map<K, List<V>> map, Function<K, U> keyMappingFunction) { + return map.entrySet().stream() + .collect(Collectors.toMap(entry -> keyMappingFunction.apply(entry.getKey()), + Map.Entry::getValue)); + } + + /** + * Create a new hash map and fills it from the given keys and values + * (keys[index] -> values[index]. + * + * @param keys The array of keys. + * @param values The array of values. + * @return A map that contains for each key element in the keys array a + * value from the values array at the same index. + */ + public static <K, V> Map<K, V> newHashMap(K[] keys, V[] values) { + Map<K, V> map = new HashMap<>(); + if (keys == null || values == null || keys.length != values.length) { + throw new IllegalArgumentException("keys and values must be non-null and have the same size."); + } + for (int i = 0; i < keys.length; i++) { + map.put(keys[i], values[i]); + } + return map; + } + + + /** + * Returns a merge function, suitable for use in + * {@link Map#merge(Object, Object, BiFunction) Map.merge()} or + * {@link #toMap(Function, Function, BinaryOperator) toMap()}, which always + * throws {@code IllegalStateException}. This can be used to enforce the + * assumption that the elements being collected are distinct. + * + * @param <T> the type of input arguments to the merge function + * @return a merge function which always throw {@code IllegalStateException} + */ + private static <T> BinaryOperator<T> throwingMerger() { + return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }; + } + + public static <V> Stream<V> streamOfNullable(Collection<V> collection) { + return collection == null? Stream.empty(): collection.stream(); + } + + public static<T> Stream<T> streamOfNullable(T t) { + return t == null ? Stream.empty() : Stream.of(t); + } } diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/TypeMap.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/TypeMap.java index 1f7e692598..6c860dd269 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/TypeMap.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/TypeMap.java @@ -24,12 +24,12 @@ import java.util.HashMap; import java.util.Map; public class TypeMap { - private Map<Class<? extends Object>, Map<String, Object>> cacheMap = new HashMap<Class<? extends Object>, Map<String, Object>>(); + private Map<Class<? extends Object>, Map<String, Object>> cacheMap = new HashMap<>(); private Map<String, Object> getMap(Class<? extends Object> clazz) { Map<String, Object> map = cacheMap.get(clazz); if (map == null) { - cacheMap.put(clazz, new HashMap<String, Object>()); + cacheMap.put(clazz, new HashMap<>()); } return cacheMap.get(clazz); } diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/UserStatusEnum.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/UserStatusEnum.java index dbe9b84a5e..f66914eb9e 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/UserStatusEnum.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/UserStatusEnum.java @@ -20,9 +20,8 @@ package org.openecomp.sdc.be.dao.utils; -import org.openecomp.sdc.common.util.MethodActivationStatusEnum; - import fj.data.Either; +import org.openecomp.sdc.common.util.MethodActivationStatusEnum; public enum UserStatusEnum { ACTIVE, INACTIVE; |