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;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * The Class MessageHolder holds a set of messages to be sent as a single block of messages in this messaging
34 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
35 * @param <M> the generic type of message being handled by a message holder instance
37 public class MessageHolder<M> implements Serializable {
38 private static final int HASH_PRIME = 31;
39 private static final int FOUR_BYTES = 32;
42 private static final long serialVersionUID = 1235487535388793719L;
44 // Get a reference to the logger
45 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MessageHolder.class);
47 // Properties of the message holder
48 private final long creationTime;
49 private final InetAddress senderHostAddress;
51 // Sequence of message in the message holder
52 private final List<M> messages;
55 * Constructor, create the message holder.
57 * @param senderHostAddress the host address of the sender of the message holder container
59 public MessageHolder(final InetAddress senderHostAddress) {
60 LOGGER.entry(senderHostAddress);
61 messages = new ArrayList<>();
62 this.senderHostAddress = senderHostAddress;
63 creationTime = System.currentTimeMillis();
67 * Return the messages in this message holder.
69 * @return the messages
71 public List<M> getMessages() {
76 * Adds a message to this message holder.
78 * @param message the message to add
80 public void addMessage(final M message) {
81 if (!messages.contains(message)) {
82 messages.add(message);
84 LOGGER.warn("duplicate message {} added to message holder", message);
89 * Gets the creation time.
91 * @return the creation time
93 public long getCreationTime() {
98 * Gets the sender host address.
100 * @return the sender host address
102 public InetAddress getSenderHostAddress() {
103 return senderHostAddress;
110 public String toString() {
111 return "ApexCommandProtocol [creationTime=" + creationTime + ", senderHostAddress=" + senderHostAddress + "]";
118 public int hashCode() {
119 final int prime = HASH_PRIME;
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));
131 public boolean equals(final Object obj) {
138 if (getClass() != obj.getClass()) {
141 final MessageHolder<?> other = (MessageHolder<?>) obj;
142 if (senderHostAddress == null) {
143 if (other.senderHostAddress != null) {
146 } else if (!senderHostAddress.equals(other.senderHostAddress)) {
149 if (messages == null) {
150 if (other.messages != null) {
153 } else if (!messages.equals(other.messages)) {
156 return creationTime == other.creationTime;