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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.messaging;
23 import java.io.Serializable;
24 import java.net.InetAddress;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * The Class MessageHolder holds a set of messages to be sent as a single block of messages in this messaging
35 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
36 * @param <M> the generic type of message being handled by a message holder instance
38 public class MessageHolder<M> implements Serializable {
39 private static final int HASH_PRIME = 31;
40 private static final int FOUR_BYTES = 32;
43 private static final long serialVersionUID = 1235487535388793719L;
45 // Get a reference to the logger
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageHolder.class);
48 // Properties of the message holder
49 private final long creationTime;
50 private final InetAddress senderHostAddress;
52 // Sequence of message in the message holder
53 private final List<M> messages;
56 * Constructor, create the message holder.
58 * @param senderHostAddress the host address of the sender of the message holder container
60 public MessageHolder(final InetAddress senderHostAddress) {
61 LOGGER.entry(senderHostAddress);
62 messages = new ArrayList<>();
63 this.senderHostAddress = senderHostAddress;
64 creationTime = System.currentTimeMillis();
68 * Return the messages in this message holder.
70 * @return the messages
72 public List<M> getMessages() {
77 * Adds a message to this message holder.
79 * @param message the message to add
81 public void addMessage(final M message) {
82 if (!messages.contains(message)) {
83 messages.add(message);
85 LOGGER.warn("duplicate message {} added to message holder", message);
90 * Gets the creation time.
92 * @return the creation time
94 public long getCreationTime() {
99 * Gets the sender host address.
101 * @return the sender host address
103 public InetAddress getSenderHostAddress() {
104 return senderHostAddress;
110 * @see java.lang.Object#toString()
113 public String toString() {
114 return "ApexCommandProtocol [creationTime=" + creationTime + ", senderHostAddress=" + senderHostAddress + "]";
120 * @see java.lang.Object#hashCode()
123 public int hashCode() {
124 final int prime = HASH_PRIME;
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));
135 * @see java.lang.Object#equals(java.lang.Object)
138 public boolean equals(final Object obj) {
145 if (getClass() != obj.getClass()) {
148 final MessageHolder<?> other = (MessageHolder<?>) obj;
149 if (senderHostAddress == null) {
150 if (other.senderHostAddress != null) {
153 } else if (!senderHostAddress.equals(other.senderHostAddress)) {
156 if (messages == null) {
157 if (other.messages != null) {
160 } else if (!messages.equals(other.messages)) {
163 return creationTime == other.creationTime;