abc5c90de5ce982c1144f0b532da59c3ecc0defc
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.messaging;
23
24 import java.io.Serializable;
25 import java.net.InetAddress;
26 import java.util.ArrayList;
27 import java.util.List;
28 import lombok.EqualsAndHashCode;
29 import lombok.Getter;
30 import lombok.ToString;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * The Class MessageHolder holds a set of messages to be sent as a single block of messages in this messaging
36  * implementation.
37  *
38  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
39  * @param <M> the generic type of message being handled by a message holder instance
40  */
41 @Getter
42 @ToString
43 @EqualsAndHashCode
44 public class MessageHolder<M> implements Serializable {
45
46     // Serial ID
47     private static final long serialVersionUID = 1235487535388793719L;
48
49     // Get a reference to the logger
50     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageHolder.class);
51
52     // Properties of the message holder
53     private final long creationTime;
54     private final InetAddress senderHostAddress;
55
56     // Sequence of message in the message holder
57     @ToString.Exclude
58     private final List<M> messages;
59
60     /**
61      * Constructor, create the message holder.
62      *
63      * @param senderHostAddress the host address of the sender of the message holder container
64      */
65     public MessageHolder(final InetAddress senderHostAddress) {
66         LOGGER.entry(senderHostAddress);
67         messages = new ArrayList<>();
68         this.senderHostAddress = senderHostAddress;
69         creationTime = System.currentTimeMillis();
70     }
71
72     /**
73      * Adds a message to this message holder.
74      *
75      * @param message the message to add
76      */
77     public void addMessage(final M message) {
78         if (!messages.contains(message)) {
79             messages.add(message);
80         } else {
81             LOGGER.warn("duplicate message {} added to message holder", message);
82         }
83     }
84 }