From 5cde88e4f1c89624671d782c6f03cc297f0f3dbe Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Tue, 12 Dec 2023 02:44:36 +0100 Subject: [PATCH] Reformat code --- plexus-interactivity-api/pom.xml | 3 +- .../interactivity/AbstractInputHandler.java | 17 +- .../interactivity/DefaultInputHandler.java | 18 +- .../interactivity/DefaultOutputHandler.java | 20 +- .../interactivity/DefaultPrompter.java | 195 ++++++------------ .../interactivity/InputHandler.java | 14 +- .../interactivity/OutputHandler.java | 9 +- .../components/interactivity/Prompter.java | 21 +- .../interactivity/PrompterException.java | 14 +- .../interactivity/DefaultPrompterTest.java | 46 ++--- plexus-interactivity-jline/pom.xml | 5 +- .../jline/JLineInputHandler.java | 33 ++- pom.xml | 13 +- 13 files changed, 150 insertions(+), 258 deletions(-) diff --git a/plexus-interactivity-api/pom.xml b/plexus-interactivity-api/pom.xml index 6bb90ad..03c6528 100644 --- a/plexus-interactivity-api/pom.xml +++ b/plexus-interactivity-api/pom.xml @@ -1,9 +1,10 @@ + 4.0.0 - plexus-interactivity org.codehaus.plexus + plexus-interactivity 1.2-SNAPSHOT diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java index e956669..2f3bc6f 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/AbstractInputHandler.java @@ -24,9 +24,9 @@ * SOFTWARE. */ -import java.util.List; -import java.util.ArrayList; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * Base input handler, implements a default readMultipleLines. @@ -34,17 +34,12 @@ * @author Brett Porter * @version $Id$ */ -public abstract class AbstractInputHandler - implements InputHandler -{ - public List readMultipleLines() - throws IOException - { +public abstract class AbstractInputHandler implements InputHandler { + public List readMultipleLines() throws IOException { List lines = new ArrayList<>(); String line = readLine(); - while ( line != null && line.length() > 0 ) - { - lines.add( line ); + while (line != null && line.length() > 0) { + lines.add(line); line = readLine(); } return lines; diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java index 4028ede..193431c 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultInputHandler.java @@ -25,6 +25,7 @@ */ import javax.annotation.PostConstruct; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -35,26 +36,19 @@ * @author Brett Porter * @version $Id$ */ -public class DefaultInputHandler - extends AbstractInputHandler -{ +public class DefaultInputHandler extends AbstractInputHandler { private BufferedReader consoleReader; - public String readLine() - throws IOException - { + public String readLine() throws IOException { return consoleReader.readLine(); } - public String readPassword() - throws IOException - { + public String readPassword() throws IOException { return consoleReader.readLine(); } @PostConstruct - public void initialize() - { - consoleReader = new BufferedReader( new InputStreamReader( System.in ) ); + public void initialize() { + consoleReader = new BufferedReader(new InputStreamReader(System.in)); } } diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java index c380807..d0b40ca 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultOutputHandler.java @@ -25,6 +25,7 @@ */ import javax.annotation.PostConstruct; + import java.io.IOException; import java.io.PrintWriter; @@ -34,27 +35,20 @@ * @author Brett Porter * @version $Id$ */ -public class DefaultOutputHandler - implements OutputHandler -{ +public class DefaultOutputHandler implements OutputHandler { private PrintWriter consoleWriter; @PostConstruct - public void initialize() - { - consoleWriter = new PrintWriter( System.out ); + public void initialize() { + consoleWriter = new PrintWriter(System.out); } - public void write( String line ) - throws IOException - { - consoleWriter.print( line ); + public void write(String line) throws IOException { + consoleWriter.print(line); consoleWriter.flush(); } - public void writeLine( String line ) - throws IOException - { + public void writeLine(String line) throws IOException { consoleWriter.println(); } } diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java index bbed767..a21c2f7 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/DefaultPrompter.java @@ -24,21 +24,19 @@ * SOFTWARE. */ -import org.codehaus.plexus.util.StringUtils; - import java.io.IOException; import java.util.Iterator; import java.util.List; +import org.codehaus.plexus.util.StringUtils; + /** * Default prompter. * * @author Brett Porter * @version $Id$ */ -public class DefaultPrompter - implements Prompter -{ +public class DefaultPrompter implements Prompter { /** * @requirement */ @@ -49,198 +47,141 @@ public class DefaultPrompter */ private InputHandler inputHandler; - public DefaultPrompter() - { + public DefaultPrompter() { super(); } - public DefaultPrompter( OutputHandler outputHandler, InputHandler inputHandler ) - { + public DefaultPrompter(OutputHandler outputHandler, InputHandler inputHandler) { this.outputHandler = outputHandler; this.inputHandler = inputHandler; } - public String prompt(String message ) - throws PrompterException - { - try - { - writePrompt( message ); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to present prompt", e ); + public String prompt(String message) throws PrompterException { + try { + writePrompt(message); + } catch (IOException e) { + throw new PrompterException("Failed to present prompt", e); } - try - { + try { return inputHandler.readLine(); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to read user response", e ); + } catch (IOException e) { + throw new PrompterException("Failed to read user response", e); } } - public String prompt( String message, String defaultReply ) - throws PrompterException - { - try - { - writePrompt( formatMessage( message, null, defaultReply ) ); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to present prompt", e ); + public String prompt(String message, String defaultReply) throws PrompterException { + try { + writePrompt(formatMessage(message, null, defaultReply)); + } catch (IOException e) { + throw new PrompterException("Failed to present prompt", e); } - try - { + try { String line = inputHandler.readLine(); - if ( StringUtils.isEmpty( line ) ) - { + if (StringUtils.isEmpty(line)) { line = defaultReply; } return line; - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to read user response", e ); + } catch (IOException e) { + throw new PrompterException("Failed to read user response", e); } } - public String prompt( String message, List possibleValues, String defaultReply ) - throws PrompterException - { - String formattedMessage = formatMessage( message, possibleValues, defaultReply ); + public String prompt(String message, List possibleValues, String defaultReply) throws PrompterException { + String formattedMessage = formatMessage(message, possibleValues, defaultReply); String line; - do - { - try - { - writePrompt( formattedMessage ); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to present prompt", e ); + do { + try { + writePrompt(formattedMessage); + } catch (IOException e) { + throw new PrompterException("Failed to present prompt", e); } - try - { + try { line = inputHandler.readLine(); if (line == null && defaultReply == null) { throw new IOException("EOF"); } - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to read user response", e ); + } catch (IOException e) { + throw new PrompterException("Failed to read user response", e); } - if ( StringUtils.isEmpty( line ) ) - { + if (StringUtils.isEmpty(line)) { line = defaultReply; } - if ( line != null && !possibleValues.contains( line ) ) - { - try - { - outputHandler.writeLine( "Invalid selection." ); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to present feedback", e ); + if (line != null && !possibleValues.contains(line)) { + try { + outputHandler.writeLine("Invalid selection."); + } catch (IOException e) { + throw new PrompterException("Failed to present feedback", e); } } - } - while ( line == null || !possibleValues.contains( line ) ); + } while (line == null || !possibleValues.contains(line)); return line; } - public String prompt( String message, List possibleValues ) - throws PrompterException - { - return prompt( message, possibleValues, null ); + public String prompt(String message, List possibleValues) throws PrompterException { + return prompt(message, possibleValues, null); } - public String promptForPassword( String message ) - throws PrompterException - { - try - { - writePrompt( message ); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to present prompt", e ); + public String promptForPassword(String message) throws PrompterException { + try { + writePrompt(message); + } catch (IOException e) { + throw new PrompterException("Failed to present prompt", e); } - try - { + try { return inputHandler.readPassword(); - } - catch ( IOException e ) - { - throw new PrompterException( "Failed to read user response", e ); + } catch (IOException e) { + throw new PrompterException("Failed to read user response", e); } } - private String formatMessage( String message, List possibleValues, String defaultReply ) - { - StringBuilder formatted = new StringBuilder( message.length() * 2 ); + private String formatMessage(String message, List possibleValues, String defaultReply) { + StringBuilder formatted = new StringBuilder(message.length() * 2); - formatted.append( message ); + formatted.append(message); - if ( possibleValues != null && !possibleValues.isEmpty() ) - { - formatted.append( " (" ); + if (possibleValues != null && !possibleValues.isEmpty()) { + formatted.append(" ("); - for ( Iterator it = possibleValues.iterator(); it.hasNext(); ) - { + for (Iterator it = possibleValues.iterator(); it.hasNext(); ) { String possibleValue = it.next(); - formatted.append( possibleValue ); + formatted.append(possibleValue); - if ( it.hasNext() ) - { - formatted.append( '/' ); + if (it.hasNext()) { + formatted.append('/'); } } - formatted.append( ')' ); + formatted.append(')'); } - if ( defaultReply != null ) - { - formatted.append( ' ' ).append( defaultReply ); + if (defaultReply != null) { + formatted.append(' ').append(defaultReply); } return formatted.toString(); } - private void writePrompt( String message ) - throws IOException - { - outputHandler.write( message + ": " ); + private void writePrompt(String message) throws IOException { + outputHandler.write(message + ": "); } - public void showMessage( String message ) - throws PrompterException - { - try - { - writePrompt( message ); + public void showMessage(String message) throws PrompterException { + try { + writePrompt(message); + } catch (IOException e) { + throw new PrompterException("Failed to present prompt", e); } - catch ( IOException e ) - { - throw new PrompterException( "Failed to present prompt", e ); - } - } } diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java index 14a0342..0cde1f1 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/InputHandler.java @@ -24,8 +24,8 @@ * SOFTWARE. */ -import java.util.List; import java.io.IOException; +import java.util.List; /** * Manage user input from different sources. @@ -36,8 +36,7 @@ * @author Brett Porter * @version $Id$ */ -public interface InputHandler -{ +public interface InputHandler { String ROLE = InputHandler.class.getName(); /** @@ -45,22 +44,19 @@ public interface InputHandler * If the input can be echoed, it will be. * @return the line read */ - String readLine() - throws IOException; + String readLine() throws IOException; /** * Read a single line of input, swalling the newline at the end. * This method guarantees input is not echoed. * @return the line read */ - String readPassword() - throws IOException; + String readPassword() throws IOException; /** * Read a set of lines. Equivalent to multiple calls to {@link #readLine()}. * Ends when an empty line is encountered. * @return a list of lines read */ - List readMultipleLines() - throws IOException; + List readMultipleLines() throws IOException; } diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java index 240a984..9b0c027 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/OutputHandler.java @@ -32,21 +32,18 @@ * @author Brett Porter * @version $Id$ */ -public interface OutputHandler -{ +public interface OutputHandler { String ROLE = OutputHandler.class.getName(); /** * Write a single line of input, excluding the newline at the end. * @param line the line */ - void write( String line ) - throws IOException; + void write(String line) throws IOException; /** * Write a single line of input, including the newline at the end. * @param line the line */ - void writeLine( String line ) - throws IOException; + void writeLine(String line) throws IOException; } diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java index bd9b37c..b5f6ffb 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/Prompter.java @@ -32,25 +32,18 @@ * @author Brett Porter * @version $Id$ */ -public interface Prompter -{ +public interface Prompter { String ROLE = Prompter.class.getName(); - String prompt( String message ) - throws PrompterException; + String prompt(String message) throws PrompterException; - String prompt( String message, String defaultReply ) - throws PrompterException; + String prompt(String message, String defaultReply) throws PrompterException; - String prompt( String message, List possibleValues ) - throws PrompterException; + String prompt(String message, List possibleValues) throws PrompterException; - String prompt( String message, List possibleValues, String defaultReply ) - throws PrompterException; + String prompt(String message, List possibleValues, String defaultReply) throws PrompterException; - String promptForPassword( String message ) - throws PrompterException; + String promptForPassword(String message) throws PrompterException; - void showMessage( String message ) - throws PrompterException; + void showMessage(String message) throws PrompterException; } diff --git a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java index d6a0b00..7385789 100644 --- a/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java +++ b/plexus-interactivity-api/src/main/java/org/codehaus/plexus/components/interactivity/PrompterException.java @@ -30,16 +30,12 @@ * @author Brett Porter * @version $Id$ */ -public class PrompterException - extends Exception -{ - public PrompterException( String message ) - { - super( message ); +public class PrompterException extends Exception { + public PrompterException(String message) { + super(message); } - public PrompterException( String message, Throwable cause ) - { - super( message, cause ); + public PrompterException(String message, Throwable cause) { + super(message, cause); } } diff --git a/plexus-interactivity-api/src/test/java/org/codehaus/plexus/components/interactivity/DefaultPrompterTest.java b/plexus-interactivity-api/src/test/java/org/codehaus/plexus/components/interactivity/DefaultPrompterTest.java index 12582f4..e261cd3 100644 --- a/plexus-interactivity-api/src/test/java/org/codehaus/plexus/components/interactivity/DefaultPrompterTest.java +++ b/plexus-interactivity-api/src/test/java/org/codehaus/plexus/components/interactivity/DefaultPrompterTest.java @@ -24,46 +24,43 @@ * SOFTWARE. */ -import org.junit.jupiter.api.Test; - import java.util.List; +import org.junit.jupiter.api.Test; + import static java.util.Arrays.asList; import static java.util.Objects.requireNonNull; import static org.junit.jupiter.api.Assertions.assertEquals; -public class DefaultPrompterTest -{ +public class DefaultPrompterTest { @Test void promptSimple() throws PrompterException { final InMemoryOutput out = new InMemoryOutput(); - final Prompter prompter = new DefaultPrompter( out, new InMemoryInput( "ok" ) ); - prompter.prompt( "test" ); - assertEquals( "test: ", out.builder.toString() ); + final Prompter prompter = new DefaultPrompter(out, new InMemoryInput("ok")); + prompter.prompt("test"); + assertEquals("test: ", out.builder.toString()); } @Test void promptOption() throws PrompterException { final InMemoryOutput out = new InMemoryOutput(); - final Prompter prompter = new DefaultPrompter( out, new InMemoryInput( "ok" ) ); - prompter.prompt( "test", "value" ); + final Prompter prompter = new DefaultPrompter(out, new InMemoryInput("ok")); + prompter.prompt("test", "value"); assertEquals("test value: ", out.builder.toString()); } @Test void promptOptions() throws PrompterException { final InMemoryOutput out = new InMemoryOutput(); - final Prompter prompter = new DefaultPrompter( out, new InMemoryInput( "yes" ) ); - prompter.prompt( "test", asList( "yes", "no" ), "value" ); + final Prompter prompter = new DefaultPrompter(out, new InMemoryInput("yes")); + prompter.prompt("test", asList("yes", "no"), "value"); assertEquals("test (yes/no) value: ", out.builder.toString()); } - private static class InMemoryInput implements InputHandler - { + private static class InMemoryInput implements InputHandler { private final String line; - private InMemoryInput( String line ) - { + private InMemoryInput(String line) { this.line = requireNonNull(line); } @@ -73,32 +70,27 @@ public String readLine() { } @Override - public String readPassword() - { + public String readPassword() { throw new UnsupportedOperationException(); } @Override - public List readMultipleLines() - { + public List readMultipleLines() { throw new UnsupportedOperationException(); } } - private static class InMemoryOutput implements OutputHandler - { + private static class InMemoryOutput implements OutputHandler { private final StringBuilder builder = new StringBuilder(); @Override - public void write( String line ) - { - builder.append( line ); + public void write(String line) { + builder.append(line); } @Override - public void writeLine( String line ) - { - builder.append( line ); + public void writeLine(String line) { + builder.append(line); } } } diff --git a/plexus-interactivity-jline/pom.xml b/plexus-interactivity-jline/pom.xml index 14b7a8b..9e5a30e 100644 --- a/plexus-interactivity-jline/pom.xml +++ b/plexus-interactivity-jline/pom.xml @@ -1,9 +1,10 @@ + 4.0.0 - plexus-interactivity org.codehaus.plexus + plexus-interactivity 1.2-SNAPSHOT @@ -31,4 +32,4 @@ 1.3.2 - \ No newline at end of file + diff --git a/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java b/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java index e5d3234..40ab3b1 100644 --- a/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java +++ b/plexus-interactivity-jline/src/main/java/org/codehaus/plexus/components/interactivity/jline/JLineInputHandler.java @@ -24,45 +24,36 @@ * SOFTWARE. */ -import jline.ConsoleReader; -import org.codehaus.plexus.components.interactivity.AbstractInputHandler; - import javax.annotation.PostConstruct; + import java.io.IOException; +import jline.ConsoleReader; +import org.codehaus.plexus.components.interactivity.AbstractInputHandler; + /** * Default input handler, that uses the console. * * @author Brett Porter * @version $Id$ */ -public class JLineInputHandler - extends AbstractInputHandler -{ +public class JLineInputHandler extends AbstractInputHandler { private ConsoleReader consoleReader; - public String readLine() - throws IOException - { + public String readLine() throws IOException { return consoleReader.readLine(); } - public String readPassword() - throws IOException - { - return consoleReader.readLine( new Character( '*' ) ); + public String readPassword() throws IOException { + return consoleReader.readLine(new Character('*')); } @PostConstruct - public void initialize() - { - try - { + public void initialize() { + try { consoleReader = new ConsoleReader(); - } - catch ( IOException e ) - { - throw new IllegalStateException( "Cannot create console reader: ", e ); + } catch (IOException e) { + throw new IllegalStateException("Cannot create console reader: ", e); } } } diff --git a/pom.xml b/pom.xml index c772b4c..9eb2bcc 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,10 @@ + 4.0.0 - plexus-components org.codehaus.plexus + plexus-components 14.2 @@ -13,6 +14,11 @@ Plexus Interactivity Handler Component + + plexus-interactivity-api + plexus-interactivity-jline + + ${scm.url} ${scm.url} @@ -34,11 +40,6 @@ 2021-09-11T07:20:06Z - - plexus-interactivity-api - plexus-interactivity-jline - -