push addional code
[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 public class MessageContainer {
31
32   private List<ErrorMessage> errorMessageList = new ArrayList<>();
33
34   public List<ErrorMessage> getErrorMessageList() {
35     return errorMessageList;
36   }
37
38   public MessageBuilder getMessageBuilder() {
39     return new MessageBuilder();
40   }
41
42   /**
43    * Gets error message list by level.
44    *
45    * @param level the level
46    * @return the error message list by level
47    */
48   public List<ErrorMessage> getErrorMessageListByLevel(ErrorLevel level) {
49
50     List<ErrorMessage> errors = new ArrayList<>();
51     errorMessageList.stream().filter(new Predicate<ErrorMessage>() {
52       @Override
53       public boolean test(ErrorMessage errorMessage) {
54         return errorMessage.getLevel().equals(level);
55       }
56     }).forEach(errorMessage -> errors.add(errorMessage));
57     return errors;
58   }
59
60   public class MessageBuilder {
61
62     String message;
63     ErrorLevel level;
64
65     MessageBuilder setMessage(String message) {
66       this.message = message;
67       return this;
68     }
69
70     MessageBuilder setLevel(ErrorLevel level) {
71       this.level = level;
72       return this;
73     }
74
75     void create() {
76       ErrorMessage errorMessage = new ErrorMessage(level, message);
77       if (!errorMessageList.contains(errorMessage)) {
78         errorMessageList.add(errorMessage);
79       }
80     }
81   }
82 }