Print

Print


Commit in lcsim/src/org/lcsim/util/loop on MAIN
LCIODriver.java+53-631.13 -> 1.14
JM: cleanup collection filtering method names; remove public access to embedded writer

lcsim/src/org/lcsim/util/loop
LCIODriver.java 1.13 -> 1.14
diff -u -r1.13 -r1.14
--- LCIODriver.java	17 Dec 2008 18:25:22 -0000	1.13
+++ LCIODriver.java	18 Dec 2008 20:47:09 -0000	1.14
@@ -5,6 +5,8 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.lcsim.event.EventHeader;
 import org.lcsim.util.Driver;
@@ -12,30 +14,30 @@
 
 /**
  * A driver for writing out LCIO events. 
+ * 
  * By default this will write out the entire event, but you can control
- * what is written out by accessing embedded LCIOWriter, for example
- * <pre>
- *   lcioDriver.getWriter().addWriteOnly("PFAReconstructedParticles");
- * </pre>
- * See the LCIOWriter documentation for more info
+ * what collections are written out by using {@link #setIgnoreCollection(String)},
+ * {@link #setIgnoreCollections(String[])}, {@link #setWriteOnlyCollection(String)},
+ * and {@link #setWriteOnlyCollections(String[])}.  
+ * 
  * @author tonyj
  * @see org.lcsim.util.lcio.LCIOWriter
  */
 public class LCIODriver extends Driver
 {
 	private LCIOWriter writer;
-	private Collection<String> listIgnore = Collections.EMPTY_SET;
-	private Collection<String> listKeep = Collections.EMPTY_SET;
-	private String filePath;
+	private Set<String> listIgnore = new HashSet<String>();
+	private Set<String> listKeep = new HashSet<String>();
+	private File outputFile;
 	
 	public LCIODriver(String file)
 	{
-		this(file, Collections.EMPTY_SET);
+		this(file, null);
 	}
 	
 	public LCIODriver(File file)
 	{
-		this(file, Collections.EMPTY_SET);
+		this(file, null);
 	}
 	
 	public LCIODriver(String file, Collection<String> listIgnore)
@@ -45,23 +47,13 @@
 	
 	public LCIODriver(File file, Collection<String> listIgnore)
 	{
-		try
-		{
-			this.writer = new LCIOWriter(file);
-                        if (listIgnore != Collections.EMPTY_SET)
-                            this.writer.addAllIgnore(listIgnore);
-		}
-		catch (IOException x)
-		{
-			throw new RuntimeException("Error opening LCIO file",x);
-		}  
+        this.outputFile = file;
+        if (listIgnore != null)
+        {
+            this.listIgnore.addAll(listIgnore);
+        }
 	}
-	
-	public LCIODriver(LCIOWriter writer)
-	{
-		this.writer = writer;
-	}
-	
+		
 	public LCIODriver()
 	{}
 		
@@ -69,57 +61,55 @@
 	{	
 		if (!filePath.endsWith(".slcio"))
 			filePath += ".slcio";
-		this.filePath = filePath;
+        outputFile = new File(filePath);
 	}
 	
 	public void setIgnoreCollections(String[] ignoreCollections)
 	{
-		listIgnore = Arrays.asList(ignoreCollections);
-                if (writer != null)
-                    writer.addAllIgnore(listIgnore);
+		listIgnore.addAll(Arrays.asList(ignoreCollections));
 	}
 	
 	public void setWriteOnlyCollections(String [] keepCollections)
 	{
-		listKeep = Arrays.asList(keepCollections);
-                if (writer != null)
-                    writer.addAllWriteOnly(listKeep);
-	}
-	
-	public LCIOWriter getWriter()
-	{
-		return writer;
+		listKeep.addAll(Arrays.asList(keepCollections));
 	}
-	
-	protected void startOfData()
-	{
-		// Setup writer if it hasn't been initialized yet.
-		if (this.writer == null)
-		{
-			if (filePath == null)
-				throw new RuntimeException("Cannot create LCIO writer.  The filePath was not set!");
-			try
-			{
-				this.writer = new LCIOWriter(new File(filePath));
-				if (listIgnore != Collections.EMPTY_SET)
-					writer.addAllIgnore(listIgnore);
-				if (listKeep != Collections.EMPTY_SET)
-					writer.addAllWriteOnly(listKeep);
-			}
-			catch (IOException x)
-			{
-				throw new RuntimeException("Error opening LCIO file",x);
-			}  
-		}
-		
-		try
-		{
-			writer.reOpen();
+
+    public void setIgnoreCollection(String ignoreCollection)
+    {
+        listIgnore.add(ignoreCollection);
+    }
+
+    public void setWriteOnlyCollection(String writeOnlyCollection)
+    {
+        listKeep.add(writeOnlyCollection);
+    }
+	
+    private void setupWriter()
+    {
+        try 
+        {
+            this.writer = new LCIOWriter(outputFile); 
+        }
+        catch (IOException x)
+        {
+            throw new RuntimeException("Error creating writer",x);
+        }
+        writer.addAllIgnore(listIgnore);
+        writer.addAllWriteOnly(listKeep);
+        
+        try 
+        {
+            writer.reOpen();
 		}
 		catch (IOException x)
 		{
 			throw new RuntimeException("Error rewinding LCIO file",x);
 		}
+    }
+	
+	protected void startOfData()
+	{
+        setupWriter();
 	}
 	
 	protected void endOfData()
CVSspam 0.2.8