de.tsl2.nano.structure.ANode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tsl2.nano.datastructure Show documentation
Show all versions of tsl2.nano.datastructure Show documentation
optimized implementations for trees, collections, arrays, historized input
/*
* File: $HeadURL$
* Id : $Id$
*
* created by: Tom
* created on: 22.01.2015
*
* Copyright: (c) Thomas Schneider 2015, all rights reserved
*/
package de.tsl2.nano.structure;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import de.tsl2.nano.collection.CollectionUtil;
import de.tsl2.nano.core.ITransformer;
import de.tsl2.nano.core.messaging.EventController;
import de.tsl2.nano.core.messaging.IListener;
import de.tsl2.nano.core.util.StringUtil;
/**
* see {@link INode}
*
* @author Tom
* @version $Revision$
*/
public class ANode implements INode, Serializable {
private static final long serialVersionUID = 1L;
/** the nodes real object */
protected T core;
/** connections to other nodes in a net */
protected List> connections;
/** controller, handling notification events for other nodes */
protected EventController controller;
public ANode() {
}
public ANode(T core) {
this(core, null);
}
public ANode(T core, List> connections) {
this(core, connections, null);
}
public ANode(T core, List> connections, EventController controller) {
super();
this.core = core;
this.connections = connections;
this.controller = controller;
}
/**
* @return Returns the {@link #core}.
*/
@Override
public T getCore() {
return core;
}
/**
* @return Returns the controller.
*/
public EventController getController() {
if (controller == null)
controller = new EventController();
return controller;
}
/**
* @return Returns the {@link #connections}.
*/
@Override
public List> getConnections() {
if (connections == null)
connections = new LinkedList>();
return connections;
}
/**
* getConnection
*
* @param destination
* @return
*/
public IConnection getConnection(INode destination) {
for (IConnection c : getConnections()) {
if (c.getDestination().equals(destination)) {
return c;
}
}
return null;
}
/**
* convenience to add a connections. delegates to {@link #connect(ANode, Object)} with null descriptor.
*/
public IConnection add(ANode destination) {
return connect(destination, null);
}
/**
* creates a new connection
*
* @param destination node to connect to
* @param descriptor connection description
* @return new created connection
*/
@SuppressWarnings("rawtypes")
public IConnection connect(ANode destination, D descriptor) {
IConnection connection = createConnection(destination, descriptor);
getConnections().add(connection);
if (connection instanceof IListener) {
getController().addListener(((IListener) connection));
}
return connection;
}
/**
* creates a connection instance
*
* @param destination node to connect to
* @param descriptor connection description
* @return new created connection
*/
protected IConnection createConnection(ANode destination, D descriptor) {
return new AConnection(destination, descriptor);
}
// public List createChildNodes(List items) {
// this.connections = new ArrayList>(items.size());
// for (CORE c : items) {
// this.connections.add(new ANode(null, c));
// }
// return this.connections;
// }
/**
* getChildNodes
*
* @return transformed list of children-nodes
*/
protected List getConnectionItems() {
return CollectionUtil.getTransforming(getConnections(), new ITransformer, T>() {
@Override
public T transform(IConnection toTransform) {
return toTransform.getDestination().getCore();
}
});
}
protected INode getConnection(T item) {
for (IConnection c : getConnections()) {
if (item.equals(c.getDestination().getCore())) {
return c.getDestination();
}
}
return null;
}
@Override
public INode path(String... nodeFilters) {
List filter = CollectionUtil.getFiltering(getConnectionItems(), new StringBuilder(nodeFilters[0]));
if (filter.size() == 0) {
return null;
} else if (filter.size() > 1) {
throw new IllegalArgumentException("node filter " + nodeFilters[0] + " was not unique for tree-children "
+ StringUtil.toString(getConnections(), 100));
} else {
return getConnection(filter.iterator().next()).path(
CollectionUtil.copyOfRange(nodeFilters, 1, nodeFilters.length));
}
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((core == null) ? 0 : core.hashCode());
return result;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return hashCode() == obj.hashCode();
}
}