Fix code format

This commit is contained in:
Manuel Thalmann 2022-10-11 13:17:58 +02:00
parent 3a3ecefed2
commit f6fe871d80
20 changed files with 141 additions and 93 deletions

View file

@ -5,7 +5,7 @@ package ch.nuth.zhaw.exbox;
*
*/
public class AnyServer implements CommandExecutor {
//----- Dies implementiert das CommandExecutor Interface.
// ----- Dies implementiert das CommandExecutor Interface.
@Override
public String execute(String command) {
return "Die Eingabe ist \"" + command + "\"\n";

View file

@ -10,4 +10,4 @@ public class App {
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
}

View file

@ -13,6 +13,7 @@ public class BracketServer implements CommandExecutor {
public BracketServer() {
super();
bracketPairs = new HashMap<>() {
{
put("(", ")");
@ -37,9 +38,8 @@ public class BracketServer implements CommandExecutor {
for (MatchResult result : results) {
if (bracketPairs.keySet().contains(result.group())) {
openBrackets.push(result.group());
} else if (
!openBrackets.isEmpty() &&
result.group().equals(bracketPairs.get(openBrackets.peek()))) {
} else if (!openBrackets.isEmpty() &&
result.group().equals(bracketPairs.get(openBrackets.peek()))) {
openBrackets.pop();
} else {
return false;

View file

@ -17,4 +17,4 @@ public interface CommandExecutor {
* @return Resultat, ueblicherweise eine oder mehrere Zeilen.
*/
String execute(String command) throws Exception;
}
}

View file

@ -10,7 +10,7 @@ public class Competitor implements Comparable<Competitor> {
private final String time;
private int rank;
public Competitor(int rank, String name, String time) {
public Competitor(int rank, String name, String time) {
this.rank = rank;
this.name = name;
this.time = time;
@ -32,17 +32,20 @@ public class Competitor implements Comparable<Competitor> {
return name;
}
private static long parseTime(String s) {
private static long parseTime(String s) {
try {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(s);
return date.getTime();
} catch (Exception e) {System.err.println(e);}
} catch (Exception e) {
System.err.println(e);
}
return 0;
}
public String toString() {
return ""+ rank + " "+name+" "+time;
return "" + rank + " " + name + " " + time;
}
@Override
@ -61,12 +64,11 @@ public class Competitor implements Comparable<Competitor> {
}
@Override
public boolean equals (Object o) {
public boolean equals(Object o) {
if (o instanceof Competitor) {
Competitor c = (Competitor)o;
Competitor c = (Competitor) o;
return compareTo(c) == 0;
}
else {
} else {
return false;
}
}
@ -86,8 +88,7 @@ class AlphaComparatorCompetitor implements Comparator<Competitor> {
if (result == 0) {
return Long.compare(o1.getTimeValue(), o2.getTimeValue());
}
else {
} else {
return result;
}
}

View file

@ -17,16 +17,16 @@ import java.util.Set;
/**
* @(#)ExBoxFrame.java
*
* JFC ExBox application
* JFC ExBox application
*
* @author K.Rege
* @version 1.00 2014/2/3
* @version 1.01 2016/8/2
* @version 2.00 2017/8/30 Test
* @version 2.01 2018/2/5 AutoscaleFaktor
* @version 2.02 2018/3/12 Reconnect (inspired by S. Kunz)
* @version 2.03 2021/7/24 Test (repeat)
* @version 2.04 2021/9/11 Test as plugin
* @version 1.00 2014/2/3
* @version 1.01 2016/8/2
* @version 2.00 2017/8/30 Test
* @version 2.01 2018/2/5 AutoscaleFaktor
* @version 2.02 2018/3/12 Reconnect (inspired by S. Kunz)
* @version 2.03 2021/7/24 Test (repeat)
* @version 2.04 2021/9/11 Test as plugin
*/
public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private final int UHDTHRESHOLD = 1920;
@ -48,9 +48,11 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
public void setFontSize(int size) {
Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
for (Object key : keySet) {
if (key != null && key.toString().toLowerCase().contains("font")) {
Font font = UIManager.getDefaults().getFont(key);
if (font != null) {
font = font.deriveFont((float) size);
UIManager.put(key, font);
@ -132,9 +134,11 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private Path getPathCompiled() {
try {
Path path = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
for (String part : getClass().getPackage().getName().split("\\.")) {
path = path.resolve(part);
}
return path;
} catch (URISyntaxException e) {
throw new RuntimeException(e);
@ -150,11 +154,11 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
} catch (Exception ex) {
ex.printStackTrace();
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double scaleFaktor = (screenSize.getWidth() <= UHDTHRESHOLD) ? 1 : 2;
setFontSize((int) (11 * scaleFaktor));
setSize(
new Dimension((int) (400 * scaleFaktor), (int) (400 * scaleFaktor)));
setSize(new Dimension((int) (400 * scaleFaktor), (int) (400 * scaleFaktor)));
setTitle("ExBox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
@ -174,14 +178,17 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
if (lastServer != null) {
command = ServerFactory.createServer(lastServer);
}
if (!arguments.getText().equals(history.getItemAt(0))
&& !arguments.getText().equals(history.getSelectedItem())) {
history.insertItemAt(arguments.getText(), 0);
}
if (command == null) {
error("no Server connected");
} else {
String res = command.execute(args);
if (graphicOn) {
graphic.setFigure(res);
} else {
@ -213,17 +220,20 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
}
private String openFileDialog(Path startDirectory, String pattern) {
FileDialog fd = new FileDialog(this, "Open");
if (pattern != null) fd.setFile(pattern);
if (startDirectory != null) fd.setDirectory(startDirectory.toString());
FileDialog fd = new FileDialog(this, "Open");
if (pattern != null)
fd.setFile(pattern);
if (startDirectory != null)
fd.setDirectory(startDirectory.toString());
fd.setVisible(true);
return fd.getDirectory() + fd.getFile();
return fd.getDirectory() + fd.getFile();
}
private void testCommand(boolean retest) throws Exception {
if (!retest) {
lastTestFile = openFileDialog(getPathCompiled(), "*test.class");
}
if (lastTestFile == null) {
output.append("ERROR no Test spezified\n");
} else if (unitTest != null) {
@ -236,29 +246,29 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
command = ServerFactory.createServer(name);
lastServer = name;
String fullClassName = command.getClass().getName();
String simpleClassName = fullClassName.substring(
fullClassName.lastIndexOf('.') + 1);
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
setTitle("ExBox connected to " + simpleClassName);
}
private void openFile() throws Exception {
private void openFile() throws Exception {
String name = openFileDialog(null, null);
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(name), STANDARDENCODING)))
{
new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) {
StringBuilder b = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
b.append(line);
b.append('\n');
}
execute(b.toString());
}
}
public void itemStateChanged(ItemEvent e) {
public void itemStateChanged(ItemEvent e) {
try {
arguments.setText((String) e.getItem());
execute(arguments.getText());
@ -267,7 +277,7 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
}
}
public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) {
try {
if ((e.getSource() == arguments) || (e.getSource() == enter)) {
execute(arguments.getText());
@ -293,4 +303,4 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
error(ex.toString());
}
}
}
}

View file

@ -19,9 +19,7 @@ public class ExBoxJUnit implements CommandExecutor {
public String execute(String testFile) throws Exception {
final List<String> successfulTests = new LinkedList<>();
final List<TestFailure> failedResults = new LinkedList<>();
StringBuilder output = new StringBuilder();
output.append("\nRUN TESTS ").append(new File(testFile).getName().split("\\.")[0]).append("\n");
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
@ -30,6 +28,7 @@ public class ExBoxJUnit implements CommandExecutor {
Launcher launcher = LauncherFactory.create();
launcher.discover(request);
launcher.registerTestExecutionListeners(new TestExecutionListener() {
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
@ -45,21 +44,27 @@ public class ExBoxJUnit implements CommandExecutor {
}
}
});
launcher.execute(request);
for (String testName : successfulTests) {
output.append(testName).append(": OK\n");
}
for (TestFailure result : failedResults) {
output.append(result.getName()).append(": ERROR\n");
String error = result.errorString();
if (!error.isEmpty()) {
output.append(error).append("\n");
}
}
boolean wasSuccessful = failedResults.isEmpty();
output.append("TESTS ").append(wasSuccessful ? "PASSED" : "FAILED").append(": ")
.append(wasSuccessful ? "OK \u263a" : failedResults.size() + " ERRORS").append("\n");
return output.toString();
}
@ -80,8 +85,9 @@ public class ExBoxJUnit implements CommandExecutor {
if (throwable == null) {
return "";
}
try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) {
PrintWriter printWriter = new PrintWriter(stringWriter)) {
throwable.printStackTrace(printWriter);
return stringWriter.toString();
} catch (IOException e) {

View file

@ -19,6 +19,7 @@ public class GraphicPanel extends JPanel {
int iy0 = (int) (h * y);
int ix1 = (int) (w * (x + width));
int iy1 = (int) (h * (y + height));
if (style.equals("draw")) {
g.drawRect(ix0, h - iy1, ix1 - ix0, iy1 - iy0);
} else {
@ -32,8 +33,10 @@ public class GraphicPanel extends JPanel {
int h = getHeight();
g.setColor(Color.black);
StringTokenizer tok = new StringTokenizer(figure, " <>=/,\"\n");
while (tok.hasMoreTokens()) {
String fig = tok.nextToken();
if (fig.equals("line")) {
tok.nextToken();
double x1 = Double.parseDouble(tok.nextToken());
@ -43,6 +46,7 @@ public class GraphicPanel extends JPanel {
double x2 = Double.parseDouble(tok.nextToken());
tok.nextToken();
double y2 = Double.parseDouble(tok.nextToken());
g.drawLine((int) (x1 * w), h - (int) (y1 * h),
(int) (x2 * w), h - (int) (y2 * h));
} else if (fig.equals("rect")) {

View file

@ -17,8 +17,7 @@ public class LCMServer implements CommandExecutor {
int y = Integer.parseInt(matcher.group(2));
int result = leastCommonMultiple(x, y);
return "Das Resultat ist %d\n".formatted(result);
}
else {
} else {
throw new Exception("Der angegebene Befehl \"%s\" ist ungültig!\n".formatted(command));
}
}
@ -31,7 +30,7 @@ public class LCMServer implements CommandExecutor {
while (y != 0) {
x %= y;
x ^= y;
y ^=x;
y ^= x;
x ^= y;
}

View file

@ -12,8 +12,7 @@ public class ListStack implements Stack {
if (innerList.add(x)) {
return;
}
}
catch (Exception e) {
} catch (Exception e) {
throw new StackOverflowError();
}
}
@ -53,5 +52,5 @@ public class ListStack implements Stack {
public boolean isFull() {
return innerList.size() == Integer.MIN_VALUE;
}
}

View file

@ -192,10 +192,10 @@ public class MyList extends AbstractList<Object> {
* Inserts the specified `item` after the specified `node`.
*
* @param node
* The node to insert the `item` after.
* The node to insert the `item` after.
*
* @param item
* The item to insert.
* The item to insert.
*/
protected void insertAfter(IListNode node, Object item) {
IListNode newNode = new ListNode(item, node.getNextNode(), node);

View file

@ -23,10 +23,10 @@ public class RankingListServer implements CommandExecutor {
String[] fields = currentLine.split(Delimiter);
result.add(
new Competitor(
0,
fields[0],
fields[1]));
new Competitor(
0,
fields[0],
fields[1]));
}
return result;
@ -56,16 +56,16 @@ public class RankingListServer implements CommandExecutor {
}
return String.join(
System.lineSeparator(),
competitors.stream().map(
(competitor) -> competitor.toString()).collect(Collectors.toList()));
System.lineSeparator(),
competitors.stream().map(
(competitor) -> competitor.toString()).collect(Collectors.toList()));
}
public String createNameList(List<Competitor> competitorList) {
return String.join(
System.lineSeparator(),
competitorList.stream().sorted(new AlphaComparatorCompetitor()).map(
(competitor) -> competitor.toString()).collect(Collectors.toList()));
System.lineSeparator(),
competitorList.stream().sorted(new AlphaComparatorCompetitor()).map(
(competitor) -> competitor.toString()).collect(Collectors.toList()));
}
public String execute(String rankingList) {

View file

@ -16,7 +16,8 @@ class MyClassLoader extends ClassLoader {
/**
* @param name filename of class
* return content of file as array of bytes; if file does not exist return null
* return content of file as array of bytes; if file does not exist
* return null
*/
private byte[] getBytes(String name) {
try {
@ -38,15 +39,19 @@ class MyClassLoader extends ClassLoader {
System.out.println("load:" + name + " " + resolve);
Class<?> clazz;
byte[] classData = getBytes(name);
if (classData != null) {
clazz = defineClass(null, classData, 0, classData.length);
path = name.substring(0,
name.length() - clazz.getName().length() - ".class".length());
return clazz;
}
if (!resolve) {
classData = getBytes(
path + name.replace(".", File.separator) + ".class");
if (classData != null) {
return defineClass(null, classData, 0, classData.length);
}
@ -67,10 +72,11 @@ public class ServerFactory {
public static Class<?> loadClass(String name) throws Exception {
MyClassLoader myClassLoader = new MyClassLoader(
MyClassLoader.class.getClassLoader());
return myClassLoader.loadClass(name, true);
}
public static CommandExecutor createServer(String name) throws Exception {
return (CommandExecutor) loadClass(name).getConstructor(new Class[]{}).newInstance();
return (CommandExecutor) loadClass(name).getConstructor(new Class[] {}).newInstance();
}
}
}

View file

@ -46,4 +46,4 @@ public interface Stack {
* @return Gibt true zurück, falls der Stack voll ist.
*/
boolean isFull();
}
}

View file

@ -75,9 +75,11 @@ public class ADS2_3_test {
public void testMixed() {
list.clear();
List<Character> list2 = new LinkedList<>();
for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random() * 26));
int op = (int) (Math.random() * 2);
switch (op) {
case 0:
list.add(c);
@ -89,7 +91,9 @@ public class ADS2_3_test {
break;
}
}
assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) {
char c1 = (char) list.get(i);
char c2 = (char) list2.get(i);

View file

@ -55,9 +55,11 @@ public class ADS2_4_test {
@Test
public void testMixed() {
List<Character> list2 = new LinkedList<>();
for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random()*26));
int op = (int)(Math.random()*2);
Character c = (char) ('A' + (Math.random() * 26));
int op = (int) (Math.random() * 2);
switch (op) {
case 0:
list.add(c);
@ -69,11 +71,13 @@ public class ADS2_4_test {
break;
}
}
Collections.sort(list2);
assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) {
char c1 = (char)list.get(i);
char c2 = (char)list2.get(i);
char c1 = (char) list.get(i);
char c2 = (char) list2.get(i);
assertEquals(c1, c2);
}
}

View file

@ -18,17 +18,17 @@ public class ADS3_1_test {
@Test
public void testEquals() {
assertTrue(c3.equals(c3), c3.getTime() + " == "+c3.getTime());
assertTrue(!c3.equals(c2), c3.getTime() + " != "+c2.getTime());
assertTrue(c3.equals(c3), c3.getTime() + " == " + c3.getTime());
assertTrue(!c3.equals(c2), c3.getTime() + " != " + c2.getTime());
assertTrue(!c3.equals(null), "equals(null)");
assertTrue(!c3.equals(new Object()), "equals(new Object())");
}
@Test
public void testCompare() {
assertTrue(c1.compareTo(c2) > 0, c1.getName() +" "+c1.getTime() + " > "+c2.getTime());
assertTrue(c2.compareTo(c1) < 0, c2.getName() +" "+c2.getTime() + " < "+c1.getTime());
assertTrue(c3.compareTo(c3) == 0, c3.getName() +" "+c3.getTime() + " == "+c3.getTime());
assertTrue(c4.compareTo(c2) < 0, c4.getName() +" "+c4.getTime() + " < "+c2.getTime());
assertTrue(c1.compareTo(c2) > 0, c1.getName() + " " + c1.getTime() + " > " + c2.getTime());
assertTrue(c2.compareTo(c1) < 0, c2.getName() + " " + c2.getTime() + " < " + c1.getTime());
assertTrue(c3.compareTo(c3) == 0, c3.getName() + " " + c3.getTime() + " == " + c3.getTime());
assertTrue(c4.compareTo(c2) < 0, c4.getName() + " " + c4.getTime() + " < " + c2.getTime());
}
}

View file

@ -18,7 +18,7 @@ public class ADS3_2_test {
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time));
competitorList.add(new Competitor(0, name, time));
}
return competitorList;
}
@ -35,6 +35,7 @@ public class ADS3_2_test {
"Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n";
rankGood = createList(rangliste);
rankTest = new RankingListServer().createList(rangliste);
}
@ -42,8 +43,9 @@ public class ADS3_2_test {
@Test
public void testCreateList() {
assertEquals(rankGood.size(), rankTest.size(), "length");
for (int i = 0; i < rankGood.size();i++) {
assertEquals(rankGood.get(i).toString(), rankTest.get(i).toString(), "rangliste["+i+"]");
for (int i = 0; i < rankGood.size(); i++) {
assertEquals(rankGood.get(i).toString(), rankTest.get(i).toString(), "rangliste[" + i + "]");
}
}
}

View file

@ -18,11 +18,13 @@ public class ADS3_3_test {
public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n");
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time));
competitorList.add(new Competitor(0, name, time));
}
return competitorList;
}
@ -30,10 +32,12 @@ public class ADS3_3_test {
Collections.sort(competitorList);
int rank = 1;
StringBuilder sb = new StringBuilder();
for (Competitor c : competitorList) {
c.setRank(rank++);
sb.append(c).append("\n");
}
return sb.toString();
}
@ -49,11 +53,13 @@ public class ADS3_3_test {
"Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n";
rankGood = createList(rangliste);
textGood = createSortedText(rankGood);
rankTest = new RankingListServer().createList(rangliste);
textTest = new RankingListServer().createSortedText(rankTest);
}
private String clean(String s) {
return s.trim();
}
@ -63,8 +69,9 @@ public class ADS3_3_test {
String[] good = textGood.split("\n");
String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste["+i+"]");
for (int i = 0; i < good.length; i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste[" + i + "]");
}
}
}

View file

@ -17,35 +17,39 @@ public class ADS3_4_test {
public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n");
for (String line : lines) {
String name = line.split(";")[0];
String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time));
competitorList.add(new Competitor(0, name, time));
}
return competitorList;
}
public String createSortedText(List<Competitor> competitorList) {
StringBuilder sb = new StringBuilder();
for (Competitor c : competitorList) {
sb.append(c).append("\n");
}
return sb.toString();
}
@BeforeEach
public void setUp() {
String ranglisteOrdered =
"Ancay Tarcis;02:20:02\n" +
"Elmer Beat;02:33:53\n" +
"Kiptum Daniel;02:11:31\n" +
"Kreibuhl Christian;02:21:47\n" +
"Kuehni Martin;02:33:36\n" +
"Marti Adrian;02:30:09\n" +
"Menzi Christoph;02:27:26\n" +
"Mueller Stefan;02:31:14\n" +
"Oliver Ruben;02:32:12\n" +
"Ott Michael;02:33:48\n";
String ranglisteOrdered = "Ancay Tarcis;02:20:02\n" +
"Elmer Beat;02:33:53\n" +
"Kiptum Daniel;02:11:31\n" +
"Kreibuhl Christian;02:21:47\n" +
"Kuehni Martin;02:33:36\n" +
"Marti Adrian;02:30:09\n" +
"Menzi Christoph;02:27:26\n" +
"Mueller Stefan;02:31:14\n" +
"Oliver Ruben;02:32:12\n" +
"Ott Michael;02:33:48\n";
String ranglisteUnordered = "Mueller Stefan;02:31:14\n" +
"Marti Adrian;02:30:09\n" +
"Kiptum Daniel;02:11:31\n" +
@ -56,6 +60,7 @@ public class ADS3_4_test {
"Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n";
rankGood = createList(ranglisteOrdered);
textGood = createSortedText(rankGood);
rankTest = createList(ranglisteUnordered);
@ -71,8 +76,9 @@ public class ADS3_4_test {
String[] good = textGood.split("\n");
String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste["+i+"]");
for (int i = 0; i < good.length; i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste[" + i + "]");
}
}
}