diff options
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java | 98 |
1 files changed, 66 insertions, 32 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java index 60c5f4ef..55f7f9d7 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 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,18 +21,29 @@ package org.onap.policy.common.utils.coder; import com.google.gson.JsonElement; +import java.io.Serializable; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; /** * Object type used by the {@link StandardCoder}. Different serialization tools have * different "standard objects". For instance, GSON uses {@link JsonElement}. This class * wraps that object so that it can be used without exposing the object, itself. */ -public class StandardCoderObject { +@AllArgsConstructor(access = AccessLevel.PROTECTED) +public class StandardCoderObject implements Serializable { + private static final long serialVersionUID = 1L; /** * Data wrapped by this. */ - private final JsonElement data; + /* + * this should not be transient, but since it isn't serializable, we're stuck with it + * until there's time to address the issue + */ + @Getter(AccessLevel.PROTECTED) + private final transient JsonElement data; /** * Constructs the object. @@ -42,52 +53,75 @@ public class StandardCoderObject { } /** - * Constructs the object. - * - * @param data data wrapped by this object. - */ - protected StandardCoderObject(JsonElement data) { - this.data = data; - } - - /** - * Gets the data wrapped by this. - * - * @return the data wrapped by this - */ - protected JsonElement getData() { - return data; - } - - /** * Gets a field's value from this object, traversing the object hierarchy. * - * @param fields field hierarchy + * @param fields field hierarchy. These may be strings, identifying fields within the + * object, or Integers, identifying an index within an array * @return the field value or {@code null} if the field does not exist or is not a * primitive */ - public String getString(String... fields) { - - /* - * This could be relatively easily modified to allow Integer arguments, as well, - * which would be used to specify indices within an array. - */ + public String getString(Object... fields) { JsonElement jel = data; - for (String field : fields) { + for (Object field : fields) { if (jel == null) { return null; } - if (jel.isJsonObject()) { - jel = jel.getAsJsonObject().get(field); + if (field instanceof String) { + jel = getFieldFromObject(jel, field.toString()); + + } else if (field instanceof Integer) { + jel = getItemFromArray(jel, (int) field); } else { - return null; + throw new IllegalArgumentException("subscript is not a string or integer: " + field); } } return (jel != null && jel.isJsonPrimitive() ? jel.getAsString() : null); } + + /** + * Gets an item from an object. + * + * @param element object from which to extract the item + * @param field name of the field from which to extract the item + * @return the item, or {@code null} if the element is not an object or if the field + * does not exist + */ + protected JsonElement getFieldFromObject(JsonElement element, String field) { + if (!element.isJsonObject()) { + return null; + } + + return element.getAsJsonObject().get(field); + } + + /** + * Gets an item from an array. + * + * @param element array from which to extract the item + * @param index index of the item to extract + * @return the item, or {@code null} if the element is not an array or if the index is + * out of bounds + */ + protected JsonElement getItemFromArray(JsonElement element, int index) { + if (index < 0) { + throw new IllegalArgumentException("subscript is invalid: " + index); + } + + if (!element.isJsonArray()) { + return null; + } + + var array = element.getAsJsonArray(); + + if (index >= array.size()) { + return null; + } + + return array.get(index); + } } |