All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.tsl2.nano.structure.ANode Maven / Gradle / Ivy

There is a newer version: 2.5.1
Show newest version
/*
 * 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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy