Compare commits
3 commits
5e93294c38
...
11983e0f08
Author | SHA1 | Date | |
---|---|---|---|
11983e0f08 | |||
aaea1a9d87 | |||
ce65993bee |
2 changed files with 373 additions and 329 deletions
|
@ -1,314 +1,325 @@
|
|||
package ch.nuth.zhaw.exbox;
|
||||
|
||||
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 Dimension UHDTHRESHOLD = new Dimension(2000, 1500);
|
||||
private final String STANDARDENCODING = "UTF-8";
|
||||
|
||||
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 scale = (
|
||||
screenSize.getWidth() > UHDTHRESHOLD.getWidth() &&
|
||||
screenSize.getHeight() > UHDTHRESHOLD.getHeight()) ? 2 : 1;
|
||||
|
||||
scale = Math.max(scale, Toolkit.getDefaultToolkit().getScreenResolution() / 96.0);
|
||||
setFontSize((int) (11 * scale));
|
||||
|
||||
setSize(
|
||||
new Dimension((int)(400 * scale), (int)(400 * scale)));
|
||||
|
||||
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);
|
||||
|
||||
try (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());
|
||||
}
|
||||
}
|
||||
}
|
||||
package ch.nuth.zhaw.exbox;
|
||||
|
||||
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 Dimension UHDTHRESHOLD = new Dimension(2000, 1500);
|
||||
private final String STANDARDENCODING = "UTF-8";
|
||||
|
||||
private JMenuItem connect, reset, 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);
|
||||
reset = new JMenuItem("Reset Server...");
|
||||
reset.addActionListener(this);
|
||||
menuServer.add(reset);
|
||||
|
||||
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 scale = (
|
||||
screenSize.getWidth() > UHDTHRESHOLD.getWidth() &&
|
||||
screenSize.getHeight() > UHDTHRESHOLD.getHeight()) ? 2 : 1;
|
||||
|
||||
scale = Math.max(scale, Toolkit.getDefaultToolkit().getScreenResolution() / 96.0);
|
||||
setFontSize((int) (11 * scale));
|
||||
|
||||
setSize(
|
||||
new Dimension((int)(400 * scale), (int)(400 * scale)));
|
||||
|
||||
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 (!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");
|
||||
loadServer(name);
|
||||
lastServer = name;
|
||||
String fullClassName = command.getClass().getName();
|
||||
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
|
||||
setTitle("ExBox connected to " + simpleClassName);
|
||||
|
||||
}
|
||||
|
||||
private void resetCommand() throws Exception {
|
||||
if (lastServer != null) {
|
||||
loadServer(lastServer);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadServer(String name) throws Exception {
|
||||
command = ServerFactory.createServer(name);
|
||||
}
|
||||
|
||||
private void openFile() throws Exception {
|
||||
String name = openFileDialog(null, null);
|
||||
|
||||
try (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() == reset) {
|
||||
resetCommand();
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,37 +6,69 @@ import java.util.Set;
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class MyHashtable<K, V> implements Map<K, V> {
|
||||
private int capacity;
|
||||
private int size;
|
||||
private K[] keys = (K[]) new Object[10];
|
||||
private V[] values = (V[]) new Object[10];
|
||||
|
||||
public MyHashtable(int size) {
|
||||
capacity = size;
|
||||
clear();
|
||||
}
|
||||
|
||||
private boolean hashEquals(Object x, Object y) {
|
||||
if (x == null || y == null) {
|
||||
return x == y;
|
||||
} else {
|
||||
return x.hashCode() == y.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
keys = (K[]) new Object[capacity];
|
||||
values = (V[]) new Object[capacity];
|
||||
}
|
||||
|
||||
// Associates the specified value with the specified key in this map (optional operation).
|
||||
// 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();
|
||||
int hashCode = hash(key);
|
||||
int index = hashCode;
|
||||
|
||||
for (int i = 0; keys[index] != null && !hashEquals(keys[index], key); i++) {
|
||||
index = hash(hashCode + (int) Math.pow(i, 2));
|
||||
}
|
||||
|
||||
if (keys[index] == null) {
|
||||
size++;
|
||||
}
|
||||
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
// Returns the value to which this map maps the specified key.
|
||||
public V get(Object key) {
|
||||
// to be done
|
||||
throw new UnsupportedOperationException();
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (hashEquals(key, keys[i])) {
|
||||
return values[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Removes the mapping for this key from this map if present (optional operation).
|
||||
// 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();
|
||||
|
@ -44,11 +76,11 @@ public class MyHashtable<K, V> implements Map<K, V> {
|
|||
|
||||
// Returns the number of key-value mappings in this map.
|
||||
public int size() {
|
||||
// to be done
|
||||
throw new UnsupportedOperationException();
|
||||
return size;
|
||||
}
|
||||
|
||||
// UnsupportedOperationException ===================================================================
|
||||
// UnsupportedOperationException
|
||||
// ===================================================================
|
||||
// Returns a collection view of the values contained in this map.
|
||||
public Collection<V> values() {
|
||||
throw new UnsupportedOperationException();
|
||||
|
@ -74,7 +106,8 @@ public class MyHashtable<K, V> implements Map<K, V> {
|
|||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
// Copies all of the mappings from the specified map to this map (optional operation).
|
||||
// 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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue