Package net.sf.mrailsim.rails

Source Code of net.sf.mrailsim.rails.NodeManager

/************************************************************************************

    MRailSim - a model railway simulation program - http://mrailsim.sourceforge.net/
    Copyright (C) 2004,2007  Bernd Arnold

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

   
************************************************************************************/


package net.sf.mrailsim.rails;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.mrailsim.shared.Coordinates;

/**
* NodeManager is the class that is responsible for all nodes which holds a list
* of all created nodes.
* @see Node
*/
public class NodeManager {

  /**
   * The list with all nodes.
   */
  private ArrayList<Node> nodeList;
 
  /**
   * Class constructor; allocates a new empty node list.
   */
  public NodeManager() {
    nodeList = new ArrayList<Node>();
  }
 
  /**
   * Returns the total number of registered nodes.
   * @return  the count of nodes
   */
  public int getNodeCount() {
    return nodeList.size();
  }
 
  /**
   * Registers a new node if not already registered; returns a reference
   * to the node. You can use <code>registerNodes(...)</code>, if you want
   * to register more nodes at once.
   *
   * @param id     the id of the node
   * @param track  the track that references this node
   * @return       the node object
   * @see          Node
   * @see          Track
   * @see          #registerNodes(long[], Track)
   */
  public Node registerNode( long id, Track track ) {
    Node node = getNode( id );
   
    if ( node == null ) {
      node = new Node( id );
      nodeList.add( node );
    }
   
    try {
      node.registerTrack( track );
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit( 2 );
    }
   
    return node;
  }

  /**
   * Returns the node object with the given id
   * @param id  the id of the node
   * @return    the node object or <code>null</code>, if not found
   */
  public Node getNode( long id ) {
    for ( Node item : nodeList ) {
      if ( item.getId() == id ) {
        return item;
      }
    }
    return null;
  }

  public void setCoordinates( String s ) {
    Pattern pattern = Pattern.compile( "^Id=(\\d+),X=(\\d+),Y=(\\d+)$" );
    Matcher matcher = pattern.matcher( s );
    if ( ! matcher.matches() ) {
      // TODO: Handle right
      System.err.println( "setCoordinates mismatch: " + s );
      System.exit(1);
    }
    long id = Long.parseLong( matcher.group( 1 ) );
    int x = Integer.parseInt( matcher.group( 2 ) );
    int y = Integer.parseInt( matcher.group( 3 ) );

    Node node = getNode( id );
    if ( node != null ) {
      node.setCoordinates( new Coordinates( x, y ) );
    } else {
      // TODO: Handle better
      System.err.println( "setCoordinates(String): Node #" + id + " does not exist." );
    }
  }

  /**
   * Registers a list of node ids for the given track. Calls the
   * <code>registerNode(...)</code> method.
   *
   * @param nodeIds  An array of node ids
   * @param track    The track the node ids belong to
   * @return  An array of Node objects
   * @see  Node
   * @see  #registerNode(long, Track)
   */
  public Node[] registerNodes( long[] nodeIds, Track track ) {
    Node nodes[];

    nodes = new Node[ nodeIds.length ];

    for ( int i = 0; i < nodeIds.length; i++ ) {
      nodes[ i ] = registerNode( nodeIds[ i ], track );
    }
   
    return nodes;
  }

}
TOP

Related Classes of net.sf.mrailsim.rails.NodeManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.