package org.dgpf.search.api.events;
import org.dgpf.search.api.SearchState;
import org.sfc.events.EventOutput;
import org.sfc.meta.MetaInformationWriter;
import org.sfc.utils.Typesafe;
/**
* The csv search listener produces comma separated value output that
* describes the search status.
*
* @author Thomas Weise
*/
public class CSVSearchListener extends EventOutput
{
/**
* The search update event handler.
*/
protected static final EventHandler<SearchUpdateEvent>
SEARCH_UPDATE_EVENT_HANDLER =
new EventHandler<SearchUpdateEvent>()
{
@Override
protected final void handle_event(final SearchUpdateEvent p_event,
final EventOutput p_output)
{
SearchUpdateEvent<?> l_su;
if(p_output instanceof CSVSearchListener)
{
l_su = Typesafe.cast(p_event);
((CSVSearchListener)p_output).update(l_su);
}
}
};
/**
* <code>true</code> if and only if a headline is required to be written.
*/
private boolean m_need_headline ;
/**
* The event output modulus.
*/
private final int m_mod ;
/**
* Create a new csv search event output listener.
* @param p_out The output stream to write to.
* @param p_mod The event output modulo.
*/
public CSVSearchListener(final Object p_out,
final int p_mod)
{
super(p_out);
this.m_need_headline = true;
this.add_handler(SearchUpdateEvent.class,
SEARCH_UPDATE_EVENT_HANDLER);
this.m_mod = ((p_mod > 1) ? p_mod : 1);
}
/**
* Create a new csv search event output listener.
* @param p_out The output stream to write to.
*/
public CSVSearchListener(final Object p_out)
{
this(p_out, -1);
}
/**
* Receive a search update event.
* @param p_sue The search update event.
*/
final void update (final SearchUpdateEvent<?> p_sue)
{
SearchState<?> l_se;
l_se = p_sue.get_state();
if((l_se.get_update_count() % this.m_mod) == 0)
{
if(this.m_need_headline)
{
this.m_need_headline = false;
MetaInformationWriter.print_csv_head(l_se, this);
}
MetaInformationWriter.print_csv_data(l_se, this);
}
}
}