Skip to content

Package: MessageSupport$2

MessageSupport$2

nameinstructionbranchcomplexitylinemethod
run(Throwable)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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.Timer;
33: import java.util.TimerTask;
34: import java.util.concurrent.TimeUnit;
35: import java.io.Serializable;
36: import it.tidalwave.actor.impl.DefaultCollaboration;
37: import it.tidalwave.actor.impl.Locator;
38: import it.tidalwave.actor.spi.CollaborationAwareMessageBus;
39: import it.tidalwave.util.As;
40: import it.tidalwave.util.AsException;
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: // @Delegate // FIXME: use AsSupport
68: // private AsDelegate asDelegate = AsDelegateProvider.Locator.find().createAsDelegate(this);
69:
70: interface Exclusions
71: {
72: public <T> T as (Class<T> type);
73: }
74:
75: @Delegate(excludes = Exclusions.class)
76: private final As as = As.forObject(this);
77:
78: /*******************************************************************************************************************
79: *
80: *
81: *
82: ******************************************************************************************************************/
83: protected MessageSupport()
84: {
85: this.collaboration = DefaultCollaboration.getOrCreateCollaboration(this);
86: }
87:
88: /*******************************************************************************************************************
89: *
90: * @param collaboration the collaboration
91: *
92: ******************************************************************************************************************/
93: protected MessageSupport (@Nonnull final Collaboration collaboration)
94: {
95: this.collaboration = (DefaultCollaboration)collaboration;
96: }
97:
98: /*******************************************************************************************************************
99: *
100: * Returns the {@link Collaboration} that this message is part of.
101: *
102: * @return the {@code Collaboration}
103: *
104: ******************************************************************************************************************/
105: @Override @Nonnull
106: public Collaboration getCollaboration()
107: {
108: return collaboration;
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * Sends this message, eventually performing a replacement (see {@link MessageDecorator} for further info).
114: *
115: * @return the {@code Collaboration} that this message is part of
116: *
117: ******************************************************************************************************************/
118: @Nonnull @SuppressWarnings("UnusedReturnValue")
119: public Collaboration send()
120: {
121: log.debug("send() - {}", this);
122: return findDecoratedMessage().sendDirectly();
123: }
124:
125: /*******************************************************************************************************************
126: *
127: * Sends this message directly, not performing any replacement (see {@link MessageDecorator} for further info).
128: *
129: * @return the {@code Collaboration} that this message is part of
130: *
131: ******************************************************************************************************************/
132: @Nonnull
133: public Collaboration sendDirectly()
134: {
135: log.debug("sendDirectly() - {}", this);
136: collaboration.registerDeliveringMessage(this);
137: messageBus.get().publish(this);
138: return collaboration;
139: }
140:
141: /*******************************************************************************************************************
142: *
143: * Sends this message after a delay, eventually performing a replacement (see {@link MessageDecorator} for
144: * further info).
145: *
146: * @param delay the delay
147: * @param timeUnit the {@link TimeUnit} for the delay
148: * @return the {@code Collaboration} that this message is part of
149: *
150: ******************************************************************************************************************/
151: @Nonnull
152: public Collaboration sendLater (@Nonnegative final int delay, @Nonnull final TimeUnit timeUnit)
153: {
154: log.debug("sendLater({}, {}) - {}", delay, timeUnit, this);
155: final MessageSupport message = findDecoratedMessage();
156: collaboration.registerDeliveringMessage(message);
157:
158: new Timer().schedule(new TimerTask()
159: {
160: @Override
161: public void run()
162: {
163: messageBus.get().publish(message);
164: }
165: }, TimeUnit.MILLISECONDS.convert(delay, timeUnit));
166:
167: return collaboration;
168: }
169:
170: /*******************************************************************************************************************
171: *
172: * {@inheritDoc}
173: *
174: ******************************************************************************************************************/
175: @Override @Nonnull
176: public <T> T as (@Nonnull final Class<T> type)
177: {
178: return as(type, new As.NotFoundBehaviour<T>()
179: {
180: @Nonnull
181: public T run (@Nonnull final Throwable t)
182: {
183:• if (type.equals(MessageDecorator.class))
184: {
185: return type.cast(sameMessageDecorator);
186: }
187:
188: throw new AsException(type, t);
189: }
190: });
191: }
192:
193: /*******************************************************************************************************************
194: *
195: *
196: *
197: ******************************************************************************************************************/
198: @Nonnull
199: private MessageSupport findDecoratedMessage()
200: {
201: final MessageSupport decoratedMessage = this.as(_MessageDecorator_).getDecoratedMessage();
202: return (decoratedMessage == this) ? this : decoratedMessage.findDecoratedMessage();
203: // MessageSupport decoratedMessage = this.as(_MessageDecorator_).getDecoratedMessage();
204: //
205: // if (decoratedMessage != this)
206: // {
207: // log.info("MESSAGE HAS BEEN DECORATED: {} -> {}", this, decoratedMessage);
208: // decoratedMessage = decoratedMessage.findDecoratedMessage();
209: // }
210: //
211: // return decoratedMessage;
212: }
213: }