3636import java .util .concurrent .Executors ;
3737import java .util .concurrent .ScheduledExecutorService ;
3838import java .util .concurrent .TimeUnit ;
39+ import java .util .function .BiConsumer ;
3940import java .util .function .Consumer ;
41+ import java .util .function .Function ;
4042
4143/**
4244 * Builder for {@link GameAnimator}.
@@ -50,10 +52,11 @@ public class GameAnimatorBuilder {
5052
5153 private ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor (new DefaultThreadFactory ("GameAnimator" ));
5254 private List <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game > games = new ArrayList <>();
53- private Consumer <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game > applier ;
55+ private BiConsumer <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game , Function < String , String > > applier ;
5456 private long interval = 30 ;
5557 private long initialDelay = 0 ;
5658 private TimeUnit unit = TimeUnit .SECONDS ;
59+ private Function <String , String > transform = it -> it ;
5760
5861 /**
5962 * Returns the currently selected scheduler.
@@ -128,8 +131,7 @@ public GameAnimatorBuilder addGames(Collection<cc.hawkbot.regnum.client.core.dis
128131 *
129132 * @return the {@link Consumer}
130133 */
131- @ NotNull
132- public Consumer <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game > getApplier () {
134+ public BiConsumer <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game , Function <String , String >> getApplier () {
133135 return applier ;
134136 }
135137
@@ -140,7 +142,7 @@ public Consumer<cc.hawkbot.regnum.client.core.discord.GameAnimator.Game> getAppl
140142 * @return the {@link GameAnimatorBuilder}
141143 */
142144 @ NotNull
143- public GameAnimatorBuilder setApplier (@ NotNull Consumer <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game > applier ) {
145+ public GameAnimatorBuilder setApplier (BiConsumer <cc .hawkbot .regnum .client .core .discord .GameAnimator .Game , Function < String , String > > applier ) {
144146 this .applier = applier ;
145147 return this ;
146148 }
@@ -158,6 +160,7 @@ public long getInterval() {
158160 * Sets the interval in which the games are going to be changed.
159161 *
160162 * @param interval the interval as a long
163+ * @throws IllegalArgumentException when the interval is not grater than 0
161164 * @return the {@link GameAnimatorBuilder}
162165 */
163166 @ NotNull
@@ -172,6 +175,7 @@ public GameAnimatorBuilder setInterval(long interval) {
172175 *
173176 * @param interval the interval
174177 * @param unit the {@link TimeUnit} or {@code null} if you don't want to change it
178+ * @throws IllegalArgumentException when the interval is not grater than 0
175179 * @return the {@link GameAnimatorBuilder}
176180 */
177181 @ NotNull
@@ -196,6 +200,7 @@ public long getInitialDelay() {
196200 * Sets the amount of time till the first animation
197201 *
198202 * @param initialDelay the amount of time as a long
203+ * @throws IllegalArgumentException when the initialDelay is negative
199204 * @return the {@link GameAnimatorBuilder}
200205 */
201206 @ NotNull
@@ -210,6 +215,7 @@ public GameAnimatorBuilder setInitialDelay(long initialDelay) {
210215 *
211216 * @param initialDelay the amount of time as a long
212217 * @param unit the {@link TimeUnit} or {@code null} if you don't want to change it
218+ * @throws IllegalArgumentException when the initialDelay is negative
213219 * @return the {@link GameAnimatorBuilder}
214220 */
215221 @ NotNull
@@ -253,23 +259,43 @@ public GameAnimatorBuilder setTimeUnit(@NotNull TimeUnit unit) {
253259 *
254260 * @param jda the {@link JDA} instance
255261 * @return the {@link GameAnimatorBuilder}
256- * @see GameAnimatorBuilder#setApplier(Consumer )
262+ * @see GameAnimatorBuilder#setApplier(BiConsumer )
257263 */
258264 @ NotNull
259265 public GameAnimatorBuilder setJDA (JDA jda ) {
260- return setApplier ((game ) -> JDAExtensions .applyGame (jda , game ));
266+ return setApplier ((game , transform ) -> JDAExtensions .applyGame (jda , game , transform ));
261267 }
262268
263269 /**
264270 * Uses an {@link ShardManager} instance for the Applier.
265271 *
266272 * @param shardManager the {@link ShardManager} instance
267273 * @return the {@link GameAnimatorBuilder}
268- * @see GameAnimatorBuilder#setApplier(Consumer )
274+ * @see GameAnimatorBuilder#setApplier(BiConsumer )
269275 */
270276 @ NotNull
271277 public GameAnimatorBuilder setShardManager (ShardManager shardManager ) {
272- return setApplier ((game ) -> JDAExtensions .applyGame (shardManager , game ));
278+ return setApplier ((game , transform ) -> JDAExtensions .applyGame (shardManager , game , transform ));
279+ }
280+
281+ /**
282+ * Returns the function that transfroms every game before applying it
283+ * @return the {@link Function}
284+ */
285+ @ NotNull
286+ public Function <String , String > getTransform () {
287+ return transform ;
288+ }
289+
290+ /**
291+ * Sets the function that transfroms every game before applying it
292+ * @param transform the {@link Function}
293+ * @return the {@link GameAnimatorBuilder}
294+ */
295+ @ NotNull
296+ public GameAnimatorBuilder setTransform (Function <String , String > transform ) {
297+ this .transform = transform ;
298+ return this ;
273299 }
274300
275301 /**
@@ -279,13 +305,15 @@ public GameAnimatorBuilder setShardManager(ShardManager shardManager) {
279305 */
280306 @ NotNull
281307 public GameAnimator build () {
308+ Preconditions .checkState (games .size () >= 2 , "You have to register at least two games!" );
282309 return new GameAnimator (
283310 scheduler ,
284311 ImmutableList .copyOf (games ),
285312 applier ,
286313 interval ,
287314 initialDelay ,
288- unit
315+ unit ,
316+ transform
289317 );
290318 }
291319}
0 commit comments