*/
protected final LogicModelModifiable model;
/**
- * The name of this component. Is unique for all components in its model.
+ * The name of this component. Is unique for all components in its model.<br>
+ * Does never change, but can't be final since it is set in {@link #init()}.
*/
- public final String name;
+ private String name;
private final Rectangle bounds;
/**
* The list of all pins of this component by name.
protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
{
this.model = model;
- this.name = name == null ? model.getDefaultComponentName(this) : name;
+ this.name = name;
this.bounds = new Rectangle(0, 0, 0, 0);
this.pinsByName = new HashMap<>();
this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
/**
* Initializes this component. This method should be called exactly once in this component's constructor.<br>
- * Currently, this method only registers this component in the model.
+ * <ul>
+ * <li>If <code>{@link #name}==null</code>, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}.
+ * <li>Registers this component in the model.
+ * </ul>
*/
protected void init()
{
+ if (name == null)
+ name = model.getDefaultComponentName(this);
model.componentCreated(this, this::destroyed);
}
+ // basic getters
+
+ public String getName()
+ {
+ return name;
+ }
+
/**
* Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent)
* destroyComponent()} of the model this component is a part of.<br>
* Returns the pin with the given name of this component.
*
* @throws IllegalArgumentException if there is no pin with the given name
+ * @see #getPinOrNull(String)
*
* @author Daniel Kirschten
*/
public Pin getPin(String name)
{
- Pin pin = pinsByName.get(name);
+ Pin pin = getPinOrNull(name);
if (pin == null)
throw new IllegalArgumentException("No pin with the name " + name);
return pin;
}
+ /**
+ * Returns the pin with the given name of this component, or <code>null</code> if there is no such pin.
+ *
+ * @see #getPin(String)
+ *
+ * @author Daniel Kirschten
+ */
+ public Pin getPinOrNull(String name)
+ {
+ return pinsByName.get(name);
+ }
+
// high-level access
/**