Skip to content

Commit

Permalink
Made it possible to skip a task if a prior task has been skipped.
Browse files Browse the repository at this point in the history
This is achieved by registering a token value in the MachieneContext of the stepEngine.
When a non-skippable step is encountered the token value is cleared away.
  • Loading branch information
mklaehn committed Oct 2, 2023
1 parent 0b611fb commit 1b6c331
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,14 @@ private Node createSpeakerImage(SpeakerImageProvider speakerImageProvider, Speak

@Override
public boolean shouldSkip(final MachineContext context) {
return votedTalksConverter.apply(context).isEmpty()
final boolean skip = votedTalksConverter.apply(context).isEmpty()
|| null != ((WordleSkin) context.get("WordleSkin")).getNode().lookup(lookupId);

if (skip) {
context.put(SKIP_TOKEN, config.skipTokenValue);
}

return skip;
}

@Override
Expand Down Expand Up @@ -362,6 +368,7 @@ public static class Config extends AbstractConfig {
public boolean compressedAvatars = true;
public int compressedAvatarsLimit = 4;
public boolean showTags = false;
public String skipTokenValue = null;

/**
* Provides the type of the Top Voted display to flip out.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2019 TweetWallFX
* Copyright (c) 2015-2023 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,6 +35,14 @@
*/
public interface Step {

/**
* The token a {@link Step} may register with {@link MachineContext} to mark it
* has been skipped. Steps following a skipped step may take the presence and
* value of such a token into account wehn deciding if their step shall be
* skipped.
*/
public static final String SKIP_TOKEN = "skip.token";

/**
* Initiates this {@link Step} prior to being included in the
* {@link StepEngine}. This method called only after instantiation by the
Expand Down Expand Up @@ -147,7 +155,8 @@ interface Factory {
* {@link DataProvider DataProviders} required by the created
* {@link Step}
*/
default Collection<Class<? extends DataProvider>> getRequiredDataProviders(final StepEngineSettings.StepDefinition stepDefinition) {
default Collection<Class<? extends DataProvider>> getRequiredDataProviders(
final StepEngineSettings.StepDefinition stepDefinition) {
return Collections.emptyList();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 TweetWallFX
* Copyright (c) 2016-2023 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -188,7 +188,12 @@ public <T> T get(final String key, final Class<T> clazz) {
}

public Object put(final String key, final Object value) {
return properties.put(key, value);
Objects.requireNonNull(key, "key must not be null");
if (null == value) {
return properties.remove(key);
} else {
return properties.put(key, value);
}
}

public void proceed() {
Expand Down Expand Up @@ -236,6 +241,8 @@ private void process() {
step = stepIterator.next();
context.restrictAvailableDataProviders(stepIterator.getRequiredDataProviders(step));
}
// found a step not being skipped. so reset the SKIP_TOKEN
context.put(Step.SKIP_TOKEN, null);
final Step stepToExecute = step;
final Duration duration = step.preferredStepDuration(context);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2019 TweetWallFX
* Copyright (c) 2015-2023 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -35,9 +35,11 @@
public class PauseStep implements Step {

private final Duration pause;
private final String skipWhenSkipped;

private PauseStep(final Duration pause) {
private PauseStep(final Duration pause, final String skipWhenSkipped) {
this.pause = pause;
this.skipWhenSkipped = skipWhenSkipped;
}

@Override
Expand All @@ -55,6 +57,13 @@ public boolean requiresPlatformThread() {
return false;
}

@Override
public boolean shouldSkip(final MachineContext context) {
return null == skipWhenSkipped
? false
: skipWhenSkipped.equals(context.get(Step.SKIP_TOKEN));
}

/**
* Implementation of {@link Step.Factory} as Service implementation creating
* {@link PauseStep}.
Expand All @@ -64,7 +73,7 @@ public static final class FactoryImpl implements Step.Factory {
@Override
public Step create(final StepEngineSettings.StepDefinition stepDefinition) {
final Config config = stepDefinition.getConfig(Config.class);
return new PauseStep(config.getDuration());
return new PauseStep(config.getDuration(), config.getSkipWhenSkipped());
}

@Override
Expand All @@ -77,6 +86,7 @@ public static class Config {

private ChronoUnit unit = ChronoUnit.SECONDS;
private long amount = 5;
private String skipWhenSkipped = null;

public ChronoUnit getUnit() {
return unit;
Expand All @@ -98,12 +108,20 @@ private Duration getDuration() {
return Duration.of(getAmount(), getUnit());
}

public void setSkipWhenSkipped(String skipWhenSkipped) {
this.skipWhenSkipped = Objects.requireNonNull(skipWhenSkipped, "skipWhenSkipped must not be null!");
}

public String getSkipWhenSkipped() {
return skipWhenSkipped;
}

@Override
public String toString() {
return createToString(this, map(
"amount", getAmount(),
"unit", getUnit()
)) + " extends " + super.toString();
"unit", getUnit(),
"skipWhenSkipped", getSkipWhenSkipped()));
}
}
}

0 comments on commit 1b6c331

Please sign in to comment.