diff options
author | Ittay Stern <ittay.stern@att.com> | 2019-01-10 12:42:59 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-01-10 12:42:59 +0000 |
commit | 659874df80409170e57b6dd6197eb3a81eb3468e (patch) | |
tree | 08e79fd57aa0ed299586fd88f69f87a5a2f5ded1 /vid-app-common/src/main/java/org/onap/vid/utils | |
parent | 143784a849ece4fc35cc290e33f829b72bf1fc79 (diff) | |
parent | 6ad41e3ccd398a2721f41ad61c80b7bb03f7d127 (diff) |
Merge "Merge from ECOMP's repository"
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/utils')
7 files changed, 165 insertions, 2 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt b/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt new file mode 100644 index 00000000..7ccdd1bc --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/KotlinUtils.kt @@ -0,0 +1,10 @@ +package org.onap.vid.utils + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper + +inline fun <reified E: Enum<E>> getEnumFromMapOfStrings(map: Map<String, Any>, key:String, defaultValue:E): E { + return java.lang.Enum.valueOf(E::class.java, (map.getOrDefault(key, defaultValue.name) as String)) +} + +val JACKSON_OBJECT_MAPPER: ObjectMapper = jacksonObjectMapper()
\ No newline at end of file diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java index 3ac90588..f02497b0 100644 --- a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java @@ -28,12 +28,14 @@ import static org.onap.vid.utils.Streams.not; public class Logging { - Logging() { + private Logging() { } public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing."; public static final String REQUEST_ID_HEADER_KEY = SystemProperties.ECOMP_REQUEST_ID; + public static final String ONAP_REQUEST_ID_HEADER_KEY = "X-ONAP-RequestID"; + private static ObjectMapper objectMapper = new ObjectMapper(); diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Multival.java b/vid-app-common/src/main/java/org/onap/vid/utils/Multival.java new file mode 100644 index 00000000..3a9b518d --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Multival.java @@ -0,0 +1,54 @@ +package org.onap.vid.utils; + +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import java.util.Collection; +import java.util.function.Function; + +import static java.util.stream.Collectors.toSet; + +@JsonPropertyOrder({"keyType", "valuesType"}) +public class Multival<K, V> { + private final String keyType; + private final String valuesType; + private final K key; + private final Collection<V> values; + + private Multival(String keyType, K key, String valuesType, Collection<V> values) { + this.keyType = keyType; + this.key = key; + this.valuesType = valuesType; + this.values = values; + } + + public static <K, V> Multival<K, V> of(String keyType, K key, String valuesType, Collection<V> values) { + return new Multival<>(keyType, key, valuesType, values); + } + + public String getKeyType() { + return keyType; + } + + public String getValuesType() { + return valuesType; + } + + public K getKey() { + return key; + } + + public Collection<V> getValues() { + return values; + } + + public <W> Multival<K, W> mapEachVal(Function<V, W> mapper) { + return Multival.of( + this.getKeyType(), + this.getKey(), + this.getValuesType(), + this.getValues().stream() + .map(mapper) + .collect(toSet()) + ); + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Streams.java b/vid-app-common/src/main/java/org/onap/vid/utils/Streams.java index 7f81b225..9c836912 100644 --- a/vid-app-common/src/main/java/org/onap/vid/utils/Streams.java +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Streams.java @@ -9,13 +9,22 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; public class Streams { + + private Streams() { + // hide the implicit public constructor + } + public static <R> Predicate<R> not(Predicate<R> predicate) { return predicate.negate(); } public static <T> Stream<T> fromIterator(final Iterator<T> iterator) { Iterable<T> iterable = () -> iterator; - return StreamSupport.<T>stream(iterable.spliterator(), false); + return StreamSupport.stream(iterable.spliterator(), false); + } + + public static <T> Stream<T> fromIterable(final Iterable<T> iterable) { + return StreamSupport.stream(iterable.spliterator(), false); } diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/TimeUtils.java b/vid-app-common/src/main/java/org/onap/vid/utils/TimeUtils.java new file mode 100644 index 00000000..7d281454 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/TimeUtils.java @@ -0,0 +1,21 @@ +package org.onap.vid.utils; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +public class TimeUtils { + private static DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; + + private TimeUtils() { + // explicit private constructor, to hide the implicit public constructor + } + + public static ZonedDateTime parseZonedDateTime(String time) { + + return ZonedDateTime.from(formatter.parse(time)); + } + + public static String zonedDateTimeToString(ZonedDateTime time) { + return formatter.format(time); + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt b/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt new file mode 100644 index 00000000..28f989bb --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Tree.kt @@ -0,0 +1,44 @@ +package org.onap.vid.utils + +data class Node<T>(val value:T, val children:MutableMap<T, Node<T>> = hashMapOf()) + +data class Tree<T>(private val root:Node<T>) { + + constructor(value: T) : this(Node(value)) + + fun getRootValue():T { + return root.value; + } + + fun addPath(vararg path: T) { + addPath(path.asList()) + } + + fun addPath(path:Collection<T>) { + var currentNode = root + path.forEach { + currentNode = currentNode.children.getOrPut(it) {Node(it)} + } + } + + fun getSubTree(vararg path: T): Tree<T>? { + return getSubTree(path.asList()) + } + + fun getSubTree(path:Collection<T>): Tree<T>? { + var currentNode:Node<T> = root + path.forEach { + currentNode = currentNode.children[it] ?: return null + } + return Tree(currentNode) + } + + fun isPathExist(vararg path: T): Boolean { + return isPathExist(path.asList()) + } + + fun isPathExist(path:Collection<T>): Boolean { + return getSubTree(path)!=null + } +} + diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java b/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java new file mode 100644 index 00000000..25bf0efd --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Unchecked.java @@ -0,0 +1,23 @@ +package org.onap.vid.utils; + +import org.onap.vid.exceptions.GenericUncheckedException; + +import java.net.URI; +import java.net.URISyntaxException; + +public class Unchecked { + private Unchecked() { + // explicit private constructor, to hide the implicit public constructor + } + + public static URI toURI(String uri) { + try { + // Indulge spaces in the URI by the replcement + return new URI(uri.replace(" ", "%20")); + } catch (URISyntaxException e) { + throw new GenericUncheckedException(e); + } + } + + +} |