diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2020-07-07 07:29:30 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-07-07 07:29:30 +0000 |
commit | a31e3acd014ae0f2cb7304bf25c9a7d50ae92eca (patch) | |
tree | e1b94a45dd0bf189200acc90f4d2a46b89e96158 /csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java | |
parent | aecdd3fcf1f53ab957482f41d8bc64eda44ccb4f (diff) | |
parent | 3d6d5ed1c133e19c4091bd9ef98b5cc8e83eed51 (diff) |
Merge "Add wrappers for yaml generic objects"
Diffstat (limited to 'csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java')
-rw-r--r-- | csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java new file mode 100644 index 0000000..b85ec7c --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java @@ -0,0 +1,61 @@ +/* + * Copyright 2020 Nokia + * + * 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. + * + */ + +package org.onap.validation.yaml.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class YamlParameterListFactory { + + public YamlParametersList createEmptyYamlParameterList() { + return new YamlParametersList(Collections.emptyList()); + } + + public YamlParametersList createYamlParameterList(Object yaml) + throws YamlParameterListParsingException { + try { + return parseYamlToListOfPossibleValues(yaml); + } catch (ClassCastException e) { + throw new YamlParameterListParsingException( + String.format("Fail to parse given objects: %s as list.",yaml), e + ); + } + } + + private YamlParametersList parseYamlToListOfPossibleValues(Object yaml) { + List<String> parametersList = new ArrayList<>(); + if( yaml instanceof List) { + for (Object element : (List) yaml) { + parametersList.add(element.toString()); + } + } else { + parametersList.add(yaml.toString()); + } + return new YamlParametersList(parametersList); + } + + + public static class YamlParameterListParsingException extends Exception { + + YamlParameterListParsingException(String message, Throwable throwable) { + super(message, throwable); + } + } + +} |