com.sap.it.commons.logging.MessageInstance Maven / Gradle / Ivy
package com.sap.it.commons.logging;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.datatype.XMLGregorianCalendar;
import com.sap.it.commons.lang.Equals;
import com.sap.it.commons.lang.Hash;
import com.sap.it.commons.tools.ArrayTools;
/*
* Immutable class to represent messages populated with actual parameters.
*/
public final class MessageInstance implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6088582063838907778L;
@XmlJavaTypeAdapter(type = MessageByText.class, value = MessageXmlAdapter.class)
private Message message;
private Object[] parameter;
@XmlElement(name = "child")
private List childInstances;
private transient int hash;
// JAXB
@XmlElement(nillable = true, name = "param")
private Object[] getJaxbParameter() {
return parameter; // NOSONAR NOPMD
}
// JAXB
@SuppressWarnings("unused")
private void setJaxbParameter(Object[] data) {
data = ArrayTools.clone(data);
int len = ArrayTools.length(data);
for (int i = 0; i < len; ++i) {
Object obj = data[i];
if (obj instanceof XMLGregorianCalendar) {
data[i] = ((XMLGregorianCalendar) obj).toGregorianCalendar().getTime();
}
}
this.parameter = data;
}
// JAXB
@SuppressWarnings("unused")
private MessageInstance() {
this.message = null;
}
private static List copyAsUnmodifiableList(Collection messages) {
List res = null;
if (messages != null) {
res = Collections.unmodifiableList(new ArrayList(messages));
}
return res;
}
public MessageInstance(Collection childMessages, Message message, Object... parameter) {
this.message = message;
this.parameter = processParamater(parameter);
this.childInstances = copyAsUnmodifiableList(childMessages);
}
public MessageInstance(Message message, Object... parameter) {
this.message = message;
this.parameter = processParamater(parameter);
this.childInstances = null;
}
public MessageInstance(Message message) {
this.message = message;
this.parameter = null;
this.childInstances = null;
}
private static Object[] processParamater(Object[] parameter) {
if (parameter != null && parameter.length > 0) {
parameter = ArrayTools.clone(parameter);
for (int i = 0; i < parameter.length; ++i) {
Object obj = parameter[i];
if (obj == null) { // NOSONAR NOPMD
} else if (obj instanceof Number) { // NOSONAR NOPMD
} else if (obj instanceof Date) { // NOSONAR NOPMD
} else if (obj instanceof String) { // NOSONAR NOPMD
} else {
parameter[i] = obj.toString();
}
}
return parameter; // NOSONAR NOPMD
} else {
return null;
}
}
/**
* @return copy of message parameters or null, if no parameters are used.
*/
public Object[] getParameters() {
return ArrayTools.clone(parameter);
}
/**
* @return message object.
*/
public Message getMessage() {
return message;
}
/**
*
* @return return immutable list of child messages or null if no child
* messages exist.
*/
public List getChildMessageInstances() {
return childInstances;
}
@Override
public String toString() {
return message != null ? MessageTools.assembleMessage(this) : "";
}
@Override
public int hashCode() {
if (hash == 0) {
hash = Hash.hash(MessageTools.hashCode(message), Arrays.hashCode(parameter), childInstances);
}
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof MessageInstance)) {
return false;
}
MessageInstance other = (MessageInstance) obj;
return MessageTools.equals(message, other.message) && //
Arrays.equals(parameter, other.parameter) && //
Equals.equals(childInstances, other.childInstances);
}
private static class SerializationProxy implements Serializable {
private static final long serialVersionUID = -5941809977564107557L;
private final Message message;
private final Object[] parameter;
private final List childInstances;
public SerializationProxy(MessageInstance messageInstance) {
this.message = MessageTools.asMessageByText(messageInstance.message);
this.parameter = messageInstance.parameter;
this.childInstances = messageInstance.childInstances;
}
private Object readResolve() {
return new MessageInstance(childInstances, message, parameter);
}
};
private Object writeReplace() {
return new SerializationProxy(this);
}
// Should be removed after Takt 2013.01
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
s.defaultReadObject();
message = (Message) s.readObject();
parameter = (Object[]) s.readObject();
childInstances = (List) s.readObject();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy