io.vlingo.xoom.actors.Routee Maven / Gradle / Ivy
// Copyright © 2012-2021 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.xoom.actors;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import io.vlingo.xoom.common.Completes;
import io.vlingo.xoom.common.PentaConsumer;
import io.vlingo.xoom.common.PentaFunction;
import io.vlingo.xoom.common.QuadConsumer;
import io.vlingo.xoom.common.QuadFunction;
import io.vlingo.xoom.common.TriConsumer;
import io.vlingo.xoom.common.TriFunction;
/**
* Routee represents a potential target for for a routed message.
*/
public class Routee {
private final Addressable addressable;
private P delegate;
private long messageCount;
static Routee of(final T actor, final Addressable addressable) {
return new Routee(actor, addressable);
}
static Routee of(final T actor) {
return new Routee(actor, null);
}
Routee(final P actor, final Addressable addressable) {
super();
this.delegate = actor;
this.addressable = addressable;
this.messageCount = 0;
}
public P delegate() {
return delegate;
}
public LifeCycle delegateLifeCycle() {
return addressable.lifeCycle();
}
public Address address() {
return addressable.address();
}
public int pendingMessages() {
return delegateLifeCycle().environment.mailbox.pendingMessages();
}
public long messageCount() {
return messageCount;
}
protected void receiveCommand(final BiConsumer consumer, final T1 routable1) {
messageCount++;
consumer.accept(delegate, routable1);
}
protected void receiveCommand(final TriConsumer consumer, final T1 routable1, final T2 routable2) {
messageCount++;
consumer.accept(delegate, routable1, routable2);
}
protected void receiveCommand(final QuadConsumer consumer, final T1 routable1, final T2 routable2, final T3 routable3) {
messageCount++;
consumer.accept(delegate, routable1, routable2, routable3);
}
protected void receiveCommand(final PentaConsumer consumer, final T1 routable1, final T2 routable2, final T3 routable3, final T4 routable4) {
messageCount++;
consumer.accept(delegate, routable1, routable2, routable3, routable4);
}
public > R receiveQuery(final BiFunction query, final T1 routable1) {
messageCount++;
return query.apply(delegate, routable1);
}
public > R receiveQuery(final TriFunction query, final T1 routable1, final T2 routable2) {
messageCount++;
return query.apply(delegate, routable1, routable2);
}
public > R receiveQuery(final QuadFunction query, final T1 routable1, final T2 routable2, final T3 routable3) {
messageCount++;
return query.apply(delegate, routable1, routable2, routable3);
}
public > R receiveQuery(final PentaFunction query, final T1 routable1, final T2 routable2, final T3 routable3, final T4 routable4) {
messageCount++;
return query.apply(delegate, routable1, routable2, routable3, routable4);
}
/* @see java.lang.Object#hashCode() */
@Override
public int hashCode() {
return (delegate == null) ? 0 : delegate.hashCode();
}
/* @see java.lang.Object#equals(java.lang.Object) */
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Routee other = (Routee) obj;
if (delegate == null) {
if (other.delegate != null)
return false;
} else if (!delegate.equals(other.delegate))
return false;
return true;
}
/* @see java.lang.Object#toString() */
@Override
public String toString() {
return new StringBuilder()
.append("Routee(")
.append("actor=").append(delegate)
.append(")")
.toString();
}
}