From 31dee2221883ff1330b9c539b7086da3913ab4ec Mon Sep 17 00:00:00 2001 From: Nicolai Ommer Date: Sat, 5 Oct 2019 18:33:50 +0200 Subject: [PATCH 1/3] Code cleanup --- .gitignore | 2 + .../instanceables/IInstanceableEnum.java | 12 +- .../instanceables/IInstanceableObserver.java | 5 - .../instanceables/InstanceableClass.java | 108 ++++----------- .../instanceables/InstanceablePanel.java | 130 +++++++----------- .../instanceables/InstanceableParameter.java | 46 +++---- .../instanceables/InstanceableUtil.java | 21 +-- 7 files changed, 104 insertions(+), 220 deletions(-) diff --git a/.gitignore b/.gitignore index b83d222..f9a5458 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target/ +.idea +*.iml diff --git a/src/main/java/com/github/g3force/instanceables/IInstanceableEnum.java b/src/main/java/com/github/g3force/instanceables/IInstanceableEnum.java index 89049f8..6c83adf 100644 --- a/src/main/java/com/github/g3force/instanceables/IInstanceableEnum.java +++ b/src/main/java/com/github/g3force/instanceables/IInstanceableEnum.java @@ -10,19 +10,11 @@ /** * Implement this in an enum that provides {@link InstanceableClass}s - * - * @author Nicolai Ommer */ public interface IInstanceableEnum { - /** - * @return - */ InstanceableClass getInstanceableClass(); - - - /** - * @return - */ + + String name(); } diff --git a/src/main/java/com/github/g3force/instanceables/IInstanceableObserver.java b/src/main/java/com/github/g3force/instanceables/IInstanceableObserver.java index 6e6a2ba..de81024 100644 --- a/src/main/java/com/github/g3force/instanceables/IInstanceableObserver.java +++ b/src/main/java/com/github/g3force/instanceables/IInstanceableObserver.java @@ -10,13 +10,8 @@ /** * Observer interface to get notified about new instances - * - * @author Nicolai Ommer */ public interface IInstanceableObserver { - /** - * @param object - */ void onNewInstance(Object object); } diff --git a/src/main/java/com/github/g3force/instanceables/InstanceableClass.java b/src/main/java/com/github/g3force/instanceables/InstanceableClass.java index 35bb04e..64e3195 100644 --- a/src/main/java/com/github/g3force/instanceables/InstanceableClass.java +++ b/src/main/java/com/github/g3force/instanceables/InstanceableClass.java @@ -9,7 +9,6 @@ package com.github.g3force.instanceables; import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -17,40 +16,23 @@ /** * An {@link InstanceableClass} can be used to create an object from a class and a set of parameters. - * - * @author Nicolai Ommer */ public class InstanceableClass { - // -------------------------------------------------------------------------- - // --- variables and constants ---------------------------------------------- - // -------------------------------------------------------------------------- - private final Class impl; - private final List params; - - - // -------------------------------------------------------------------------- - // --- constructors --------------------------------------------------------- - // -------------------------------------------------------------------------- - - /** - * @param impl - * @param params - */ + private final Class impl; + private final List params; + + public InstanceableClass(final Class impl, final InstanceableParameter... params) { this.impl = impl; this.params = Arrays.asList(params); } - - - // -------------------------------------------------------------------------- - // --- methods -------------------------------------------------------------- - // -------------------------------------------------------------------------- - + + /** * Create a new instance with the specified arguments - * + * * @param args * @return * @throws NotCreateableException @@ -62,35 +44,20 @@ public Object newInstance(final Object... args) throws NotCreateableException { Constructor con = getConstructor(); result = con.newInstance(args); - } catch (final SecurityException err) - { - throw new NotCreateableException("", err); - } catch (final InstantiationException err) - { - throw new NotCreateableException("", err); - } catch (final IllegalAccessException err) - { - throw new NotCreateableException("", err); - } catch (final IllegalArgumentException err) - { - throw new NotCreateableException("", err); - } catch (final InvocationTargetException err) - { - throw new NotCreateableException("", err); - } catch (final IllegalStateException err) - { - throw new NotCreateableException("", err); } catch (NoSuchMethodException err) { throw new NotCreateableException("Wrong constructor types.", err); + } catch (final Exception err) + { + throw new NotCreateableException("Can not create instance", err); } return result; } - - + + /** * Create a new instance with default parameters (as defined in enum) - * + * * @return * @throws NotCreateableException */ @@ -100,7 +67,7 @@ public Object newDefaultInstance() throws NotCreateableException { return newInstance(); } - List objParams = new ArrayList(getParams().size()); + List objParams = new ArrayList<>(getParams().size()); for (InstanceableParameter param : getParams()) { Object objParam = param.parseString(param.getDefaultValue()); @@ -108,42 +75,33 @@ public Object newDefaultInstance() throws NotCreateableException } return newInstance(objParams.toArray()); } - - + + /** * Add parameter - * + * * @param param */ public void addParam(final InstanceableParameter param) { params.add(param); } - - - // -------------------------------------------------------------------------- - // --- getter/setter -------------------------------------------------------- - // -------------------------------------------------------------------------- - - + + /** * Returns the first public constructor of the Play. - * - * @return - * @throws NoSuchMethodException - * @throws SecurityException */ public Constructor getConstructor() throws NoSuchMethodException { - Class paramTypes[] = new Class[params.size()]; + Class[] paramTypes = new Class[params.size()]; for (int i = 0; i < params.size(); i++) { paramTypes[i] = params.get(i).getImpl(); } return impl.getConstructor(paramTypes); } - - + + /** * @return the params */ @@ -151,28 +109,20 @@ public final List getParams() { return params; } - - - /** - * @author Nicolai Ommer - */ + + public static class NotCreateableException extends Exception { - /** */ - private static final long serialVersionUID = 89775383135278930L; - - - /** - * @param message - * @param cause - */ + private static final long serialVersionUID = 89775383135278930L; + + public NotCreateableException(final String message, final Throwable cause) { super(message, cause); } } - - + + /** * @return the impl */ diff --git a/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java b/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java index 41a010c..edcc306 100644 --- a/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java +++ b/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java @@ -8,8 +8,7 @@ */ package com.github.g3force.instanceables; -import java.awt.BorderLayout; -import java.awt.GridLayout; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; @@ -17,13 +16,7 @@ import java.util.Properties; import java.util.concurrent.CopyOnWriteArrayList; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JComboBox; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JTextField; +import javax.swing.*; import org.apache.log4j.Logger; @@ -32,58 +25,44 @@ /** * Panel for creating custom instances - * - * @author Nicolai Ommer */ public class InstanceablePanel extends JPanel { - /** */ - private static final long serialVersionUID = -6272636064374504265L; - private static final Logger log = Logger.getLogger(InstanceablePanel.class - .getName()); - private final JComboBox cbbInstances; - private final JPanel inputPanel; - private final List inputFields = new ArrayList<>(); - private JButton btnCreate = null; - private final List observers = new CopyOnWriteArrayList(); - - private final Properties prop; - - - /** - * @param instanceableEnums - */ + private static final long serialVersionUID = -6272636064374504265L; + private static final Logger log = Logger.getLogger(InstanceablePanel.class + .getName()); + private final JComboBox cbbInstances; + private final JPanel inputPanel; + private final List inputFields = new ArrayList<>(); + private JButton btnCreate = null; + private final List observers = new CopyOnWriteArrayList<>(); + + private final Properties prop; + + public InstanceablePanel(final IInstanceableEnum[] instanceableEnums) { this(instanceableEnums, new Properties()); } - - - /** - * @param instanceableEnums - * @param prop - */ + + public InstanceablePanel(final IInstanceableEnum[] instanceableEnums, final Properties prop) { this.prop = prop; - cbbInstances = new JComboBox(instanceableEnums); + cbbInstances = new JComboBox<>(instanceableEnums); CbbInstancesActionListener cbbInstAl = new CbbInstancesActionListener(); cbbInstances.addActionListener(cbbInstAl); inputPanel = new JPanel(); inputPanel.setLayout(new GridLayout(0, 2)); - + cbbInstAl.actionPerformed(null); - + setLayout(new BorderLayout()); add(cbbInstances, BorderLayout.NORTH); add(inputPanel, BorderLayout.CENTER); } - - - /** - * @author Nicolai Ommer - * @param show - */ + + public void setShowCreate(final boolean show) { if (show && (btnCreate == null)) @@ -98,11 +77,8 @@ public void setShowCreate(final boolean show) btnCreate = null; } } - - - /** - * @param observer - */ + + public void addObserver(final IInstanceableObserver observer) { synchronized (observers) @@ -110,11 +86,8 @@ public void addObserver(final IInstanceableObserver observer) observers.add(observer); } } - - - /** - * @param observer - */ + + public void removeObserver(final IInstanceableObserver observer) { synchronized (observers) @@ -122,8 +95,8 @@ public void removeObserver(final IInstanceableObserver observer) observers.remove(observer); } } - - + + private void notifyNewInstance(final Object instance) { synchronized (observers) @@ -134,26 +107,20 @@ private void notifyNewInstance(final Object instance) } } } - - - /** - * @param item - */ + + public final void setSelectedItem(final Enum item) { cbbInstances.setSelectedItem(item); } - - - /** - * @return - */ + + public final IInstanceableEnum getSelectedItem() { return (IInstanceableEnum) cbbInstances.getSelectedItem(); } - - + + @Override public void setEnabled(final boolean enabled) { @@ -164,29 +131,29 @@ public void setEnabled(final boolean enabled) btnCreate.setEnabled(enabled); } } - - + + private String getModelKey(final IInstanceableEnum instance, final InstanceableParameter param) { return instance.getClass().getCanonicalName() + "." + instance.name() + "." + param.getDescription(); } - - + + private String loadParamValue(final IInstanceableEnum instance, final InstanceableParameter param) { return prop.getProperty(getModelKey(instance, param), param.getDefaultValue()); } - - + + private void saveParamValue(final IInstanceableEnum instance, final InstanceableParameter param, final String value) { prop.setProperty(getModelKey(instance, param), value); } - - + + private class CbbInstancesActionListener implements ActionListener { - + @Override public void actionPerformed(final ActionEvent e) { @@ -226,26 +193,23 @@ public void actionPerformed(final ActionEvent e) updateUI(); } } - + private class CreateInstanceActionListener implements ActionListener { - + @Override public void actionPerformed(final ActionEvent e) { createInstance(); } } - - - /** - * - */ + + public void createInstance() { IInstanceableEnum instanceName = (IInstanceableEnum) cbbInstances.getSelectedItem(); int i = 0; - List params = new ArrayList(instanceName.getInstanceableClass().getParams().size()); + List params = new ArrayList<>(instanceName.getInstanceableClass().getParams().size()); for (InstanceableParameter param : instanceName.getInstanceableClass().getParams()) { JComponent comp = inputFields.get(i); diff --git a/src/main/java/com/github/g3force/instanceables/InstanceableParameter.java b/src/main/java/com/github/g3force/instanceables/InstanceableParameter.java index 83df412..c02ec5c 100644 --- a/src/main/java/com/github/g3force/instanceables/InstanceableParameter.java +++ b/src/main/java/com/github/g3force/instanceables/InstanceableParameter.java @@ -16,25 +16,17 @@ /** * Parameter of a {@link InstanceableClass}. - * - * @author Nicolai Ommer */ public class InstanceableParameter { - private static String2ValueConverter s2vConv = String2ValueConverter.getDefault(); - - private final Class impl; - private final String description; - private final String defaultValue; - private final List> genericsImpls; - - - /** - * @param impl - * @param description - * @param defaultValue - * @param genericsImpls - */ + private static String2ValueConverter s2vConv = String2ValueConverter.getDefault(); + + private final Class impl; + private final String description; + private final String defaultValue; + private final List> genericsImpls; + + public InstanceableParameter(final Class impl, final String description, final String defaultValue, final Class... genericsImpls) { @@ -43,11 +35,11 @@ public InstanceableParameter(final Class impl, final String description, fina this.defaultValue = defaultValue; this.genericsImpls = Arrays.asList(genericsImpls); } - - + + /** * Parse given String to value - * + * * @param value * @return */ @@ -59,8 +51,8 @@ public Object parseString(final String value) } return s2vConv.parseString(impl, genericsImpls, value); } - - + + /** * @return the impl */ @@ -68,8 +60,8 @@ public final Class getImpl() { return impl; } - - + + /** * @return the description */ @@ -77,8 +69,8 @@ public final String getDescription() { return description; } - - + + /** * @return the defaultValue */ @@ -86,8 +78,8 @@ public final String getDefaultValue() { return defaultValue; } - - + + /** * @return the genericsImpls */ diff --git a/src/main/java/com/github/g3force/instanceables/InstanceableUtil.java b/src/main/java/com/github/g3force/instanceables/InstanceableUtil.java index dc3e09e..c52fc6f 100644 --- a/src/main/java/com/github/g3force/instanceables/InstanceableUtil.java +++ b/src/main/java/com/github/g3force/instanceables/InstanceableUtil.java @@ -5,18 +5,13 @@ /** - * @author Nicolai Ommer + * Utility class for this lib. */ public class InstanceableUtil { - /** - * @author Nicolai Ommer - * @param enumClazz - * @return - */ public static Set> getAllClasses(final Class> enumClazz) { - Set> classes = new LinkedHashSet>(); + Set> classes = new LinkedHashSet<>(); Enum values[] = enumClazz.getEnumConstants(); IInstanceableEnum valInstancable[] = (IInstanceableEnum[]) values; for (IInstanceableEnum en : valInstancable) @@ -35,17 +30,11 @@ public static Set> getAllClasses(final Class - * @param classes1 - * @param classes2 - * @return - */ + + public static Set> mergeClasses(final Set> classes1, final Set> classes2) { - Set> classes = new LinkedHashSet>(); + Set> classes = new LinkedHashSet<>(); classes.addAll(classes1); classes.addAll(classes2); return classes; From 22a13f9e588517d258cc399a339bc4db25503211 Mon Sep 17 00:00:00 2001 From: Nicolai Ommer Date: Sat, 5 Oct 2019 18:36:09 +0200 Subject: [PATCH 2/3] Migrate to log4j2 --- pom.xml | 10 +++++----- .../g3force/instanceables/InstanceablePanel.java | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 9ef3990..9dff3a6 100644 --- a/pom.xml +++ b/pom.xml @@ -20,15 +20,15 @@ - log4j - log4j - 1.2.17 + org.apache.logging.log4j + log4j-api + 2.12.1 com.github.g3force String2ValueConverter - v1.6 + 2.1 @@ -74,4 +74,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java b/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java index edcc306..efd5301 100644 --- a/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java +++ b/src/main/java/com/github/g3force/instanceables/InstanceablePanel.java @@ -18,7 +18,8 @@ import javax.swing.*; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import com.github.g3force.instanceables.InstanceableClass.NotCreateableException; @@ -29,8 +30,7 @@ public class InstanceablePanel extends JPanel { private static final long serialVersionUID = -6272636064374504265L; - private static final Logger log = Logger.getLogger(InstanceablePanel.class - .getName()); + private static final Logger log = LogManager.getLogger(InstanceablePanel.class.getName()); private final JComboBox cbbInstances; private final JPanel inputPanel; private final List inputFields = new ArrayList<>(); From ed5bfa3f1fca39e2634e3fddb08aa2bcb1669be7 Mon Sep 17 00:00:00 2001 From: Nicolai Ommer Date: Sat, 5 Oct 2019 18:36:27 +0200 Subject: [PATCH 3/3] Set version to 2.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9dff3a6..d6c7afc 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.github.g3force instanceables - v1.3 + 2.0 instanceables