cd353b95ded576e6d56977281e6f64037c186d67
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.messaging;
22
23 import java.io.Serializable;
24 import java.net.InetAddress;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * The Class MessageHolder holds a set of messages to be sent as a single block of messages in this messaging
33  * implementation.
34  *
35  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
36  * @param <M> the generic type of message being handled by a message holder instance
37  */
38 public class MessageHolder<M> implements Serializable {
39     private static final int HASH_PRIME = 31;
40     private static final int FOUR_BYTES = 32;
41
42     // Serial ID
43     private static final long serialVersionUID = 1235487535388793719L;
44
45     // Get a reference to the logger
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageHolder.class);
47
48     // Properties of the message holder
49     private final long creationTime;
50     private final InetAddress senderHostAddress;
51
52     // Sequence of message in the message holder
53     private final List<M> messages;
54
55     /**
56      * Constructor, create the message holder.
57      *
58      * @param senderHostAddress the host address of the sender of the message holder container
59      */
60     public MessageHolder(final InetAddress senderHostAddress) {
61         LOGGER.entry(senderHostAddress);
62         messages = new ArrayList<>();
63         this.senderHostAddress = senderHostAddress;
64         creationTime = System.currentTimeMillis();
65     }
66
67     /**
68      * Return the messages in this message holder.
69      *
70      * @return the messages
71      */
72     public List<M> getMessages() {
73         return messages;
74     }
75
76     /**
77      * Adds a message to this message holder.
78      *
79      * @param message the message to add
80      */
81     public void addMessage(final M message) {
82         if (!messages.contains(message)) {
83             messages.add(message);
84         } else {
85             LOGGER.warn("duplicate message {} added to message holder", message);
86         }
87     }
88
89     /**
90      * Gets the creation time.
91      *
92      * @return the creation time
93      */
94     public long getCreationTime() {
95         return creationTime;
96     }
97
98     /**
99      * Gets the sender host address.
100      *
101      * @return the sender host address
102      */
103     public InetAddress getSenderHostAddress() {
104         return senderHostAddress;
105     }
106
107     /**
108      * {@inheritDoc}.
109      */
110     @Override
111     public String toString() {
112         return "ApexCommandProtocol [creationTime=" + creationTime + ", senderHostAddress=" + senderHostAddress + "]";
113     }
114
115     /**
116      * {@inheritDoc}.
117      */
118     @Override
119     public int hashCode() {
120         final int prime = HASH_PRIME;
121         int result = 1;
122         result = prime * result + ((senderHostAddress == null) ? 0 : senderHostAddress.hashCode());
123         result = prime * result + ((messages == null) ? 0 : messages.hashCode());
124         result = prime * result + (int) (creationTime ^ (creationTime >>> FOUR_BYTES));
125         return result;
126     }
127
128     /**
129      * {@inheritDoc}.
130      */
131     @Override
132     public boolean equals(final Object obj) {
133         if (this == obj) {
134             return true;
135         }
136         if (obj == null) {
137             return false;
138         }
139         if (getClass() != obj.getClass()) {
140             return false;
141         }
142         final MessageHolder<?> other = (MessageHolder<?>) obj;
143         if (senderHostAddress == null) {
144             if (other.senderHostAddress != null) {
145                 return false;
146             }
147         } else if (!senderHostAddress.equals(other.senderHostAddress)) {
148             return false;
149         }
150         if (messages == null) {
151             if (other.messages != null) {
152                 return false;
153             }
154         } else if (!messages.equals(other.messages)) {
155             return false;
156         }
157         return creationTime == other.creationTime;
158     }
159 }