initial commit

This commit is contained in:
schrom01
2022-11-15 10:20:07 +01:00
commit 69fb83dd74
20 changed files with 4628 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
package ch.zhaw.ads;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS9_2_test {
MyCompetitor c1 = new MyCompetitor(1, "Kiptum Daniel", "02:11:31");
MyCompetitor c2 = new MyCompetitor(1, "Kiptum Daniel", "02:11:31");
MyCompetitor c3 = new MyCompetitor(2, "Ancay Tarcis", "02:20:02");
@Test
public void testEquals() {
assertEquals(c1, c2);
assertNotEquals(c1, c3);
}
@Test
public void testCompareTo() {
assertEquals(0, c1.compareTo(c2));
assertNotEquals(0, c1.compareTo(c3));
}
@Test
public void testHashcode() {
assertEquals(c1.hashCode(), c2.hashCode());
assertNotEquals(c1.hashCode(), c3.hashCode());
}
}
+144
View File
@@ -0,0 +1,144 @@
package ch.zhaw.ads;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Town {
int hashCode;
String name;
String nb;
Town (int hashCode, String name, String nb) {
this.name = name; this.hashCode = hashCode; this.nb = nb;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Town)) return false;
return ((Town)o).name.equals(this.name);
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public String toString() {
return name + " " + hashCode;
}
}
public class ADS9_3_test {
Map<Town, Town> hashmap;
List<Town> towns;
@BeforeEach
public void setUp() {
towns = List.of(
new Town(5, "Bari", "BA"),
new Town(8, "Bologna", "BO"),
new Town(3, "Catania", "CA"),
new Town(9, "Firenze", "FI"),
new Town(0, "Genova", "GV"),
new Town(12, "Milano", "MI"),
new Town(7, "Napoli", "NA"),
new Town(7, "Palermo", "PA"),
new Town(7, "Roma", "RM"),
new Town(5, "Torino", "TO")
);
hashmap = new MyHashtable<>(100);
}
@Test
public void testAdd() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd2() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.put(t1, t1);
Town t2 = hashmap.get(t0);
assertEquals(t0, t2);
t2 = hashmap.get(t1);
assertEquals(t1, t2);
}
@Test
public void testAdd3() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.remove(t0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd4() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
}
@Test
public void testSize() {
hashmap.clear();
assertEquals(0, hashmap.size());
testAdd2();
assertEquals(2, hashmap.size());
}
@Test
public void testRemove() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.remove(t0);
assertEquals(0, hashmap.size());
hashmap.put(t0, t0);
hashmap.remove(t1);
assertEquals(1, hashmap.size());
hashmap.remove(t0);
assertEquals(0, hashmap.size());
}
@Test
public void testMixed() {
hashmap.clear();
Map<Town, Town> hashmap2 = new HashMap<>();
for (int i = 0; i < 10000; i++) {
Town c = towns.get((int)(Math.random() * towns.size()));
int op = (int)(Math.random() * 2);
switch (op) {
case 0: hashmap.put(c, c); hashmap2.put(c, c); break;
case 1: hashmap.remove(c); hashmap2.remove(c); break;
}
}
assertEquals(hashmap2.size(), hashmap.size());
for (Town t : towns) {
Town c1 = hashmap.get(t);
Town c2 = hashmap2.get(t);
assertEquals(c1, c2);
}
}
}
+119
View File
@@ -0,0 +1,119 @@
package ch.zhaw.ads;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ADS9_4_test {
Map<Town, Town> hashmap;
List<Town> towns;
@BeforeEach
public void setUp() {
towns = List.of(
new Town(5, "Bari", "BA"),
new Town(8, "Bologna", "BO"),
new Town(3, "Catania", "CA"),
new Town(9, "Firenze", "FI"),
new Town(0, "Genova", "GV"),
new Town(12, "Milano", "MI"),
new Town(7, "Napoli", "NA"),
new Town(7, "Palermo", "PA"),
new Town(7, "Roma", "RM"),
new Town(5, "Torino", "TO")
);
hashmap = new MyHashtable<>(2);
}
@Test
public void testAdd() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd2() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.put(t1, t1);
Town t2 = hashmap.get(t0);
assertEquals(t0, t2);
t2 = hashmap.get(t1);
assertEquals(t1, t2);
}
@Test
public void testAdd3() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.remove(t0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
Town t1 = hashmap.get(t0);
assertEquals(t0, t1);
}
@Test
public void testAdd4() {
hashmap.clear();
Town t0 = towns.get(0);
hashmap.put(t0, t0);
hashmap.put(t0, t0);
assertEquals(1, hashmap.size());
}
@Test
public void testSize() {
hashmap.clear();
assertEquals(0, hashmap.size());
testAdd2();
assertEquals(2, hashmap.size());
}
@Test
public void testRemove() {
hashmap.clear();
Town t0 = towns.get(0);
Town t1 = towns.get(1);
hashmap.put(t0, t0);
hashmap.remove(t0);
assertEquals(0, hashmap.size());
hashmap.put(t0, t0);
hashmap.remove(t1);
assertEquals(1, hashmap.size());
hashmap.remove(t0);
assertEquals(0, hashmap.size());
}
@Test
public void testMixed() {
hashmap.clear();
Map<Town, Town> hashmap2 = new HashMap<>();
for (int i = 0; i < 1000; i++) {
Town c = towns.get((int)(Math.random() * towns.size()));
int op = (int)(Math.random() * 2);
switch (op) {
case 0: hashmap.put(c, c); hashmap2.put(c, c); break;
case 1: hashmap.remove(c); hashmap2.remove(c); break;
}
}
assertEquals(hashmap2.size(), hashmap.size());
for (Town t : towns) {
Town c1 = hashmap.get(t);
Town c2 = hashmap2.get(t);
assertEquals(c1, c2);
}
}
}
+15
View File
@@ -0,0 +1,15 @@
package ch.zhaw.ads;
/**
* AnyServer -- Praktikum Experimentierkasten -- ADS
*
* @author E. Mumprecht
* @version 1.0 -- Geruest fuer irgendeinen Server
*/
public class AnyServer implements CommandExecutor {
//----- Dies implementiert das CommandExecutor Interface.
@Override
public String execute(String command) {
return "Die Eingabe ist \"" + command + "\"\n";
}
}
+20
View File
@@ -0,0 +1,20 @@
package ch.zhaw.ads;
/**
* CommandExecutor -- Praktikum Experimentierkasten -- SW3 Dieses Interface muss
* von jedem Server implementiert werden.
*
* @author E. Mumprecht
* @version 1.0 -- Geruest fuer irgendeinen Server
* @version 1.1 -- K. Rege Fehlerueckgabe hinzugefuegt
*/
public interface CommandExecutor {
/**
* execute -- nimmt eine Kommandozeile, tut irgendetwas gescheites, und
* berichtet das Resultat.
*
* @param command Kommandozeile
* @return Resultat, ueblicherweise eine oder mehrere Zeilen.
*/
String execute(String command) throws Exception;
}
+13
View File
@@ -0,0 +1,13 @@
package ch.zhaw.ads;
/**
* @author K. Rege
* @version 1.0 -- Experimentierkasten
*/
public class ExBox {
public static void main(String[] args) {
ExBoxFrame f = new ExBoxFrame();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
+294
View File
@@ -0,0 +1,294 @@
package ch.zhaw.ads;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
/**
* @(#)ExBoxFrame.java
*
* 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
*/
public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private final int UHDTHRESHOLD = 1920;
private final String STANDARDENCODING = "ISO-8859-1";
private JMenuItem connect, exit, open, test, retest, textView, graphicView, clear;
private JMenu menuServer;
private JButton enter;
private JTextField arguments;
private JComboBox<String> history;
private JTextArea output;
private JScrollPane scrollPane;
private CommandExecutor command;
private CommandExecutor unitTest;
private boolean graphicOn;
private GraphicPanel graphic;
private String lastServer;
private String lastTestFile;
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);
}
}
}
}
private void initMenu() {
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);
open = new JMenuItem("Open...");
open.addActionListener(this);
menuFile.add(open);
exit = new JMenuItem();
exit.setText("Exit");
exit.addActionListener(this);
menuFile.add(exit);
menuServer = new JMenu("Server");
menuBar.add(menuServer);
connect = new JMenuItem("Connect ...");
connect.addActionListener(this);
menuServer.add(connect);
JMenu menuView = new JMenu("View");
menuBar.add(menuView);
clear = new JMenuItem("Clear");
clear.addActionListener(this);
menuView.add(clear);
textView = new JMenuItem("Text");
textView.addActionListener(this);
menuView.add(textView);
graphicView = new JMenuItem("Graphic");
graphicView.addActionListener(this);
menuView.add(graphicView);
}
private void initJUnit() {
try {
unitTest = ServerFactory.createServer(getPathCompiled().resolve("ExBoxJUnit.class").toString());
test = new JMenuItem("Test ...");
test.addActionListener(this);
menuServer.add(test);
retest = new JMenuItem("Test");
retest.addActionListener(this);
menuServer.add(retest);
} catch (Exception e) {
warning("Test Plugin not found\n");
}
}
private void initComponents() {
setLayout(new BorderLayout());
output = new JTextArea();
scrollPane = new JScrollPane(output);
add(BorderLayout.CENTER, scrollPane);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
arguments = new JTextField();
arguments.addActionListener(this);
panel.add(BorderLayout.CENTER, arguments);
enter = new JButton("enter");
enter.addActionListener(this);
panel.add(BorderLayout.EAST, enter);
history = new JComboBox<>();
history.addItemListener(this);
panel.add(BorderLayout.SOUTH, history);
add(BorderLayout.SOUTH, panel);
}
/**
* get default path for file open dialog
*/
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);
}
}
/**
* The constructor
*/
public ExBoxFrame() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 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)));
setTitle("ExBox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
initMenu();
initJUnit();
}
private void warning(String s) {
System.err.println("\nWARNING: " + s + "\n");
}
private void error(String s) {
output.append("\nERROR: " + s + "\n");
}
private void execute(String args) throws Exception {
/* 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 {
output.append(res);
}
}
}
private void setGraphicView() {
if (!graphicOn) {
remove(scrollPane);
graphic = new GraphicPanel();
output.removeNotify();
add(BorderLayout.CENTER, graphic);
graphicOn = true;
validate();
repaint();
}
}
private void setTextView() {
if (graphicOn) {
remove(graphic);
add(BorderLayout.CENTER, scrollPane);
graphicOn = false;
validate();
repaint();
}
}
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());
fd.setVisible(true);
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) {
output.append(unitTest.execute(lastTestFile));
}
}
private void connectCommand() throws Exception {
String name = openFileDialog(getPathCompiled(), "*Server.class");
command = ServerFactory.createServer(name);
lastServer = name;
String fullClassName = command.getClass().getName();
String simpleClassName = fullClassName.substring(
fullClassName.lastIndexOf('.') + 1);
setTitle("ExBox connected to " + simpleClassName);
}
private void openFile() throws Exception {
String name = openFileDialog(null, null);
BufferedReader br = new BufferedReader(
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) {
try {
arguments.setText((String) e.getItem());
execute(arguments.getText());
} catch (Throwable ex) {
error(ex.toString());
}
}
public void actionPerformed(ActionEvent e) {
try {
if ((e.getSource() == arguments) || (e.getSource() == enter)) {
execute(arguments.getText());
} else if (e.getSource() == connect) {
connectCommand();
} else if (e.getSource() == test) {
testCommand(false);
} else if (e.getSource() == retest) {
testCommand(true);
} else if (e.getSource() == open) {
openFile();
} else if (e.getSource() == textView) {
setTextView();
} else if (e.getSource() == graphicView) {
setGraphicView();
} else if (e.getSource() == clear) {
output.setText("");
} else if (e.getSource() == exit) {
System.exit(0);
}
} catch (Throwable ex) {
ex.printStackTrace();
error(ex.toString());
}
}
}
+92
View File
@@ -0,0 +1,92 @@
package ch.zhaw.ads;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
public class ExBoxJUnit implements CommandExecutor {
@Override
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()
.selectors(DiscoverySelectors.selectClass(ServerFactory.loadClass(testFile)))
.build();
Launcher launcher = LauncherFactory.create();
launcher.discover(request);
launcher.registerTestExecutionListeners(new TestExecutionListener() {
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
if (testIdentifier.getType() != TestDescriptor.Type.TEST) {
return;
}
if (testExecutionResult.getStatus() == TestExecutionResult.Status.SUCCESSFUL) {
successfulTests.add(testIdentifier.getDisplayName());
} else {
failedResults.add(new TestFailure(testIdentifier.getDisplayName(),
testExecutionResult.getThrowable().orElse(null)));
}
}
});
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();
}
private static class TestFailure {
private final String name;
private final Throwable throwable;
TestFailure(String name, Throwable throwable) {
this.name = name;
this.throwable = throwable;
}
public String getName() {
return name;
}
public String errorString() {
if (throwable == null) {
return "";
}
try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) {
throwable.printStackTrace(printWriter);
return stringWriter.toString();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
+84
View File
@@ -0,0 +1,84 @@
package ch.zhaw.ads;
import javax.swing.*;
import java.awt.*;
import java.util.StringTokenizer;
public class GraphicPanel extends JPanel {
String figure;
public void setFigure(String figure) {
this.figure = figure;
paint(getGraphics());
}
private void drawRect(Graphics g, double x, double y, double width, double height, String style) {
int w = getWidth();
int h = getHeight();
int ix0 = (int) (w * x);
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 {
g.fillRect(ix0, h - iy1, ix1 - ix0, iy1 - iy0);
}
}
private void drawFigure(Graphics g) {
if (figure != null) {
int w = getWidth();
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());
tok.nextToken();
double y1 = Double.parseDouble(tok.nextToken());
tok.nextToken();
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")) {
tok.nextToken();
double x = Double.parseDouble(tok.nextToken());
tok.nextToken();
double y = Double.parseDouble(tok.nextToken());
tok.nextToken();
double width = Double.parseDouble(tok.nextToken());
tok.nextToken();
double height = Double.parseDouble(tok.nextToken());
tok.nextToken();
String style = tok.nextToken();
drawRect(g, x, y, width, height, style);
} else if (fig.equals("color")) {
tok.nextToken();
int red = Integer.parseInt(tok.nextToken());
tok.nextToken();
int green = Integer.parseInt(tok.nextToken());
tok.nextToken();
int blue = Integer.parseInt(tok.nextToken());
g.setColor(new Color(red, green, blue));
}
}
}
}
private void clear(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(new Color(240, 240, 240));
g.fillRect(0, 0, w, h);
}
public void paint(Graphics g) {
clear(g);
drawFigure(g);
}
}
+45
View File
@@ -0,0 +1,45 @@
package ch.zhaw.ads;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
public class HashServer implements CommandExecutor {
private final static int STARTNR = 0;
private final static int NAME = 1;
private final static int TIME = 2;
private final Map<MyCompetitor, MyCompetitor> data = new MyHashtable<>(4000);
private long parseTime(String s) throws Exception {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
Date date = sdf.parse(s);
return date.getTime();
}
public void load(Map<MyCompetitor, MyCompetitor> data, String list) {
String[] lines = list.split("\n");
for (String line : lines) {
String[] items = line.split(";");
MyCompetitor c = new MyCompetitor(Integer.parseInt(items[STARTNR]), // startNr
items[NAME], // name
items[TIME]); // time
data.put(c, c);
}
}
public String execute(String arg) {
if (arg.toUpperCase().startsWith("GET")) {
String[] items = arg.substring(3).trim().split(";");
MyCompetitor key = new MyCompetitor(Integer.parseInt(items[1]), items[0], "00:00:00.0");
MyCompetitor o = data.get(key);
if (o != null) {
return items[0] + " " + items[1] + " -> " + o + "\n";
}
return "not found\n";
}
load(data, arg);
return "" + data.size() + " loaded\n";
}
}
+65
View File
@@ -0,0 +1,65 @@
package ch.zhaw.ads;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyCompetitor implements Comparable<MyCompetitor> {
private final String name;
private final String time;
private int rank;
public MyCompetitor(int rank, String name, String time) {
this.rank = rank;
this.name = name;
this.time = time;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getTime() {
return time;
}
public String getName() {
return name;
}
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);}
return 0;
}
private static String timeToString(int time) {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(new Date(time));
}
public String toString() {
return ""+ rank + " "+name+" "+time;
}
@Override
public int compareTo(MyCompetitor o) {
// to be done
return -1;
}
@Override
public int hashCode() {
// to be done
return -1;
}
@Override
public boolean equals (Object o) {
// to be done
return false;
}
}
+96
View File
@@ -0,0 +1,96 @@
package ch.zhaw.ads;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
@SuppressWarnings("unchecked")
public class MyHashtable<K, V> implements Map<K, V> {
private K[] keys = (K[]) new Object[10];
private V[] values = (V[]) new Object[10];
private int hash(Object k) {
int h = Math.abs(k.hashCode());
return h % keys.length;
}
public MyHashtable(int size) {
// to be done
}
// Removes all mappings from this map (optional operation).
public void clear() {
// to be done
throw new UnsupportedOperationException();
}
// Associates the specified value with the specified key in this map (optional operation).
public V put(K key, V value) {
// to be done
throw new UnsupportedOperationException();
}
// Returns the value to which this map maps the specified key.
public V get(Object key) {
// to be done
throw new UnsupportedOperationException();
}
// Removes the mapping for this key from this map if present (optional operation).
public V remove(Object key) {
// to be done (Aufgabe 3)
throw new UnsupportedOperationException();
}
// Returns the number of key-value mappings in this map.
public int size() {
// to be done
throw new UnsupportedOperationException();
}
// UnsupportedOperationException ===================================================================
// Returns a collection view of the values contained in this map.
public Collection<V> values() {
throw new UnsupportedOperationException();
}
// Returns true if this map contains a mapping for the specified key.
public boolean containsKey(Object key) {
throw new UnsupportedOperationException();
}
// Returns true if this map maps one or more keys to the specified value.
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}
// Returns a set view of the mappings contained in this map.
public Set<Map.Entry<K, V>> entrySet() {
throw new UnsupportedOperationException();
}
// Compares the specified object with this map for equality.
public boolean equals(Object o) {
throw new UnsupportedOperationException();
}
// Copies all of the mappings from the specified map to this map (optional operation).
public void putAll(Map<? extends K, ? extends V> t) {
throw new UnsupportedOperationException();
}
// Returns the hash code value for this map.
public int hashCode() {
throw new UnsupportedOperationException();
}
// Returns true if this map contains no key-value mappings.
public boolean isEmpty() {
throw new UnsupportedOperationException();
}
// Returns a set view of the keys contained in this map.
public Set<K> keySet() {
throw new UnsupportedOperationException();
}
}
File diff suppressed because it is too large Load Diff
+76
View File
@@ -0,0 +1,76 @@
package ch.zhaw.ads;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Classloader that handles file path of class
*/
class MyClassLoader extends ClassLoader {
private String path;
MyClassLoader(ClassLoader parent) {
super(parent);
}
/**
* @param name filename of class
* return content of file as array of bytes; if file does not exist return null
*/
private byte[] getBytes(String name) {
try {
System.out.println(name);
RandomAccessFile file = new RandomAccessFile(name, "r");
byte[] data = new byte[(int) file.length()];
file.readFully(data);
file.close();
return data;
} catch (IOException e) {
}
return null;
}
/**
* @param name filename of class
*/
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
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);
}
}
return findSystemClass(name);
}
}
/**
* ServerFactory -- Praktikum Experimentierkasten --
*
* @author K. Rege
* @version 1.0 -- Factory zur Erstellung von Server Objekten
* @version 2.0 -- Dynamisches Nachladen
* @version 2.01 -- Fix deprecated Functions
*/
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();
}
}