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

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

View file

@ -37,12 +37,15 @@ public class Competitor implements Comparable<Competitor> {
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

@ -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 {
@ -214,8 +221,10 @@ 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());
if (pattern != null)
fd.setFile(pattern);
if (startDirectory != null)
fd.setDirectory(startDirectory.toString());
fd.setVisible(true);
return fd.getDirectory() + fd.getFile();
}
@ -224,6 +233,7 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
if (!retest) {
lastTestFile = openFileDialog(getPathCompiled(), "*test.class");
}
if (lastTestFile == null) {
output.append("ERROR no Test spezified\n");
} else if (unitTest != null) {
@ -236,8 +246,7 @@ 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);
}
@ -246,14 +255,15 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
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());
}
}

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,6 +85,7 @@ public class ExBoxJUnit implements CommandExecutor {
if (throwable == null) {
return "";
}
try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) {
throwable.printStackTrace(printWriter);

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();
}
}

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

@ -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

@ -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));
}
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,26 +17,29 @@ 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));
}
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" +
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" +
@ -46,6 +49,7 @@ public class ADS3_4_test {
"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 + "]");
}
}
}