From b88bcdd406ef53e7e19f04abf3a708ac30de9c78 Mon Sep 17 00:00:00 2001 From: schuhmannje82308 Date: Mon, 20 Nov 2023 11:51:56 +0100 Subject: [PATCH] Init Commit --- build.xml | 73 ++ manifest.mf | 3 + nbproject/build-impl.xml | 1771 +++++++++++++++++++++++++++ nbproject/genfiles.properties | 8 + nbproject/project.properties | 97 ++ nbproject/project.xml | 15 + src/bandit/Start.java | 45 + src/bandit/StartStopController.java | 49 + src/bandit/WertAdapter.java | 55 + src/bandit/ZahlenView.form | 62 + src/bandit/ZahlenView.java | 132 ++ src/bandit/Zahlengenerator.java | 129 ++ src/bandit/util/OhmLogger.java | 55 + 13 files changed, 2494 insertions(+) create mode 100644 build.xml create mode 100644 manifest.mf create mode 100644 nbproject/build-impl.xml create mode 100644 nbproject/genfiles.properties create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml create mode 100644 src/bandit/Start.java create mode 100644 src/bandit/StartStopController.java create mode 100644 src/bandit/WertAdapter.java create mode 100644 src/bandit/ZahlenView.form create mode 100644 src/bandit/ZahlenView.java create mode 100644 src/bandit/Zahlengenerator.java create mode 100644 src/bandit/util/OhmLogger.java diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..156ea44 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Aufgabe7_bandit. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..3830812 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1771 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..406e1d6 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=ed1b7c08 +build.xml.script.CRC32=ec768dde +build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=ed1b7c08 +nbproject/build-impl.xml.script.CRC32=6154016a +nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..f9b18fb --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,97 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=Aufgabe7_bandit +application.vendor=le +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.modulepath=\ + ${run.modulepath} +debug.test.classpath=\ + ${run.test.classpath} +debug.test.modulepath=\ + ${run.test.modulepath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Aufgabe7_bandit.jar +dist.javadoc.dir=${dist.dir}/javadoc +dist.jlink.dir=${dist.dir}/jlink +dist.jlink.output=${dist.jlink.dir}/Aufgabe7_bandit +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.modulepath= +javac.processormodulepath= +javac.processorpath=\ + ${javac.classpath} +javac.source=17 +javac.target=17 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.modulepath=\ + ${javac.modulepath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.html5=false +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +# The jlink additional root modules to resolve +jlink.additionalmodules= +# The jlink additional command line parameters +jlink.additionalparam= +jlink.launcher=true +jlink.launcher.name=Aufgabe7_bandit +main.class=bandit.Start +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.modulepath=\ + ${javac.modulepath} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +run.test.modulepath=\ + ${javac.test.modulepath} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..40d1e57 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Aufgabe7_bandit + + + + + + + + + diff --git a/src/bandit/Start.java b/src/bandit/Start.java new file mode 100644 index 0000000..caedbbc --- /dev/null +++ b/src/bandit/Start.java @@ -0,0 +1,45 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template + */ + +package bandit; + +import javax.swing.JOptionPane; +import javax.swing.UIManager; + +/** + * Builder Class + * @author le + */ +public class Start +{ + public Start() + { + ZahlenView view = new ZahlenView(); + Zahlengenerator model = new Zahlengenerator(); + StartStopController controller = new StartStopController(view, model); + WertAdapter adapter = new WertAdapter(view, model); + + controller.registerEvents(); + model.addWertSubscription(adapter); + view.setVisible(true); + + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception ex) + { + JOptionPane.showMessageDialog(null, ex.toString()); + } + new Start(); + } +} diff --git a/src/bandit/StartStopController.java b/src/bandit/StartStopController.java new file mode 100644 index 0000000..677d85a --- /dev/null +++ b/src/bandit/StartStopController.java @@ -0,0 +1,49 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package bandit; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.logging.Logger; +import bandit.util.OhmLogger; + +/** + * + * @author le + */ +public class StartStopController implements ActionListener +{ + private static Logger lg = OhmLogger.getLogger(); + private ZahlenView view; + private Zahlengenerator model; + + public StartStopController(ZahlenView view, Zahlengenerator model) + { + this.view = view; + this.model = model; + } + + public void registerEvents() + { + view.getBtnStart().addActionListener(this); + view.getBtnStop().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent evt) + { + if (evt.getSource() == view.getBtnStart()) + { + lg.info("Start"); + model.start(); + } + else + { + lg.info("Stop"); + model.stop(); + } + } +} diff --git a/src/bandit/WertAdapter.java b/src/bandit/WertAdapter.java new file mode 100644 index 0000000..120720d --- /dev/null +++ b/src/bandit/WertAdapter.java @@ -0,0 +1,55 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package bandit; + +import java.util.concurrent.Flow; +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.Flow.Subscription; + +/** + * + * @author le + */ +public class WertAdapter implements Subscriber +{ + private ZahlenView view; + private Zahlengenerator model; + private Subscription subscription; + + public WertAdapter(ZahlenView view, Zahlengenerator model) + { + this.view = view; + this.model = model; + } + + @Override + public void onSubscribe(Flow.Subscription subscription) + { + this.subscription = subscription; + this.subscription.request(1); + //this.subscription.request(Long.MAX_VALUE); + } + + @Override + public void onNext(Integer item) + { + String strWert = String.valueOf(item); + view.getLblZahl().setText(strWert); + this.subscription.request(1); + } + + @Override + public void onError(Throwable throwable) + { + } + + @Override + public void onComplete() + { + } + + +} diff --git a/src/bandit/ZahlenView.form b/src/bandit/ZahlenView.form new file mode 100644 index 0000000..88ce7ad --- /dev/null +++ b/src/bandit/ZahlenView.form @@ -0,0 +1,62 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/bandit/ZahlenView.java b/src/bandit/ZahlenView.java new file mode 100644 index 0000000..dc7cdf5 --- /dev/null +++ b/src/bandit/ZahlenView.java @@ -0,0 +1,132 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package bandit; + +/** + * + * @author le + */ +public class ZahlenView extends javax.swing.JFrame +{ + /** + * @return the btnStart + */ + public javax.swing.JButton getBtnStart() + { + return btnStart; + } + + /** + * @return the btnStop + */ + public javax.swing.JButton getBtnStop() + { + return btnStop; + } + + /** + * @return the lblZahl + */ + public javax.swing.JLabel getLblZahl() + { + return lblZahl; + } + + /** + * Creates new form ZahlenView + */ + public ZahlenView() + { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + lblZahl = new javax.swing.JLabel(); + jPanel1 = new javax.swing.JPanel(); + btnStart = new javax.swing.JButton(); + btnStop = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + lblZahl.setFont(new java.awt.Font("Liberation Sans", 0, 100)); // NOI18N + lblZahl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + lblZahl.setText("?"); + getContentPane().add(lblZahl, java.awt.BorderLayout.CENTER); + + btnStart.setText("Start"); + jPanel1.add(btnStart); + + btnStop.setText("Stop"); + jPanel1.add(btnStop); + + getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END); + + pack(); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) + { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try + { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) + { + if ("Nimbus".equals(info.getName())) + { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } + catch (ClassNotFoundException ex) + { + java.util.logging.Logger.getLogger(ZahlenView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (InstantiationException ex) + { + java.util.logging.Logger.getLogger(ZahlenView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (IllegalAccessException ex) + { + java.util.logging.Logger.getLogger(ZahlenView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (javax.swing.UnsupportedLookAndFeelException ex) + { + java.util.logging.Logger.getLogger(ZahlenView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() + { + public void run() + { + new ZahlenView().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton btnStart; + private javax.swing.JButton btnStop; + private javax.swing.JPanel jPanel1; + private javax.swing.JLabel lblZahl; + // End of variables declaration//GEN-END:variables +} diff --git a/src/bandit/Zahlengenerator.java b/src/bandit/Zahlengenerator.java new file mode 100644 index 0000000..5175616 --- /dev/null +++ b/src/bandit/Zahlengenerator.java @@ -0,0 +1,129 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package bandit; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.SubmissionPublisher; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Logger; +import bandit.util.OhmLogger; + +/** + * + * @author le + */ +public class Zahlengenerator implements Runnable // Callable +{ + private static Logger lg = OhmLogger.getLogger(); //Logger sollten immer static sein da sie für alle instanzen loggs ausführen + + private int wert; + /** Zustandsvariable !!! + * bei 2 Werten boolean sonst enum + */ +// private volatile boolean laufend; + private AtomicBoolean laufend; + //private Thread thd; + private ExecutorService eService; + + private final Object LOCK; + + private SubmissionPublisher wertPublisher; + + public Zahlengenerator() + { + wert = -1; + //laufend = false; + laufend = new AtomicBoolean(false); + //thd = null; + eService = Executors.newSingleThreadExecutor(); + wertPublisher = new SubmissionPublisher<>(); + LOCK = new Object(); + } + + public void addWertSubscription(Subscriber subscriber) + { + wertPublisher.subscribe(subscriber); + } + + public void start() + { + //laufend = true; + laufend.set(true); + synchronized (LOCK) + { + LOCK.notify(); + } + + + if (eService.isShutdown()) + { + eService = Executors.newSingleThreadExecutor(); + } + eService.execute(this); + //Future task = eService.submit(this); v.a. bei callable +// if (thd == null) +// { +// thd = new Thread(this); +// thd.start(); +// } + } + + public void stop() + { + //laufend = false; + laufend.set(false); + } + + @Override + public void run() + { + while (true) + { + //while (!laufend) + while (!laufend.get()) + { + synchronized (LOCK) + { + try + { + lg.info("WAIT"); + LOCK.wait(); + } + catch (InterruptedException ex) + { + lg.warning(ex.toString()); + } + } + } + + try + { + Thread.sleep(100); + } + catch (Exception ex) + { + System.err.println(ex); + } + this.berechneWert(); + wertPublisher.submit(wert); + } +// thd = null; +// eService.shutdown(); + } + + private synchronized void berechneWert() + { + wert = (int) (1 + 49*Math.random()); + } + + public synchronized int getWert() + { + return wert; + } + +} diff --git a/src/bandit/util/OhmLogger.java b/src/bandit/util/OhmLogger.java new file mode 100644 index 0000000..3d48b4f --- /dev/null +++ b/src/bandit/util/OhmLogger.java @@ -0,0 +1,55 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package bandit.util; + +import java.time.LocalDateTime; +import java.util.logging.*; +/** + * + * @author le + */ +public class OhmLogger +{ + private static Logger lg = null; + + private OhmLogger() //Singelton-Patter -> privater ctor -> anlegen nur über klassenmethode möglich + { + } + + public static Logger getLogger() + { + if (lg == null) + { + lg = Logger.getLogger("OhmLogger"); + initLogger(); + } + return lg; + } + + private static void initLogger() + { +// FileHandler fh = new FileHandler(); + ConsoleHandler ch = new ConsoleHandler(); + ch.setFormatter(new OhmFormatter()); + lg.addHandler(ch); + } +} + +class OhmFormatter extends SimpleFormatter +{ + @Override + public String format(LogRecord record) + { + String logline = ""; + + LocalDateTime ldt = LocalDateTime.now(); + logline += ldt.toString(); + logline += ";" + record.getMessage(); + logline += "\n"; + + return logline; + } +}