Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-protocols / src / main / java / org / onap / policy / apex / core / protocols / Message.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.protocols;
22
23 import java.io.Serializable;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
25
26 /**
27  * The Class Message is used to pass protocol messages between Apex components.
28  *
29  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
30  */
31 public abstract class Message implements Serializable {
32     private static final int HASH_PRIME = 31;
33
34     // Serialization ID
35     private static final long serialVersionUID = 2271443377544488309L;
36
37     // Default timeout on server side should be used
38     private static final int DEFAULT_REPLY_TIMEOUT = -1;
39
40     // The Action or message type of the message
41     private Action action = null;
42
43     // The artifact key of the artifact to which this message is related
44     private AxArtifactKey targetKey = null;
45
46     // The data of the message
47     private String messageData = null;
48
49     // The timeout time for replies in milliseconds
50     private int replyTimeout = DEFAULT_REPLY_TIMEOUT;
51
52     /**
53      * Instantiates a new message.
54      *
55      * @param action the action or message type of the message
56      * @param targetKey the artifact key of the artifact to which this message relates
57      */
58     public Message(final Action action, final AxArtifactKey targetKey) {
59         this(action, targetKey, null);
60     }
61
62     /**
63      * Instantiates a new message.
64      *
65      * @param action the action or message type of the message
66      * @param targetKey the artifact key of the artifact to which this message relates
67      * @param messageData the message data to deliver
68      */
69     public Message(final Action action, final AxArtifactKey targetKey, final String messageData) {
70         this.action = action;
71         this.targetKey = targetKey;
72         this.messageData = messageData;
73     }
74
75     /**
76      * Set the message timeout.
77      *
78      * @param replyTimeout the timeout on reply messages in milliseconds
79      */
80     public void setReplyTimeout(final int replyTimeout) {
81         this.replyTimeout = replyTimeout;
82     }
83
84     /**
85      * Sets the message data.
86      *
87      * @param messageData the new message data
88      */
89     public void setMessageData(final String messageData) {
90         this.messageData = messageData;
91     }
92
93     /**
94      * Append to the message data.
95      *
96      * @param newMessageData the message data
97      */
98     public void appendMessageData(final String newMessageData) {
99         if (this.messageData == null) {
100             this.messageData = newMessageData;
101         } else {
102             this.messageData += newMessageData;
103         }
104     }
105
106     /**
107      * Gets the artifact key of the target of the message.
108      *
109      * @return the target
110      */
111     public final AxArtifactKey getTarget() {
112         return targetKey;
113     }
114
115     /**
116      * Gets the artifact key name of the target of the message.
117      *
118      * @return the target name
119      */
120     public final String getTargetName() {
121         return targetKey.getName();
122     }
123
124     /**
125      * Gets the action or message type of this message.
126      *
127      * @return the action
128      */
129     public final Action getAction() {
130         return action;
131     }
132
133     /**
134      * Gets the message data.
135      *
136      * @return the message data
137      */
138     public final String getMessageData() {
139         return messageData;
140     }
141
142     /**
143      * {@inheritDoc}.
144      */
145     @Override
146     public boolean equals(final Object object) {
147         if (this == object) {
148             return true;
149         }
150         if (object == null || getClass() != object.getClass()) {
151             return false;
152         }
153
154         final Message message = (Message) object;
155
156         if (action != null ? !action.equals(message.action) : message.action != null) {
157             return false;
158         }
159         if (targetKey != null ? !targetKey.equals(message.targetKey) : message.targetKey != null) {
160             return false;
161         }
162         return !(messageData != null ? !messageData.equals(message.messageData) : message.messageData != null);
163
164     }
165
166     /**
167      * {@inheritDoc}.
168      */
169     @Override
170     public int hashCode() {
171         int result = action != null ? action.hashCode() : 0;
172         result = HASH_PRIME * result + (targetKey != null ? targetKey.hashCode() : 0);
173         result = HASH_PRIME * result + (messageData != null ? messageData.hashCode() : 0);
174         return result;
175     }
176
177     /**
178      * {@inheritDoc}.
179      */
180     @Override
181     public String toString() {
182         return "Message [action=" + action + ", targetKey=" + targetKey + ", data=" + messageData + "]";
183     }
184
185     /**
186      * Get the timeout to wait for a reply.
187      *
188      * @return the timeout in milliseconds
189      */
190     public int getReplyTimeout() {
191         return replyTimeout;
192     }
193 }