Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-infrastructure / src / main / java / org / onap / policy / apex / core / infrastructure / messaging / MessageHolder.java
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 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * The Class MessageHolder holds a set of messages to be sent as a single block of messages in this messaging
32  * implementation.
33  *
34  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
35  * @param <M> the generic type of message being handled by a message holder instance
36  */
37 public class MessageHolder<M> implements Serializable {
38     private static final int HASH_PRIME = 31;
39     private static final int FOUR_BYTES = 32;
40
41     // Serial ID
42     private static final long serialVersionUID = 1235487535388793719L;
43
44     // Get a reference to the logger
45     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageHolder.class);
46
47     // Properties of the message holder
48     private final long creationTime;
49     private final InetAddress senderHostAddress;
50
51     // Sequence of message in the message holder
52     private final List<M> messages;
53
54     /**
55      * Constructor, create the message holder.
56      *
57      * @param senderHostAddress the host address of the sender of the message holder container
58      */
59     public MessageHolder(final InetAddress senderHostAddress) {
60         LOGGER.entry(senderHostAddress);
61         messages = new ArrayList<>();
62         this.senderHostAddress = senderHostAddress;
63         creationTime = System.currentTimeMillis();
64     }
65
66     /**
67      * Return the messages in this message holder.
68      *
69      * @return the messages
70      */
71     public List<M> getMessages() {
72         return messages;
73     }
74
75     /**
76      * Adds a message to this message holder.
77      *
78      * @param message the message to add
79      */
80     public void addMessage(final M message) {
81         if (!messages.contains(message)) {
82             messages.add(message);
83         } else {
84             LOGGER.warn("duplicate message {} added to message holder", message);
85         }
86     }
87
88     /**
89      * Gets the creation time.
90      *
91      * @return the creation time
92      */
93     public long getCreationTime() {
94         return creationTime;
95     }
96
97     /**
98      * Gets the sender host address.
99      *
100      * @return the sender host address
101      */
102     public InetAddress getSenderHostAddress() {
103         return senderHostAddress;
104     }
105
106     /**
107      * {@inheritDoc}.
108      */
109     @Override
110     public String toString() {
111         return "ApexCommandProtocol [creationTime=" + creationTime + ", senderHostAddress=" + senderHostAddress + "]";
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public int hashCode() {
119         final int prime = HASH_PRIME;
120         int result = 1;
121         result = prime * result + ((senderHostAddress == null) ? 0 : senderHostAddress.hashCode());
122         result = prime * result + ((messages == null) ? 0 : messages.hashCode());
123         result = prime * result + (int) (creationTime ^ (creationTime >>> FOUR_BYTES));
124         return result;
125     }
126
127     /**
128      * {@inheritDoc}.
129      */
130     @Override
131     public boolean equals(final Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (obj == null) {
136             return false;
137         }
138         if (getClass() != obj.getClass()) {
139             return false;
140         }
141         final MessageHolder<?> other = (MessageHolder<?>) obj;
142         if (senderHostAddress == null) {
143             if (other.senderHostAddress != null) {
144                 return false;
145             }
146         } else if (!senderHostAddress.equals(other.senderHostAddress)) {
147             return false;
148         }
149         if (messages == null) {
150             if (other.messages != null) {
151                 return false;
152             }
153         } else if (!messages.equals(other.messages)) {
154             return false;
155         }
156         return creationTime == other.creationTime;
157     }
158 }