added some documentation

This commit is contained in:
hneemann 2016-04-09 14:07:55 +02:00
parent 8d704ba955
commit f335a93cf4
18 changed files with 182 additions and 29 deletions

View File

@ -19,9 +19,9 @@ public class SpeedTest {
public double calculate() throws NodeException {
ArrayList<Clock> clocks = model.getClocks();
if (clocks.isEmpty())
throw new NodeException(Lang.get("err_noClockFound"), null);
throw new NodeException(Lang.get("err_noClockFound"));
else if (clocks.size() > 1)
throw new NodeException(Lang.get("err_moreThenOneClocksFound"), null);
throw new NodeException(Lang.get("err_moreThenOneClocksFound"));
Clock clock = clocks.get(0);

View File

@ -38,7 +38,7 @@ public abstract class FanIn extends Node implements Element {
}
public static class FanInDescription extends ElementTypeDescription {
public FanInDescription(Class<?> clazz) {
public FanInDescription(Class<? extends Element> clazz) {
super(clazz);
addAttributes();
}

View File

@ -4,6 +4,9 @@ import de.neemann.digital.core.memory.DataField;
import de.neemann.digital.lang.Lang;
/**
* A key for a single attribute
*
* @param <VALUE> the type of the value
* @author hneemann
*/
public class AttributeKey<VALUE> {
@ -47,18 +50,34 @@ public class AttributeKey<VALUE> {
this.def = def;
}
/**
* Returns the attributes key
*
* @return the key
*/
public String getKey() {
return key;
}
/**
* Returns the attributes display name
*
* @return thr name
*/
public String getName() {
return name;
}
/**
* @return the default value of this key
*/
public VALUE getDefault() {
return def;
}
/**
* @return The values class
*/
public Class getValueClass() {
return def.getClass();
}
@ -68,6 +87,10 @@ public class AttributeKey<VALUE> {
return name;
}
/**
* A integer attribute.
* Stores additional combo box values
*/
public static final class AttributeKeyInteger extends AttributeKey<Integer> {
private Integer[] values;
@ -80,6 +103,9 @@ public class AttributeKey<VALUE> {
return this;
}
/**
* @return the values to show in the combo box
*/
public Integer[] getComboBoxValues() {
return values;
}

View File

@ -1,8 +1,15 @@
package de.neemann.digital.core.element;
/**
* Interface to implement a AttributeListener
*
* @author hneemann
*/
public interface AttributeListener {
/**
* Is called if an attribute changes
*
* @param key the key which value has changed
*/
void attributeChanged(AttributeKey key);
}

View File

@ -16,7 +16,7 @@ public interface Element {
* inputs of this element.
*
* @param inputs the list of <code>ObservableValue</code>s to use
* @throws NodeException
* @throws NodeException NodeException
*/
void setInputs(ObservableValue... inputs) throws NodeException;

View File

@ -7,6 +7,7 @@ import java.util.Map;
/**
* Describes one concrete Part.
* Its a Key value list, which is used to store the diferent elements attributes.
*
* @author hneemann
*/
@ -14,9 +15,17 @@ public class ElementAttributes {
private HashMap<String, Object> attributes;
private transient ArrayList<AttributeListener> listeners;
/**
* Creates a new instance
*/
public ElementAttributes() {
}
/**
* Creates a deep copy of the given attributes
*
* @param proto the ElementAttributes to copy
*/
public ElementAttributes(ElementAttributes proto) {
if (proto.attributes != null) {
attributes = new HashMap<>();
@ -26,6 +35,15 @@ public class ElementAttributes {
}
}
/**
* Gets a value from the map.
* If the value is not present the default value is returned
*
* @param key the key
* @param <VALUE> the type of the value
* @return the value
*/
@SuppressWarnings("unchecked")
public <VALUE> VALUE get(AttributeKey<VALUE> key) {
if (attributes == null)
return key.getDefault();
@ -37,6 +55,13 @@ public class ElementAttributes {
}
}
/**
* Checks if a value is present.
*
* @param key the key
* @param <VALUE> the type of the value
* @return true if value is present
*/
public <VALUE> boolean contains(AttributeKey<VALUE> key) {
if (attributes == null)
return false;
@ -44,6 +69,14 @@ public class ElementAttributes {
return attributes.containsKey(key.getKey());
}
/**
* Sets a value
*
* @param key the key
* @param value the value
* @param <VALUE> the type of the value
* @return this to chain calls
*/
public <VALUE> ElementAttributes set(AttributeKey<VALUE> key, VALUE value) {
if (value != get(key)) {
if (value.equals(key.getDefault())) {
@ -68,6 +101,11 @@ public class ElementAttributes {
l.attributeChanged(key);
}
/**
* Adds a listener to this class
*
* @param listener the listener
*/
public void addListener(AttributeListener listener) {
if (listeners == null)
listeners = new ArrayList<>();
@ -75,19 +113,43 @@ public class ElementAttributes {
listeners.add(listener);
}
/**
* removes a listener to this class
*
* @param listener the listener
*/
public void removeListener(AttributeListener listener) {
if (listeners != null)
listeners.remove(listener);
}
/**
* Returns the bits count stored in this attributes.
* Its a short hand for get(AttributeKey.Bits)
*
* @return the number of bits
*/
public int getBits() {
return get(AttributeKey.Bits);
}
/**
* Returns the label stored in this attributes.
* Its a short hand for get(AttributeKey.Label)
*
* @return the label
*/
public String getLabel() {
return get(AttributeKey.Label);
}
/**
* Sets the bit count to this map.
* Shorthand for set(AttributeKey.Bits, bits);
*
* @param bits the number of bits
* @return this tp chain calls
*/
public ElementAttributes setBits(int bits) {
set(AttributeKey.Bits, bits);
return this;
@ -95,17 +157,26 @@ public class ElementAttributes {
@Override
public String toString() {
return "ElementAttributes{" +
"attributes=" + attributes +
'}';
return "ElementAttributes{"
+ "attributes=" + attributes
+ '}';
}
/**
* @return true if map is empty
*/
public boolean isEmpty() {
if (attributes == null)
return true;
return attributes.isEmpty();
}
/**
* Gets a file stored directly in the map
*
* @param fileKey the file key
* @return the file
*/
public File getFile(String fileKey) {
if (attributes != null) {
Object f = attributes.get(fileKey);
@ -115,6 +186,12 @@ public class ElementAttributes {
return null;
}
/**
* Stores a file directly in the map
*
* @param fileKey the key
* @param file the file
*/
public void setFile(String fileKey, File file) {
if (attributes == null)
attributes = new HashMap<>();

View File

@ -1,8 +1,16 @@
package de.neemann.digital.core.element;
/**
* Interface is used to implement a factory for elements
*
* @author hneemann
*/
public interface ElementFactory {
/**
* creates a new element matching the given attributes
*
* @param attributes the attributes describing the element
* @return the created element
*/
Element create(ElementAttributes attributes);
}

View File

@ -19,11 +19,24 @@ public class ElementTypeDescription {
private final String[] inputNames;
private final ArrayList<AttributeKey> attributeList;
public ElementTypeDescription(Class<?> clazz, String... inputNames) {
/**
* Creates a new ElementTypeDescription
*
* @param clazz the elements class
* @param inputNames names of the input signals
*/
public ElementTypeDescription(Class<? extends Element> clazz, String... inputNames) {
this(clazz.getSimpleName(), clazz, inputNames);
}
public ElementTypeDescription(String name, Class<?> clazz, String... inputNames) {
/**
* Creates a new ElementTypeDescription
*
* @param name name of this element
* @param clazz the elements class
* @param inputNames names of the input signals
*/
public ElementTypeDescription(String name, Class<? extends Element> clazz, String... inputNames) {
this(name, new ElementFactory() {
@Override
public Element create(ElementAttributes attributes) {
@ -37,6 +50,13 @@ public class ElementTypeDescription {
}, inputNames);
}
/**
* Creates a new ElementTypeDescription
*
* @param name name of this element
* @param elementFactory factory used to create the element
* @param inputNames names of the input signals
*/
public ElementTypeDescription(String name, ElementFactory elementFactory, String... inputNames) {
this.name = name;
this.shortName = name;
@ -108,6 +128,7 @@ public class ElementTypeDescription {
*
* @param elementAttributes the elements attributes
* @return the list of input names
* @throws NodeException NodeException
*/
public String[] getInputNames(ElementAttributes elementAttributes) throws NodeException {
return inputNames;

View File

@ -1,13 +1,31 @@
package de.neemann.digital.core.element;
/**
* Warapper for the elements rotation
*
* @author hneemann
*/
public class Rotation {
public int rotation;
/**
* The rotation value.
* is in between 0 and 3
*/
private final int rotation;
public Rotation(int rot) {
rotation = rot;
/**
* Creates a new instance
*
* @param rotation the rotation
*/
public Rotation(int rotation) {
this.rotation = rotation;
}
/**
* @return the rotation value
*/
public int getRotation() {
return rotation;
}
@Override

View File

@ -1,10 +0,0 @@
package de.neemann.digital.core.element;
import de.neemann.digital.draw.shapes.Shape;
/**
* @author hneemann
*/
public interface ShapeFactory {
Shape create(ElementAttributes elementAttributes);
}

View File

@ -0,0 +1,6 @@
/**
* This package contains the classes used to describe and create the elements.
*
* @author hneemann
*/
package de.neemann.digital.core.element;

View File

@ -28,7 +28,7 @@ public class Const implements Element {
@Override
public void setInputs(ObservableValue... inputs) throws NodeException {
throw new NodeException(Lang.get("err_noInputsAvailable"), null);
throw new NodeException(Lang.get("err_noInputsAvailable"));
}
@Override

View File

@ -31,7 +31,7 @@ public class In implements Element {
@Override
public void setInputs(ObservableValue... inputs) throws NodeException {
throw new NodeException(Lang.get("err_noInputsAvailable"), null);
throw new NodeException(Lang.get("err_noInputsAvailable"));
}
@Override

View File

@ -33,7 +33,7 @@ public class Clock implements Element {
@Override
public void setInputs(ObservableValue... inputs) throws NodeException {
throw new NodeException(Lang.get("err_noInputsAvailable"), null);
throw new NodeException(Lang.get("err_noInputsAvailable"));
}
@Override

View File

@ -26,7 +26,7 @@ public class Reset implements Element {
@Override
public void setInputs(ObservableValue... inputs) throws NodeException {
throw new NodeException(Lang.get("err_noInputsAvailable"), null);
throw new NodeException(Lang.get("err_noInputsAvailable"));
}
@Override

View File

@ -195,7 +195,7 @@ public class VisualElement implements Drawable, Moveable, AttributeListener {
public void attributeChanged(AttributeKey key) {
shape = null;
minMax = null;
rotate = elementAttributes.get(AttributeKey.Rotate).rotation;
rotate = elementAttributes.get(AttributeKey.Rotate).getRotation();
}
@Override

View File

@ -77,7 +77,7 @@ public final class ShapeFactory {
try {
if (cr == null) {
if (library == null)
throw new NodeException(Lang.get("err_noShapeFoundFor_N", partName), null);
throw new NodeException(Lang.get("err_noShapeFoundFor_N", partName));
else {
ElementTypeDescription pt = library.getElementType(partName);
if (pt instanceof LibrarySelector.ElementTypeDescriptionCustom) {

View File

@ -241,7 +241,7 @@ public final class EditorFactory {
@Override
public Component getComponent(ElementAttributes elementAttributes) {
comb = new JComboBox<>(LIST);
comb.setSelectedIndex(rotation.rotation);
comb.setSelectedIndex(rotation.getRotation());
return comb;
}