Last active
August 10, 2017 10:14
-
-
Save dfeist/4bb882c7c6f4561695e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mule.message; | |
import com.eaio.uuid.UUID; | |
import java.io.Serializable; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class GenericMessage<T> implements MuleMessage, Serializable | |
{ | |
private final String id; | |
private final T payload; | |
private final Map<String, Object> properties; | |
private ImmutableMessage(final String id, final T payload, final Map<String, Object> properties) | |
{ | |
this.id = id; | |
this.payload = payload; | |
this.properties = properties; | |
} | |
public String getId() | |
{ | |
return id; | |
} | |
public T getPayload() | |
{ | |
return payload; | |
} | |
public Map<String, Object> getProperties() | |
{ | |
return properties; | |
} | |
public <V> V getProperty(String key) | |
{ | |
return (V) properties.get(key); | |
} | |
public String toString() | |
{ | |
return payload.toString(); | |
} | |
public static Builder create() | |
{ | |
return new Builder(); | |
} | |
public static Builder copy(final Message message) | |
{ | |
return new Builder().setPayload(message.payload).addProperties(message.properties).setId(message.id); | |
} | |
public static class Builder<T> | |
{ | |
private String id; | |
private T payload; | |
private Map<String, Object> properties = new HashMap<>(); | |
Builder() {} | |
public ImmutableMessage build() | |
{ | |
return new ImmutableMessage(id != null ? id : new UUID().toString(), payload, Collections.unmodifiableMap(properties)); | |
} | |
public Builder setId(String id) | |
{ | |
this.id = id; | |
return this; | |
} | |
public Builder setPayload(T payload) | |
{ | |
this.payload = payload; | |
return this; | |
} | |
public Builder addProperties(Map<String, Object> properties) | |
{ | |
this.properties.putAll(properties); | |
return this; | |
} | |
public Builder addProperty(String key, String value) | |
{ | |
this.properties.put(key, value); | |
return this; | |
} | |
public Builder removeProperty(String key) | |
{ | |
this.properties.remove(key); | |
return this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment