-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow editing of problems and refactor IO utilities
- Loading branch information
Showing
14 changed files
with
617 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/danielptv/simplex/command/HelperComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.danielptv.simplex.command; | ||
|
||
import com.danielptv.simplex.shell.EditType; | ||
import com.danielptv.simplex.shell.InputResult; | ||
import com.danielptv.simplex.shell.SimplexInput; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jline.terminal.Terminal; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.shell.component.SingleItemSelector; | ||
import org.springframework.shell.component.support.SelectorItem; | ||
import org.springframework.shell.standard.ShellComponent; | ||
import org.springframework.shell.style.TemplateExecutor; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@SuppressFBWarnings("EI_EXPOSE_REP2") | ||
@ShellComponent | ||
@RequiredArgsConstructor | ||
public class HelperComponent { | ||
private static final String EDIT_PROBLEM_TITLE = "Edit problem"; | ||
private static final Map<String, String> EDIT_PROBLEM_SELECTION = Map.of( | ||
EditType.CONTINUE.toString(), | ||
"Continue to calculation", | ||
EditType.EDIT.toString(), | ||
"Edit the linear problem" | ||
); | ||
private final Terminal terminal; | ||
private final ResourceLoader resourceLoader; | ||
private final ObjectProvider<TemplateExecutor> templateExecutorProvider; | ||
|
||
public String singleSelector(final String name, final Map<String, String> data) { | ||
final var items = data.entrySet().stream() | ||
.map(entry -> SelectorItem.of(entry.getKey(), entry.getValue())) | ||
.toList(); | ||
final var component = new SingleItemSelector<>(terminal, | ||
items, name, null); | ||
component.setResourceLoader(resourceLoader); | ||
component.setTemplateExecutor(templateExecutorProvider.getObject()); | ||
final var context = component.run(SingleItemSelector.SingleItemSelectorContext.empty()); | ||
return context.getResultItem().flatMap(si -> Optional.ofNullable(si.getName())).orElse(null); | ||
} | ||
|
||
public EditType editProblem() { | ||
final var result = singleSelector(EDIT_PROBLEM_TITLE, EDIT_PROBLEM_SELECTION); | ||
return result.equals(EditType.CONTINUE.toString()) ? EditType.CONTINUE : EditType.EDIT; | ||
} | ||
|
||
public InputResult simplexInput( | ||
final String name, | ||
final int varCount, | ||
final boolean isObjFunction, | ||
final boolean minimize | ||
) { | ||
return simplexInput(name, varCount, isObjFunction, minimize, null); | ||
} | ||
|
||
public InputResult simplexInput( | ||
final String name, | ||
final int varCount, | ||
final boolean isObjFunction, | ||
final boolean minimize, | ||
final InputResult defaultValue | ||
) { | ||
final var component = new SimplexInput(terminal, name, varCount, isObjFunction, minimize, defaultValue); | ||
component.setResourceLoader(resourceLoader); | ||
component.setTemplateExecutor(templateExecutorProvider.getObject()); | ||
final var context = component.run(SimplexInput.SimplexInputContext.empty()); | ||
return context.getResultValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.danielptv.simplex.shell; | ||
|
||
public enum EditType { | ||
EDIT("Edit"), | ||
CONTINUE("Continue"); | ||
private final String value; | ||
|
||
EditType(final String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
|
||
} |
40 changes: 0 additions & 40 deletions
40
src/main/java/com/danielptv/simplex/shell/InputReader.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
src/main/java/com/danielptv/simplex/shell/InputResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.danielptv.simplex.shell; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) | ||
@AllArgsConstructor | ||
@RequiredArgsConstructor | ||
public final class InputResult implements Serializable { | ||
@Getter | ||
@Setter | ||
private List<String> values = new ArrayList<>(); | ||
@Getter | ||
@Setter | ||
private String representation = ""; | ||
@Getter | ||
@Setter | ||
private String rawInput = ""; | ||
|
||
@Override | ||
public String toString() { | ||
return representation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.