Skip to content

Commit

Permalink
Fix sonar warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed May 1, 2024
1 parent 8620692 commit bacc130
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private InterruptionDetectedDecision showAutomaticInterruptionDialog(final Local
LOG.info("Showing automatic interruption alert starting at {} for {}...", startOfInterruption,
interruption);
final Optional<ButtonType> selectedButton = alert.showAndWait();
final InterruptionDetectedDecision decision = evaluateButton(selectedButton);
final InterruptionDetectedDecision decision = evaluateButton(selectedButton.orElse(null));
LOG.info("User clicked button {} -> {}", selectedButton, decision);
return decision;
}
Expand All @@ -313,26 +313,23 @@ private Alert createAlertDialog(final LocalTime startOfInterruption, final Durat
return alert;
}

private InterruptionDetectedDecision evaluateButton(final Optional<ButtonType> selectedButton)
private InterruptionDetectedDecision evaluateButton(final ButtonType selectedButton)
{
if (isButton(selectedButton, ButtonData.FINISH) && !state.stoppedWorkingForToday.get())
if (selectedButton == null)
{
return InterruptionDetectedDecision.SKIP_INTERRUPTION;
}
if (selectedButton.getButtonData() == ButtonData.FINISH && !state.stoppedWorkingForToday.get())
{
return InterruptionDetectedDecision.STOP_WORKING_FOR_TODAY;
}
if (isButton(selectedButton, ButtonData.YES))
if (selectedButton.getButtonData() == ButtonData.YES)
{
return InterruptionDetectedDecision.ADD_INTERRUPTION;
}
return InterruptionDetectedDecision.SKIP_INTERRUPTION;
}

private boolean isButton(final Optional<ButtonType> button, final ButtonData data)
{
return button.map(ButtonType::getButtonData)
.filter(d -> d == data)
.isPresent();
}

@Override
public void recordUpdated(final DayRecord day)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.text.ParsePosition;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.function.UnaryOperator;

import org.itsallcode.whiterabbit.logic.service.AppService;
Expand Down Expand Up @@ -65,7 +66,7 @@ private MenuItem createMenuitem(final Duration interruption)
final String verb = interruption.isNegative() ? "Subtract" : "Add";
final MenuItem menuItem = new MenuItem(
verb + " interruption of " + appService.formatter().format(interruption.abs()));
menuItem.setId(verb.toLowerCase() + "-interruption-preset-" + interruption.toString());
menuItem.setId(verb.toLowerCase(Locale.ENGLISH) + "-interruption-preset-" + interruption.toString());
menuItem.setOnAction(event -> addInterruptionForToday(interruption));
return menuItem;
}
Expand All @@ -83,7 +84,6 @@ private static class DurationInputDialog extends Dialog<Duration>

