[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-api / src / main / java / org / openecomp / core / validation / types / MessageContainer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.validation.types;
22
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.datatypes.error.ErrorMessage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.function.Predicate;
29
30
31 public class MessageContainer {
32
33   private List<ErrorMessage> errorMessageList = new ArrayList<>();
34
35   public List<ErrorMessage> getErrorMessageList() {
36     return errorMessageList;
37   }
38
39   public MessageBuilder getMessageBuilder() {
40     return new MessageBuilder();
41   }
42
43   /**
44    * Gets error message list by level.
45    *
46    * @param level the level
47    * @return the error message list by level
48    */
49   public List<ErrorMessage> getErrorMessageListByLevel(ErrorLevel level) {
50
51     List<ErrorMessage> errors = new ArrayList<>();
52     errorMessageList.stream().filter(new Predicate<ErrorMessage>() {
53       @Override
54       public boolean test(ErrorMessage errorMessage) {
55         return errorMessage.getLevel().equals(level);
56       }
57     }).forEach(errorMessage -> errors.add(errorMessage));
58     return errors;
59   }
60
61   public class MessageBuilder {
62
63     String message;
64     ErrorLevel level;
65
66     MessageBuilder setMessage(String message) {
67       this.message = message;
68       return this;
69     }
70
71     MessageBuilder setLevel(ErrorLevel level) {
72       this.level = level;
73       return this;
74     }
75
76     void create() {
77       ErrorMessage errorMessage = new ErrorMessage(level, message);
78       if (!errorMessageList.contains(errorMessage)) {
79         errorMessageList.add(errorMessage);
80       }
81     }
82   }
83 }