/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.fabric;

/**
 * This implementation of the <code>MessageSubscription</code> works on literal matches for the symbol and does
 * not support wildcard or other patterns. This is the best choice for single-symbol subscriptions.
 * <p>
 * MTS: Safe
 *
 * @param <E>
 * @author Alex Leigh
 */
public class LiteralMessageSubscription<E> implements MessageSubscription<E> {
    private final MessageReceiver<E> subscriber;
    private final String selector;

    public LiteralMessageSubscription(final MessageReceiver<E> subscriber, final String selector) {
        this.subscriber = subscriber;
        this.selector = selector;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LiteralMessageSubscription that = (LiteralMessageSubscription) o;

        if (selector != null ? !selector.equals(that.selector) : that.selector != null) return false;
        return subscriber != null ? subscriber.equals(that.subscriber) : that.subscriber == null;

    }

    @Override
    public int hashCode() {
        int result = subscriber != null ? subscriber.hashCode() : 0;
        result = 31 * result + (selector != null ? selector.hashCode() : 0);
        return result;
    }

    @Override
    public boolean matches(final String symbol) {
        return selector.equals(symbol);
    }

    @Override
    public MessageReceiver<E> getSubscriber() {
        return subscriber;
    }

    @Override
    public String toString() {
        return "LiteralMessageSubscription{" +
                "selector='" + selector + '\'' +
                '}';
    }

}