private DurationInputDialog()
{
final DialogPane dialogPane = getDialogPane();
final int maxValue = (int) Duration.ofHours(8).toMinutes();

spinner = new Spinner<>(0, maxValue, 0, 5);
Expand Down Expand Up @@ -117,6 +117,7 @@ private DurationInputDialog()
GridPane.setHgrow(spinner, Priority.ALWAYS);
GridPane.setFillWidth(spinner, true);

final DialogPane dialogPane = getDialogPane();
label = createContentLabel(dialogPane.getContentText());
label.setPrefWidth(Region.USE_COMPUTED_SIZE);
label.textProperty().bind(dialogPane.contentTextProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,11 @@ private void updateRows(final List<DayRecord> sortedDays)

private void updateSelectedRow(final MonthIndex previousMonth, final MonthIndex month)
{
final boolean isCurrentMonth = month.getYearMonth().equals(clockService.getCurrentYearMonth());
final boolean otherMonthSelected = previousMonth != null
&& !month.getYearMonth().equals(previousMonth.getYearMonth());
if (!otherMonthSelected)
if (!otherMonthSelected(previousMonth, month))
{
return;
}
if (isCurrentMonth)
if (isCurrentMonth(month))
{
selectRow(clockService.getCurrentDate());
}
Expand All @@ -120,6 +117,17 @@ private void updateSelectedRow(final MonthIndex previousMonth, final MonthIndex
}
}

private boolean otherMonthSelected(final MonthIndex previousMonth, final MonthIndex month)
{
return previousMonth != null
&& !month.getYearMonth().equals(previousMonth.getYearMonth());
}

private boolean isCurrentMonth(final MonthIndex month)
{
return month.getYearMonth().equals(clockService.getCurrentYearMonth());
}

@SuppressWarnings("java:S110") // Deep inheritance tree required by JavaFx
public TableView<DayRecordPropertyAdapter> initTable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private TreeTableView<ReportRow> createTreeTable()
final TreeItem<ReportRow> root = new TreeItem<>();

root.getChildren().addAll(report.getDays().stream()
.map(this::createDayTreeItem)
.map(DailyProjectReportViewer::createDayTreeItem)
.toList());

final TreeTableView<ReportRow> treeTable = new TreeTableView<>(root);
Expand All @@ -125,7 +125,7 @@ private TreeTableView<ReportRow> createTreeTable()
return treeTable;
}

private TreeItem<ReportRow> createDayTreeItem(final ProjectReportDay day)
private static TreeItem<ReportRow> createDayTreeItem(final ProjectReportDay day)
{
final TreeItem<ReportRow> treeItem = new TreeItem<>(new ReportRow(day));
treeItem.setExpanded(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public MenuBar build()
return menuBar;
}

private SeparatorMenuItem separatorItem()
private static SeparatorMenuItem separatorItem()
{
return new SeparatorMenuItem();
}

private Menu menu(final String label, final String id)
private static Menu menu(final String label, final String id)
{
final Menu menu = new Menu(label);
menu.setId(id);
Expand All @@ -91,12 +91,12 @@ private MenuItem createStopWorkingForTodayMenuItem()
return menuItem;
}

private MenuItem menuItem(final String label, final String id, final Runnable action)
private static MenuItem menuItem(final String label, final String id, final Runnable action)
{
return menuItem(label, id, event -> action.run());
}

private MenuItem menuItem(final String label, final String id, final EventHandler<ActionEvent> action)
private static MenuItem menuItem(final String label, final String id, final EventHandler<ActionEvent> action)
{
final MenuItem menuItem = new MenuItem(label);
menuItem.setId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ private TableView<PluginTableEntry> createTableView()
private List<PluginTableEntry> getAllPlugins()
{
return pluginManager.getAllPlugins().stream()
.map(this::createTableEntry)
.map(PluginManagerViewer::createTableEntry)
.toList();
}

private PluginTableEntry createTableEntry(final AppPlugin plugin)
private static PluginTableEntry createTableEntry(final AppPlugin plugin)
{
return new PluginTableEntry(plugin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private TableView<ReportRow> createTreeTable()
return treeTable;
}

private ReportRow createRow(final int year, final ProjectReportActivity project)
private static ReportRow createRow(final int year, final ProjectReportActivity project)
{
return new ReportRow(year, project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private MenuItem createMenuItem(final AutocompleteProposal result)
return item;
}

private List<Text> highlightMatch(final AutocompleteProposal result)
private static List<Text> highlightMatch(final AutocompleteProposal result)
{
final int matchPositionStart = result.getMatchPositionStart();
final List<Text> textParts = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void setCommandLineArgs(final List<String> commandLineArgs)

protected void doStart(final Stage stage, final ProjectConfig projectConfig)
{
LOG.info("Starting application using stage {}", stage);
LOG.debug("Starting application using stage {}", stage);

timeUtil = TimeUtil.start(initialTime);
final TestDirProvider testDirProvider = TestDirProvider.create(tempDir);
Expand All @@ -107,14 +107,14 @@ protected void doStart(final Stage stage, final ProjectConfig projectConfig)
this.javaFxApp.start(stage);

timeUtil.captureScheduledRunnables();
LOG.info("Application startup finished");
LOG.debug("Application startup finished");
}

protected void doStop()
{
LOG.info("Preparing application shutdown");
LOG.debug("Preparing application shutdown");
this.javaFxApp.prepareShutdown();
LOG.info("Application shutdown done");
LOG.debug("Application shutdown done");
}

private void prepareConfiguration(final ProjectConfig projectConfig, final WorkingDirProvider testDirProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.itsallcode.whiterabbit.logic.autocomplete;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;

import java.time.LocalDate;
import java.time.Period;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -30,15 +32,16 @@ public class AutocompleteService
private final CachingStorage storage;
private final ClockService clockService;

private final CachingAutocompleter dayCommentAutocompleter = new CachingAutocompleter(this::getDayComments);
private final CachingAutocompleter activityCommentAutocompleter = new CachingAutocompleter(
this::getActivityComments);
private final CachingAutocompleter dayCommentAutocompleter;
private final CachingAutocompleter activityCommentAutocompleter;

public AutocompleteService(final CachingStorage storage, final ClockService clockService)
public AutocompleteService(final CachingStorage storage, final ClockService clockService, final Locale locale)
{
this.storage = storage;
this.clockService = clockService;
storage.addCacheInvalidationListener(this::invalidateCache);
this.dayCommentAutocompleter = new CachingAutocompleter(this::getDayComments, locale);
this.activityCommentAutocompleter = new CachingAutocompleter(this::getActivityComments, locale);
this.storage = storage;
this.storage.addCacheInvalidationListener(this::invalidateCache);
}

private void invalidateCache(final MonthIndex updatedMonth)
Expand Down Expand Up @@ -108,14 +111,16 @@ public Optional<ProjectImpl> getSuggestedProject()
return mostFrequentlyUsedProject;
}

private class CachingAutocompleter implements AutocompleteEntrySupplier
private static class CachingAutocompleter implements AutocompleteEntrySupplier
{
private TextIndex index;
private final Supplier<List<String>> availableTextSupplier;
private final Locale locale;

private CachingAutocompleter(final Supplier<List<String>> availableTextSupplier)
private CachingAutocompleter(final Supplier<List<String>> availableTextSupplier, final Locale locale)
{
this.availableTextSupplier = availableTextSupplier;
this.locale = locale;
}

private void invalidateCache()
Expand All @@ -128,7 +133,7 @@ public List<AutocompleteProposal> getEntries(final String prompt)
{
if (index == null)
{
index = TextIndex.build(availableTextSupplier.get());
index = TextIndex.build(availableTextSupplier.get(), locale);
}
return index.getEntries(prompt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static java.util.stream.Collectors.groupingBy;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
Expand All @@ -21,28 +23,35 @@ class TextIndex implements AutocompleteEntrySupplier
private final Map<String, List<String>> lowerCaseIndex;
private final SortedSet<String> lowerCaseValues;
private final Map<String, Long> lowerCaseFrequency;
private final Locale locale;

private TextIndex(final Map<String, List<String>> lowerCaseIndex, final SortedSet<String> lowerCaseValues,
final Map<String, Long> lowerCaseFrequency)
final Map<String, Long> lowerCaseFrequency, final Locale locale)
{
this.lowerCaseIndex = lowerCaseIndex;
this.lowerCaseValues = lowerCaseValues;
this.lowerCaseFrequency = lowerCaseFrequency;
this.lowerCaseIndex = new HashMap<>(lowerCaseIndex);
this.lowerCaseValues = new TreeSet<>(lowerCaseValues);
this.lowerCaseFrequency = new HashMap<>(lowerCaseFrequency);
this.locale = locale;
}

static TextIndex build(final Collection<String> entries)
static TextIndex build(final Collection<String> entries, final Locale locale)
{
final List<String> uniqueEntries = entries.stream().distinct().toList();

final Map<String, List<String>> lowerCaseIndex = uniqueEntries.stream()
.collect(groupingBy(String::toLowerCase));
.collect(groupingBy(value -> value.toLowerCase(locale)));
final SortedSet<String> lowerCaseValues = new TreeSet<>(lowerCaseIndex.keySet());
final Map<String, Long> lowerCaseFrequency = entries.stream().map(String::toLowerCase)
final Map<String, Long> lowerCaseFrequency = entries.stream().map(value -> value.toLowerCase(locale))
.collect(groupingBy(identity(), counting()));
LOG.trace("Creating autocompleter for {} entries ({} unique): {}, frequencies: {}", entries.size(),
uniqueEntries.size(),
uniqueEntries, lowerCaseFrequency);
return new TextIndex(lowerCaseIndex, lowerCaseValues, lowerCaseFrequency);
return new TextIndex(lowerCaseIndex, lowerCaseValues, lowerCaseFrequency, locale);
}

private String toLowerCase(final String value)
{
return value.toLowerCase(locale);
}

@Override
Expand All @@ -56,8 +65,8 @@ public List<AutocompleteProposal> getEntries(final String searchText)
{
return createProposals(lowerCaseValues, searchText);
}
final SortedSet<String> lowerCaseMatches = lowerCaseValues.subSet(searchText.toLowerCase(),
searchText.toLowerCase() + Character.MAX_VALUE);
final SortedSet<String> lowerCaseMatches = lowerCaseValues.subSet(toLowerCase(searchText),
toLowerCase(searchText) + Character.MAX_VALUE);
return createProposals(lowerCaseMatches, searchText);
}

Expand All @@ -75,8 +84,8 @@ private List<AutocompleteProposal> createProposals(final SortedSet<String> lower

private AutocompleteProposal createProposal(final String searchText, final String proposedText)
{
final int matchPositionStart = proposedText.toLowerCase().indexOf(searchText.toLowerCase());
final long priority = lowerCaseFrequency.getOrDefault(proposedText.toLowerCase(), 0L);
final int matchPositionStart = toLowerCase(proposedText).indexOf(toLowerCase(searchText));
final long priority = lowerCaseFrequency.getOrDefault(toLowerCase(proposedText), 0L);
LOG.trace("Create proposal for '{}'. Proposal: {}, priority: {}", searchText, proposedText, priority);
return new AutocompleteProposal(proposedText, priority, matchPositionStart, searchText.length());
}
Expand Down
Loading

0 comments on commit bacc130

Please sign in to comment.