Logo
Distributed Genetic Programming Framework
print print

File org.sfc.events.EventOutput.java

Here you can find all the information about the file org.sfc.events.EventOutput.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2005 Thomas Weise
 *
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2005-12-20 09:43:24
 * Original Filename: org.sfc.events.EventOutput.java
 * Version          : 3.0.0
 * Last modification: 2006-04-10
 *                by: Thomas Weise
 *
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */


package org.sfc.events;

import java.util.Map;

import org.sfc.collections.Collections;
import org.sfc.io.TextWriter;
import org.sfc.utils.Typesafe;

/**
 * This class is able to print event data to some output.
 *
 *
 * @author Thomas Weise
 */

public  class EventOutput extends    TextWriter
                          implements IEventListener
  {
/**
 * The internal map with the handlers to call when events get caught.
 */

  private final Map<Object, EventHandler<SfcEvent>>   m_handlers;

/**
 * Create a new event output listener.
 * @param p_out             The output stream to write to.
 * @param p_default_indent  The default indentation step width.
 */

  public EventOutput(final Object p_out,
                     final int    p_default_indent)
    {
    super(p_out, p_default_indent);
    this.m_handlers = Collections.create_map(-1);
    }
  
/**
 * Create a new event output listener.
 * @param p_out             The output stream to write to.
 */

  public EventOutput(final Object p_out)
    {
    this(p_out, -1);
    }

/**
 * Store a new handler in this event output.
 * @param p_class The class to be handled.
 * @param p_handler The handler object to assign to the class.
 * @param <Type>  The type of the handler.
 */

  protected final <Type extends SfcEvent>
                      void  add_handler(final Class<Type>        p_class,
                                        final EventHandler<Type> p_handler)
    {
    EventHandler<SfcEvent>  l_x;
    l_x = Typesafe.cast(p_handler);
    this.m_handlers.put(p_class, l_x);
    }




/**
 * This method is called when the event sink receives an event.
 * @param p_event The event to be received.
 */

  public  final void  receive (final SfcEvent  p_event)
    {
    Map<Object, EventHandler<SfcEvent>> l_map;
    Class                               l_c;
    EventHandler<SfcEvent>              l_eh;
    int                                 l_i;

    if(p_event != null)
      {
      l_map = this.m_handlers;

      for(l_c = p_event.getClass(); l_c != null; l_c = l_c.getSuperclass())
        {
        l_eh = l_map.get(l_c);
        if(l_eh != null)
          {
          synchronized(this.m_handlers)
            {
            l_i  = this.get_buffer_length();
            l_eh.handle_event(p_event, this);
            if(l_i != this.get_buffer_length())
              {
              this.write_linebreak();
              this.flush();
              }
            }

          return;
          }
        }
      }
    }


/**
 * The internal handler class.
 * @param <EventType>     The event type this handler is used for.
 * @author Thomas Weise
 */

  public  static abstract class EventHandler<EventType extends SfcEvent>
    {
/**
 * Handle an event of a given type.
 * @param p_event   The event that was caught by the calling event output.
 * @param p_output  The calling event output. Write your stuff to this
 *                  object.
 */

    protected abstract void handle_event(final EventType   p_event,
                                         final EventOutput p_output);
    }
  }

File Information:

file name:EventOutput.java
package:org.sfc.events
qualified name:org.sfc.events.EventOutput.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/events/EventOutput.java
size:4.178 KB (4279 B)
uploaded: 2015-07-22 04:11:11 GMT+0000
last update: 2006-05-05 14:21:09 GMT+0000
last access: 2018-01-22 04:01:54 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2018-01-22 04:01:54 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo