Skip to content

Commit

Permalink
Replaced two separate methods (hasTimer and currentTimer) with just c…
Browse files Browse the repository at this point in the history
…urrentTimer that returns an Optional
  • Loading branch information
tedyoung committed Dec 18, 2024
1 parent 323d9f9 commit 5824acd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.jitterted.mobreg.application.EnsembleTimerHolder;
import com.jitterted.mobreg.domain.EnsembleId;
import com.jitterted.mobreg.domain.EnsembleTimer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -74,21 +75,25 @@ public String htmlForTimerButton(Long id) {
public String htmlForTimerStatusText(Long id) {
if (ensembleTimerHolder.hasTimerFor(EnsembleId.of(id))) {
return """
<p>A timer is currently running for this Ensemble
<a class="underline font-semibold text-blue-600"
href="/member/timer-view/%s">here</a>.
</p>""".formatted(id);
}

if (ensembleTimerHolder.hasTimer()) {
return """
<p>Timer exists for
<p>A timer is currently running for this Ensemble
<a class="underline font-semibold text-blue-600"
href="/admin/ensemble/%s">another ensemble</a>.
Create will replace that timer with a new one for this Ensemble.
</p>""".formatted(ensembleTimerHolder.currentTimer().ensembleId().id());
href="/member/timer-view/%s">here</a>.
</p>""".formatted(id);
}
return "<p>No timer currently exists for this Ensemble.</p>";

return ensembleTimerHolder
.currentTimer()
.map(this::htmlWithLinkToTimer)
.orElse("<p>No timer currently exists for this Ensemble.</p>");
}

private String htmlWithLinkToTimer(EnsembleTimer timer) {
return """
<p>Timer exists for
<a class="underline font-semibold text-blue-600"
href="/admin/ensemble/%s">another ensemble</a>.
Create will replace that timer with a new one for this Ensemble.
</p>""".formatted(timer.ensembleId().id());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class EnsembleTimerHolder implements EnsembleTimerTickHandler {
Expand Down Expand Up @@ -184,12 +185,10 @@ public void resetTimerFor(EnsembleId ensembleId) {
broadcaster.sendCurrentTimer(ensembleTimer);
}

public boolean hasTimer() {
return !ensembleTimers.isEmpty();
}

public EnsembleTimer currentTimer() {
return ensembleTimers.values().iterator().next();
public Optional<EnsembleTimer> currentTimer() {
return ensembleTimers.values()
.stream()
.findFirst();
}

static class SingleEntryHashMap<K, V> extends HashMap<K, V> {
Expand Down

0 comments on commit 5824acd

Please sign in to comment.