package org.gocha.text;
import java.beans.ExceptionListener;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.HashSet;
/**
* @author gocha
*/
public class UnionWriter extends java.io.Writer
{
private Iterable<java.io.Writer> writers = new HashSet<Writer>();
private ExceptionListener exListener = null;
public UnionWriter()
{
}
public UnionWriter(Writer ... writers)
{
if( writers==null )return;
Iterable<Writer> ww = getWriters();
if( ww==null )return;
if( !(ww instanceof Collection) )return;
Collection<Writer> _w = (Collection<Writer>)ww;
for( Writer w : writers )
{
if( w==null )continue;
_w.add(w);
}
}
public UnionWriter(Iterable<Writer> writers)
{
if( writers==null )return;
Iterable<Writer> ww = getWriters();
if( ww==null )return;
if( !(ww instanceof Collection) )return;
Collection<Writer> _w = (Collection<Writer>)ww;
for( Writer w : writers )
{
if( w==null )continue;
_w.add(w);
}
}
public UnionWriter(ExceptionListener exceptionListener, Writer ... writers)
{
setExceptionListener(exceptionListener);
if( writers==null )return;
Iterable<Writer> ww = getWriters();
if( ww==null )return;
if( !(ww instanceof Collection) )return;
Collection<Writer> _w = (Collection<Writer>)ww;
for( Writer w : writers )
{
if( w==null )continue;
_w.add(w);
}
}
public UnionWriter(ExceptionListener exceptionListener, Iterable<Writer> writers)
{
setExceptionListener(exceptionListener);
if( writers==null )return;
Iterable<Writer> ww = getWriters();
if( ww==null )return;
if( !(ww instanceof Collection) )return;
Collection<Writer> _w = (Collection<Writer>)ww;
for( Writer w : writers )
{
if( w==null )continue;
_w.add(w);
}
}
public Iterable<Writer> getWriters() {
if( writers==null )writers = new HashSet<Writer>();
return writers;
}
public void setWriters(Iterable<Writer> writers) {
this.writers = writers;
}
public ExceptionListener getExceptionListener() {
return exListener;
}
public void setExceptionListener(ExceptionListener exListener) {
this.exListener = exListener;
}
protected boolean isCatchExceptionForEach()
{
return true;
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
Iterable<Writer> _writers = getWriters();
if( _writers==null )return;
for( Writer w : _writers )
{
if( w==null )continue;
if( isCatchExceptionForEach() )
{
try
{
w.write(cbuf, off, len);
}
catch(Exception e)
{
ExceptionListener el = getExceptionListener();
if( el!=null )el.exceptionThrown(e);
}
}else{
w.write(cbuf, off, len);
}
}
}
@Override
public void flush() throws IOException {
Iterable<Writer> _writers = getWriters();
if( _writers==null )return;
for( Writer w : _writers )
{
if( w==null )continue;
if( isCatchExceptionForEach() )
{
try
{
w.flush();
}
catch(Exception e)
{
ExceptionListener el = getExceptionListener();
if( el!=null )el.exceptionThrown(e);
}
}else{
w.flush();
}
}
}
@Override
public void close() throws IOException {
Iterable<Writer> _writers = getWriters();
if( _writers==null )return;
for( Writer w : _writers )
{
if( w==null )continue;
if( isCatchExceptionForEach() )
{
try
{
w.close();
}
catch(Exception e)
{
ExceptionListener el = getExceptionListener();
if( el!=null )el.exceptionThrown(e);
}
}else{
w.close();
}
}
}
}