com.thorstenmarx.modules.api.message.Message Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of modules-api Show documentation
Show all versions of modules-api Show documentation
Modules is a tiny module framework for java.
/**
* ModuleManager
* Copyright (C) 2016 ThorstenMarx ([email protected])
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.thorstenmarx.modules.api.message;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* MUTABLE message object that passed from publisher to consumer.
* Be aware of possible message values mutability, do not try to edit this values in consumer.
* Each message may contain key-value(object) map.
*
* Keep in mind that Messageis not serializable because it should be used only in one active instance.
*/
public class Message {
private final String type;
private final Map properties = new HashMap<>(8);
private boolean locked;
/**
* Create message with appropriate type.
*
* @param type Message type, message bus will use type while choosing appropriate consumers.
*/
public Message(String type) {
this.type = Objects.requireNonNull(type);
}
/**
* Create message with provided properties.
*
* @param type Message type, message bus will use type while choosing appropriate consumers.'
* @param properties Message properties
*/
public Message(String type, Map properties) {
this.type = Objects.requireNonNull(type);;
this.properties.putAll(Objects.requireNonNull(properties));
}
/**
* Return message type.
* Message bus will use type while choosing appropriate consumers.
* @return
*/
public String getType() {
return type;
}
protected void lock() {
this.locked = true;
}
private void isLocked() {
if (locked) {
throw new UnsupportedOperationException("You can not call any setters on "
+ getClass().getSimpleName() + " instance after message was published.");
}
}
/**
* Return true if message has any properties (key-value maps is not empty).
* @return
*/
public boolean hasProperties() {
return properties.isEmpty();
}
/**
* Return all keys associated with key-value properties of current message.
* @return
*/
public Collection getKeys() {
return properties.keySet();
}
/**
* Associate property value with message.
*
* @param key Property key
* @param value Property value
* @throws UnsupportedOperationException If this method will be called during message processing
*/
public void set(String key, Object value) throws UnsupportedOperationException {
isLocked();
properties.put(key, value);
}
/**
* Return message property value casted to class if any.
*
* @param key Property key in map
* @param cls Expected value class
* @return Result will be empty if value does not exist or wrong class to cast
*/
public Optional getValue(String key, Class cls) {
return Optional.ofNullable(get(key, cls));
}
/**
* Return message property value casted to class if any.
*
* @param key Property key in map
* @param cls Expected value class
* @return Will return matched value or null (if value does not exist or wrong class)
*/
public T get(String key, Class cls) {
Object val = properties.get(key);
if (val == null) {
return null;
}
if (cls.isInstance(val)) {
return cls.cast(val);
}
return null;
}
/**
* Return message property value casted to class or default value.
*
* @param key Property key in map
* @param cls Expected value class
* @return Will return matched value or null (if value does not exist or wrong class)
*
*/
public T getOr(String key, Class cls, T defaultValue) {
T val = get(key, cls);
return val == null ? defaultValue : val;
}
/**
* Return string representation of value using toSting method.
*
* @param key Property key
* @return String or null if not value
*/
public String getString(String key) {
Object val = properties.get(key);
return val == null ? null : val.toString();
}
/**
* Return string representation of value using toSting method or default String.
*
* @param key Property key
* @param defaultValue
* @return String or default value
*/
public String getStringOr(String key, String defaultValue) {
String str = getString(key);
return str == null ? defaultValue : str;
}
}