com.usefulmilk.pubsub.MilkSubscriber Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-pubsub Show documentation
Show all versions of commons-pubsub Show documentation
A common publisher-subscriber library.
The newest version!
/*
* Copyright 2016 UsefulMilk
*
* Licensed 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.usefulmilk.pubsub;
import com.usefulmilk.support.Validate;
/**
* The {@link MilkSubscriber} have to be create to listen specific extension of {@link MilkMessage}.
* When {@link MilkMessage#publish()} is called, the {@link MilkPublisher} will send the
* {@link MilkMessage} to this {@link MilkSubscriber} through
* {@link MilkSubscriber#handleMilkMessage(MilkMessage)} method.
* Ex:
*
* class MyMilkMessage extends {@link MilkMessage}{
* private String anyMessage;
*
* public MyMilkMessage(MilkPublisher milkPublisher) {
* super(milkPublisher);
* }
*
* public void setAnyMessage(String anyMessage) {
* this.anyMessage = anyMessage;
* }
*
* public String getAnyMessage() {
* return anyMessage;
* }
* }
*
* class MyMilkSubscriber extends {@link MilkSubscriber}<MyMilkMessage>}{
* protected MyMilkSubscriber() {
* super(MyMilkMessage.class);
* }
*
* {@literal}@Override
* public void handleMilkMessage(MyMilkMessage milkMessage) {
* System.out.println(milkMessage.getAnyMessage());
* }
* }
*
*/
public abstract class MilkSubscriber {
private Class milkMessageClass;
protected MilkSubscriber(Class milkMessageClass) {
this.milkMessageClass = Validate.nonNullArgument(milkMessageClass);
}
public abstract void handleMilkMessage(T milkMessage);
Class milkMessageClass() {
return milkMessageClass;
}
}