Skip to content

Package: MessageSupport$Exclusions

MessageSupport$Exclusions

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.actor;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import javax.inject.Provider;
32: import java.util.Optional;
33: import java.util.Timer;
34: import java.util.TimerTask;
35: import java.util.concurrent.TimeUnit;
36: import java.io.Serializable;
37: import it.tidalwave.actor.impl.DefaultCollaboration;
38: import it.tidalwave.actor.impl.Locator;
39: import it.tidalwave.actor.spi.CollaborationAwareMessageBus;
40: import it.tidalwave.util.As;
41: import lombok.EqualsAndHashCode;
42: import lombok.experimental.Delegate;
43: import lombok.extern.slf4j.Slf4j;
44: import static it.tidalwave.actor.MessageDecorator._MessageDecorator_;
45:
46: /***********************************************************************************************************************
47: *
48: * A support class for implementing messages.
49: *
50: * @stereotype Message
51: *
52: * @author Fabrizio Giudici
53: *
54: **********************************************************************************************************************/
55: @Slf4j @EqualsAndHashCode(of = "collaboration")
56: public abstract class MessageSupport implements Collaboration.Provider, As, Serializable
57: {
58: // TODO: @Inject
59: private final Provider<CollaborationAwareMessageBus> messageBus =
60: Locator.createProviderFor(CollaborationAwareMessageBus.class);
61:
62: @Nonnull
63: protected final DefaultCollaboration collaboration;
64:
65: private final MessageDecorator sameMessageDecorator = new MessageDecorator.Same<>(this);
66:
67: interface Exclusions
68: {
69: public <T> T maybeAs (Class<? extends T> type);
70: }
71:
72: @Delegate(excludes = Exclusions.class)
73: private final As as = As.forObject(this);
74:
75: /*******************************************************************************************************************
76: *
77: *
78: *
79: ******************************************************************************************************************/
80: protected MessageSupport()
81: {
82: this.collaboration = DefaultCollaboration.getOrCreateCollaboration(this);
83: }
84:
85: /*******************************************************************************************************************
86: *
87: * @param collaboration the collaboration
88: *
89: ******************************************************************************************************************/
90: protected MessageSupport (@Nonnull final Collaboration collaboration)
91: {
92: this.collaboration = (DefaultCollaboration)collaboration;
93: }
94:
95: /*******************************************************************************************************************
96: *
97: * Returns the {@link Collaboration} that this message is part of.
98: *
99: * @return the {@code Collaboration}
100: *
101: ******************************************************************************************************************/
102: @Override @Nonnull
103: public Collaboration getCollaboration()
104: {
105: return collaboration;
106: }
107:
108: /*******************************************************************************************************************
109: *
110: * Sends this message, eventually performing a replacement (see {@link MessageDecorator} for further info).
111: *
112: * @return the {@code Collaboration} that this message is part of
113: *
114: ******************************************************************************************************************/
115: @Nonnull @SuppressWarnings("UnusedReturnValue")
116: public Collaboration send()
117: {
118: log.debug("send() - {}", this);
119: return findDecoratedMessage().sendDirectly();
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * Sends this message directly, not performing any replacement (see {@link MessageDecorator} for further info).
125: *
126: * @return the {@code Collaboration} that this message is part of
127: *
128: ******************************************************************************************************************/
129: @Nonnull
130: public Collaboration sendDirectly()
131: {
132: log.debug("sendDirectly() - {}", this);
133: collaboration.registerDeliveringMessage(this);
134: messageBus.get().publish(this);
135: return collaboration;
136: }
137:
138: /*******************************************************************************************************************
139: *
140: * Sends this message after a delay, eventually performing a replacement (see {@link MessageDecorator} for
141: * further info).
142: *
143: * @param delay the delay
144: * @param timeUnit the {@link TimeUnit} for the delay
145: * @return the {@code Collaboration} that this message is part of
146: *
147: ******************************************************************************************************************/
148: @Nonnull
149: public Collaboration sendLater (@Nonnegative final int delay, @Nonnull final TimeUnit timeUnit)
150: {
151: log.debug("sendLater({}, {}) - {}", delay, timeUnit, this);
152: final var message = findDecoratedMessage();
153: collaboration.registerDeliveringMessage(message);
154:
155: new Timer().schedule(new TimerTask()
156: {
157: @Override
158: public void run()
159: {
160: messageBus.get().publish(message);
161: }
162: }, TimeUnit.MILLISECONDS.convert(delay, timeUnit));
163:
164: return collaboration;
165: }
166:
167: /*******************************************************************************************************************
168: *
169: * {@inheritDoc}
170: *
171: ******************************************************************************************************************/
172: @Override @Nonnull
173: public <T> Optional<T> maybeAs (@Nonnull final Class<? extends T> type)
174: {
175: final Optional<T> t = as.maybeAs(type);
176:
177: return t.isPresent()
178: ? t
179: : type.equals(MessageDecorator.class) ? Optional.of(type.cast(sameMessageDecorator)) : Optional.empty();
180: }
181:
182: /*******************************************************************************************************************
183: *
184: *
185: *
186: ******************************************************************************************************************/
187: @Nonnull
188: private MessageSupport findDecoratedMessage()
189: {
190: final var decoratedMessage = this.as(_MessageDecorator_).getDecoratedMessage();
191: return (decoratedMessage == this) ? this : decoratedMessage.findDecoratedMessage();
192: // MessageSupport decoratedMessage = this.as(_MessageDecorator_).getDecoratedMessage();
193: //
194: // if (decoratedMessage != this)
195: // {
196: // log.info("MESSAGE HAS BEEN DECORATED: {} -> {}", this, decoratedMessage);
197: // decoratedMessage = decoratedMessage.findDecoratedMessage();
198: // }
199: //
200: // return decoratedMessage;
201: }
202: }