aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src/main/java/org/onap
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-08-26 12:20:27 -0400
committerJim Hahn <jrh3@att.com>2019-08-27 11:49:30 -0400
commitfd809717ca774dfabeddd3984fbbbbdfd029601e (patch)
treea4070c2529b0be1899ece44e3687b72ed32505a4 /models-base/src/main/java/org/onap
parenta930b0105c2e45a657427cfcb41fc0330d0c2e99 (diff)
Replace copyTo methods with copy constructors
Deleted the copyTo() method from PfConcepts and replaced uses with deep-copy constructors. Also added mapMap() and makeCopy() methods to PfUtils to facilitate. Change-Id: Id6391bb806ef0dfab6c1089278bf2b514c7e303e Issue-ID: POLICY-1600 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-base/src/main/java/org/onap')
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConcept.java12
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java31
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java18
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java23
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfModel.java12
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java17
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfUtils.java85
7 files changed, 101 insertions, 97 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
index 9a376feff..a1cfac4ae 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 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.
@@ -44,7 +45,7 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> {
* @param copyConcept the concept to copy from
*/
public PfConcept(@NonNull final PfConcept copyConcept) {
- copyConcept.copyTo(this);
+ // nothing else to do (other than @NonNull check)
}
/**
@@ -87,15 +88,6 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> {
public abstract int hashCode();
/**
- * Copy this concept to another object. The target object must have the same class as the source
- * object.
- *
- * @param target the target object to which this object is copied
- * @return the copied object
- */
- public abstract PfConcept copyTo(@NonNull PfConcept target);
-
- /**
* Gets the ID string of this concept.
*
* @return the ID string of this concept
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
index 63ab14ee5..1b60932e9 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 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.
@@ -29,7 +30,6 @@ import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;
-
import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@@ -37,12 +37,9 @@ import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.ws.rs.core.Response;
-
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
-
-import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
// @formatter:off
@@ -111,6 +108,14 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
*/
public PfConceptContainer(@NonNull final PfConceptContainer<C, A> copyConcept) {
super(copyConcept);
+ this.key = new PfConceptKey(copyConcept.key);
+
+ this.conceptMap = new TreeMap<>();
+ for (final Entry<PfConceptKey, C> conceptMapEntry : copyConcept.conceptMap.entrySet()) {
+ PfConceptKey newK = new PfConceptKey(conceptMapEntry.getKey());
+ C newC = PfUtils.makeCopy(conceptMapEntry.getValue());
+ this.conceptMap.put(newK, newC);
+ }
}
@Override
@@ -279,24 +284,6 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
}
@Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- Assertions.instanceOf(target, PfConceptContainer.class);
-
- @SuppressWarnings("unchecked")
- final PfConceptContainer<C, A> copy = (PfConceptContainer<C, A>) target;
- copy.setKey(new PfConceptKey(key));
- final Map<PfConceptKey, C> newConceptMap = new TreeMap<>();
- for (final Entry<PfConceptKey, C> conceptMapEntry : conceptMap.entrySet()) {
- C newC = getConceptNewInstance();
- conceptMapEntry.getValue().copyTo(newC);
- newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), newC);
- }
- copy.setConceptMap(newConceptMap);
-
- return copy;
- }
-
- @Override
public C get(final PfConceptKey conceptKey) {
return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java
index 17fda7cfe..f5baae7fe 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java
@@ -1,7 +1,7 @@
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
- * ModificationsCopyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 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.
@@ -73,6 +73,8 @@ public class PfConceptKey extends PfKey {
*/
public PfConceptKey(@NonNull final PfConceptKey copyConcept) {
super(copyConcept);
+ this.name = copyConcept.name;
+ this.version = copyConcept.version;
}
/**
@@ -289,20 +291,6 @@ public class PfConceptKey extends PfKey {
}
@Override
- public PfConcept copyTo(final PfConcept target) {
- Assertions.argumentNotNull(target, "target may not be null");
-
- final PfConcept copyObject = target;
- Assertions.instanceOf(copyObject, PfConceptKey.class);
-
- final PfConceptKey copy = ((PfConceptKey) copyObject);
- copy.setName(name);
- copy.setVersion(version);
-
- return copyObject;
- }
-
- @Override
public int compareTo(@NonNull final PfConcept otherObj) {
Assertions.argumentNotNull(otherObj, "comparison object may not be null");
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java b/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java
index d56b0dbab..03873236c 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 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.
@@ -21,13 +22,9 @@
package org.onap.policy.models.base;
import java.util.List;
-
-import javax.ws.rs.core.Response;
-
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;
-
import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
@@ -69,6 +66,7 @@ public class PfKeyUse extends PfKey {
*/
public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
super(copyConcept);
+ this.usedKey = PfUtils.makeCopy(copyConcept.usedKey);
}
@Override
@@ -159,21 +157,4 @@ public class PfKeyUse extends PfKey {
return usedKey.compareTo(other.usedKey);
}
-
- @Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfKeyUse.class);
-
- final PfKeyUse copy = ((PfKeyUse) copyObject);
- try {
- copy.usedKey = usedKey.getClass().newInstance();
- } catch (final Exception e) {
- throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
- "error copying concept key: " + e.getMessage(), e);
- }
- usedKey.copyTo(copy.usedKey);
-
- return copy;
- }
}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java
index 07ec3af04..83ae71cca 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 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.
@@ -91,6 +92,7 @@ public abstract class PfModel extends PfConcept {
*/
public PfModel(@NonNull final PfModel copyConcept) {
super(copyConcept);
+ this.key = new PfConceptKey(copyConcept.key);
}
/**
@@ -281,14 +283,4 @@ public abstract class PfModel extends PfConcept {
return key.compareTo(other.key);
}
-
- @Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- Assertions.instanceOf(target, PfModel.class);
-
- final PfModel copy = ((PfModel) target);
- copy.setKey(new PfConceptKey(key));
-
- return copy;
- }
}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java
index bdf16dcdf..a5ae6c51e 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 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.
@@ -382,22 +383,6 @@ public class PfReferenceKey extends PfKey {
}
@Override
- public PfConcept copyTo(final PfConcept target) {
- Assertions.argumentNotNull(target, "target may not be null");
-
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfReferenceKey.class);
-
- final PfReferenceKey copy = ((PfReferenceKey) copyObject);
- copy.setParentKeyName(parentKeyName);
- copy.setParentKeyVersion(parentKeyVersion);
- copy.setLocalName(localName);
- copy.setParentLocalName(parentLocalName);
-
- return copy;
- }
-
- @Override
public int compareTo(final PfConcept otherObj) {
Assertions.argumentNotNull(otherObj, "comparison object may not be null");
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
index c33271d78..8f1040bfe 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
@@ -21,10 +21,14 @@
package org.onap.policy.models.base;
-import java.util.ArrayList;
+import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;
+import javax.ws.rs.core.Response;
/**
* Utility class for Policy Framework concept utilities.
@@ -69,13 +73,88 @@ public final class PfUtils {
*
* @param source list whose elements are to be mapped, or {@code null}
* @param mapFunc mapping function
+ * @param defaultValue value to be returned if source is {@code null}
* @return a new list, containing mappings of all of the items in the original list
*/
- public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
+ public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc, List<T> defaultValue) {
if (source == null) {
- return new ArrayList<>(0);
+ return defaultValue;
}
return source.stream().map(mapFunc).collect(Collectors.toList());
}
+
+ /**
+ * Convenience method to apply a mapping function to all of the elements of a list,
+ * generating a new list.
+ *
+ * @param source list whose elements are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @return a new list, containing mappings of all of the items in the original list,
+ * or {@code null} if the source is {@code null}
+ */
+ public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
+ return mapList(source, mapFunc, null);
+ }
+
+ /**
+ * Convenience method to apply a mapping function to all of the values of a map,
+ * generating a new map.
+ *
+ * @param source map whose values are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @param defaultValue value to be returned if source is {@code null}
+ * @return a new map, containing mappings of all of the items in the original map
+ */
+ public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc,
+ Map<String, T> defaultValue) {
+ if (source == null) {
+ return defaultValue;
+ }
+
+ Map<String, T> map = new LinkedHashMap<>();
+ for (Entry<String, T> ent : source.entrySet()) {
+ map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
+ }
+
+ return map;
+ }
+
+ /**
+ * Convenience method to apply a mapping function to all of the values of a map,
+ * generating a new map.
+ *
+ * @param source map whose values are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @return a new map, containing mappings of all of the items in the original map,
+ * or {@code null} if the source is {@code null}
+ */
+ public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc) {
+ return mapMap(source, mapFunc, null);
+ }
+
+ /**
+ * Makes a copy of an object using the copy constructor from the object's class.
+ *
+ * @param source object to be copied
+ * @return a copy of the source, or {@code null} if the source is {@code null}
+ * @throws PfModelRuntimeException if the object cannot be copied
+ */
+ public static <T> T makeCopy(T source) {
+ if (source == null) {
+ return null;
+ }
+
+ try {
+ @SuppressWarnings("unchecked")
+ Class<? extends T> clazz = (Class<? extends T>) source.getClass();
+
+ return clazz.getConstructor(clazz).newInstance(source);
+
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
+ | RuntimeException e) {
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+ "error copying concept key class: " + source.getClass().getName(), e);
+ }
+ }
}