243e057be16b563864e14d7461504e8f564d7c7d
[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      * (non-Javadoc)
109      *
110      * @see java.lang.Object#toString()
111      */
112     @Override
113     public String toString() {
114         return "ApexCommandProtocol [creationTime=" + creationTime + ", senderHostAddress=" + senderHostAddress + "]";
115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see java.lang.Object#hashCode()
121      */
122     @Override
123     public int hashCode() {
124         final int prime = HASH_PRIME;
125         int result = 1;
126         result = prime * result + ((senderHostAddress == null) ? 0 : senderHostAddress.hashCode());
127         result = prime * result + ((messages == null) ? 0 : messages.hashCode());
128         result = prime * result + (int) (creationTime ^ (creationTime >>> FOUR_BYTES));
129         return result;
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see java.lang.Object#equals(java.lang.Object)
136      */
137     @Override
138     public boolean equals(final Object obj) {
139         if (this == obj) {
140             return true;
141         }
142         if (obj == null) {
143             return false;
144         }
145         if (getClass() != obj.getClass()) {
146             return false;
147         }
148         final MessageHolder<?> other = (MessageHolder<?>) obj;
149         if (senderHostAddress == null) {
150             if (other.senderHostAddress != null) {
151                 return false;
152             }
153         } else if (!senderHostAddress.equals(other.senderHostAddress)) {
154             return false;
155         }
156         if (messages == null) {
157             if (other.messages != null) {
158                 return false;
159             }
160         } else if (!messages.equals(other.messages)) {
161             return false;
162         }
163         return creationTime == other.creationTime;
164     }
165 }