Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code quality - drop while(true) #526

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ public void initializeApp() {
Arrays.asList(htmlPane, slidePane, liveReloadPane).forEach(viewPanel -> VBox.getVgrow(viewPanel));

threadService.runTaskLater(() -> {
// Main loop for the app
while (true) {
try {
renderLoop();
Expand Down
76 changes: 26 additions & 50 deletions src/main/java/com/kodedu/service/impl/DirectoryServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.kodedu.helper.IOHelper;
import com.kodedu.other.Current;
import com.kodedu.service.DirectoryService;
import com.kodedu.service.FileWatchService;
import com.kodedu.service.PathMapper;
import com.kodedu.service.PathResolverService;
import com.kodedu.service.ThreadService;
import com.kodedu.service.ui.FileBrowseService;
import javafx.application.Platform;
Expand All @@ -22,12 +20,12 @@
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
Expand All @@ -38,43 +36,31 @@
@Component
public class DirectoryServiceImpl implements DirectoryService {

private final ApplicationController controller;
private final FileBrowseService fileBrowser;
private final Current current;
private final PathResolverService pathResolver;
private final StoredConfigBean storedConfigBean;
@Autowired
private ApplicationController controller;
@Autowired
private FileBrowseService fileBrowser;
@Autowired
private ThreadService threadService;
@Autowired
private PathMapper pathMapper;
@Autowired
private Current current;
@Autowired
private StoredConfigBean storedConfigBean;

private final Logger logger = LoggerFactory.getLogger(DirectoryService.class);

private Optional<Path> workingDirectory = Optional.of(IOHelper.getPath(System.getProperty("user.home")));
private Optional<File> initialDirectory = Optional.empty();

private Supplier<Path> pathSaveSupplier;
private final FileWatchService fileWatchService;
private final ThreadService threadService;
private final PathMapper pathMapper;


@Autowired
public DirectoryServiceImpl(final ApplicationController controller, final FileBrowseService fileBrowser, final Current current, PathResolverService pathResolver, StoredConfigBean storedConfigBean, FileWatchService fileWatchService, ThreadService threadService, PathMapper pathMapper) {
this.controller = controller;
this.fileBrowser = fileBrowser;
this.current = current;
this.pathResolver = pathResolver;
this.storedConfigBean = storedConfigBean;
this.fileWatchService = fileWatchService;
this.threadService = threadService;
this.pathMapper = pathMapper;

pathSaveSupplier = () -> {
final FileChooser chooser = newFileChooser("Save Document");
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Asciidoc", "*.adoc", "*.asciidoc", "*.asc", "*.ad", "*.txt", "*.*"));
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown", "*.md", "*.markdown", "*.txt", "*.*"));
File file = chooser.showSaveDialog(null);
return Objects.nonNull(file) ? file.toPath() : null;
};

}
private Supplier<Path> pathSaveSupplier = () -> {
final FileChooser chooser = newFileChooser("Save Document");
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Asciidoc", "*.adoc", "*.asciidoc", "*.asc", "*.ad", "*.txt", "*.*"));
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown", "*.md", "*.markdown", "*.txt", "*.*"));
File file = chooser.showSaveDialog(null);
return Objects.nonNull(file) ? file.toPath() : null;
};

@Override
public DirectoryChooser newDirectoryChooser(String title) {
Expand Down Expand Up @@ -314,13 +300,13 @@ public Path findPathInCurrentOrWorkDir(String uri) {
.map(path -> path.resolve(uri))
.filter(Files::exists)
.findFirst()
.orElseGet(() -> null);
.orElse(null);

}

@Override
public Path findPathInPublic(String finalUri) {
List<String> uris = asList(finalUri, finalUri.replaceFirst("/", "")).stream().collect(Collectors.toList());
List<String> uris = new ArrayList<>(asList(finalUri, finalUri.replaceFirst("/", "")));
Path result = null;
for (String uri : uris) {
Path configPath = controller.getConfigPath().resolve("public");
Expand Down Expand Up @@ -360,21 +346,11 @@ public Path findPathInWorkdirOrLookup(Path uri) {
}

if (optional.isPresent()) {
Path currentParent = optional.get();

while (true) {

if (Objects.nonNull(currentParent)) {
Path candidate = currentParent.resolve(uri);
if (Files.exists(candidate)) {
return candidate;
} else {
currentParent = currentParent.getParent();
}
} else {
break;
for (Path currentParent = optional.get(); Objects.nonNull(currentParent); currentParent = currentParent.getParent()) {
Path candidate = currentParent.resolve(uri);
if (Files.exists(candidate)) {
return candidate;
}

}

}
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/kodedu/service/impl/FileWatchServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ private void watchPathChanges() {
reCreateWatchService();
}

// This should keep running to keep track of files changes
// Must be implemented as an active check due to the way Java handles listening to file changes
while (true) {

if (Objects.isNull(watcher)) {
Expand All @@ -118,6 +120,7 @@ private void watchPathChanges() {
}

List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pollEvents is not blocking, might want to add a sleep somewhere in here or use a self-scheduling method instead of a loop

watchKey.reset();

boolean updateFsView = false;
for (WatchEvent<?> event : watchEvents) {
Expand All @@ -137,14 +140,9 @@ private void watchPathChanges() {
}
}
}
watchKey.reset();
} else if (kind == ENTRY_MODIFY && event.count() > 1) {
watchKey.reset();
} else {
} else if (kind != ENTRY_MODIFY || (kind == ENTRY_MODIFY && event.count() == 0)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME: should be && event.count() <= 1

updateFsView = true;
watchKey.reset();
}

}

if (updateFsView) {
Expand All @@ -159,7 +157,6 @@ private void watchPathChanges() {
}
fileBrowseService.refreshPathToTree(path, changedPath);
}

}

}
Expand Down
71 changes: 26 additions & 45 deletions src/main/java/com/kodedu/service/ui/impl/FileBrowseServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,33 +285,26 @@ public void searchUpAndSelect(String text) {

ListIterator<TreeItem<Item>> listIterator = foundItems.listIterator();

while (true) {

if (Objects.isNull(searchFoundItem)) {
if (listIterator.hasNext()) {
searchFoundItem = listIterator.next();
}
break;
}

if (Objects.isNull(searchFoundItem)) {
if (listIterator.hasNext()) {
TreeItem<Item> next = listIterator.next();
if (next.getValue().equals(searchFoundItem.getValue())) {
if (listIterator.hasNext()) {
TreeItem<Item> nexted = listIterator.next();
searchFoundItem = listIterator.next();
}
}
while (listIterator.hasNext()) {
TreeItem<Item> next = listIterator.next();
if (next.getValue().equals(searchFoundItem.getValue())) {
if (listIterator.hasNext()) {
TreeItem<Item> nexted = listIterator.next();

if (next == nexted) {
if (listIterator.hasNext()) {
nexted = listIterator.next();
}
if (next == nexted) {
if (listIterator.hasNext()) {
nexted = listIterator.next();
}

searchFoundItem = nexted;
break;
}

searchFoundItem = nexted;
break;
}
} else {
break;
}

}
Expand All @@ -332,34 +325,23 @@ public void searchDownAndSelect(String text) {

ListIterator<TreeItem<Item>> listIterator = foundItems.listIterator();

while (true) {

if (Objects.isNull(searchFoundItem)) {
if (listIterator.hasPrevious()) {
searchFoundItem = listIterator.previous();
}

break;
if (Objects.isNull(searchFoundItem)) {
if (listIterator.hasNext()) {
searchFoundItem = listIterator.next();
}
} else {

if (listIterator.hasNext()) {
while (listIterator.hasNext()) {
TreeItem<Item> next = listIterator.next();
if (next.getValue().equals(searchFoundItem.getValue())) {
if (listIterator.hasPrevious()) {
TreeItem<Item> previous = listIterator.previous();
if (next == previous) {
if (listIterator.hasPrevious()) {
previous = listIterator.previous();
}
}
searchFoundItem = previous;
break;
if (next.getValue().equals(searchFoundItem.getValue()) && listIterator.hasPrevious()) {
TreeItem<Item> previous = listIterator.previous();
if (next == previous && listIterator.hasPrevious()) {
previous = listIterator.previous();
}
searchFoundItem = previous;
break;
}
} else {
break;
}

}

focusFoundItem(searchFoundItem);
Expand Down Expand Up @@ -439,7 +421,6 @@ public void searchAndSelect(String text) {

ListIterator<TreeItem<Item>> listIterator = foundItems.listIterator();


if (Objects.isNull(searchFoundItem)) {
if (listIterator.hasNext()) {
searchFoundItem = listIterator.next();
Expand Down