changeset 480:f3fab7a20841

Refactor LineReader utility as stanalone class to facilitate reuse
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 17 Jul 2012 22:14:21 +0200
parents 59b7c817bc4d
children a458f9fb00ce
files src/org/tmatesoft/hg/internal/LineReader.java src/org/tmatesoft/hg/repo/ext/MqManager.java
diffstat 2 files changed, 129 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/internal/LineReader.java	Tue Jul 17 22:14:21 2012 +0200
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2012 TMate Software Ltd
+ *  
+ * 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; version 2 of the License.
+ *
+ * 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.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.internal;
+
+import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Collection;
+
+import org.tmatesoft.hg.repo.HgInvalidFileException;
+import org.tmatesoft.hg.repo.ext.MqManager;
+import org.tmatesoft.hg.util.LogFacility;
+
+/**
+ * Handy class to read line-based configuration files
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public final class LineReader {
+		
+		public interface LineConsumer<T> {
+	//		boolean begin(File f, T paramObj) throws IOException;
+			boolean consume(String line, T paramObj) throws IOException;
+	//		boolean end(File f, T paramObj) throws IOException;
+		}
+
+		public static class SimpleLineCollector implements LineReader.LineConsumer<Collection<String>> {
+		
+			public boolean consume(String line, Collection<String> result) throws IOException {
+				result.add(line);
+				return true;
+			}
+		}
+
+		private final File file;
+		private final LogFacility log;
+		private boolean trimLines = true;
+		private boolean skipEmpty = true;
+		private String ignoreThatStarts = null;
+
+		public LineReader(File f, LogFacility logFacility) {
+			file = f;
+			log = logFacility;
+		}
+		
+		/**
+		 * default: <code>true</code>
+		 * <code>false</code> to return line as is
+		 */
+		public LineReader trimLines(boolean trim) {
+			trimLines = trim;
+			return this;
+		}
+		
+		/**
+		 * default: <code>true</code>
+		 * <code>false</code> to pass empty lines to consumer
+		 */
+		public LineReader skipEmpty(boolean skip) {
+			skipEmpty = skip;
+			return this;
+		}
+		
+		/**
+		 * default: doesn't skip any line.
+		 * set e.g. to "#" or "//" to skip lines that start with such prefix
+		 */
+		public LineReader ignoreLineComments(String lineStart) {
+			ignoreThatStarts = lineStart;
+			return this;
+		}
+
+		public <T> void read(LineConsumer<T> consumer, T paramObj) throws HgInvalidFileException {
+			BufferedReader statusFileReader = null;
+			try {
+//				consumer.begin(file, paramObj);
+				statusFileReader = new BufferedReader(new FileReader(file));
+				String line;
+				boolean ok = true;
+				while (ok && (line = statusFileReader.readLine()) != null) {
+					if (trimLines) {
+						line = line.trim();
+					}
+					if (ignoreThatStarts != null && line.startsWith(ignoreThatStarts)) {
+						continue;
+					}
+					if (!skipEmpty || line.length() > 0) {
+						ok = consumer.consume(line, paramObj);
+					}
+				}
+			} catch (IOException ex) {
+				throw new HgInvalidFileException(ex.getMessage(), ex, file);
+			} finally {
+				try {
+					statusFileReader.close();
+				} catch (IOException ex) {
+					log.dump(MqManager.class, Warn, ex, null);
+				}
+//				try {
+//					consumer.end(file, paramObj);
+//				} catch (IOException ex) {
+//					log.warn(MqManager.class, ex, null);
+//				}
+			}
+		}
+	}
\ No newline at end of file
--- a/src/org/tmatesoft/hg/repo/ext/MqManager.java	Thu Jul 12 20:02:49 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/ext/MqManager.java	Tue Jul 17 22:14:21 2012 +0200
@@ -18,12 +18,9 @@
 
 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -31,6 +28,7 @@
 import java.util.Map;
 
 import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.internal.LineReader;
 import org.tmatesoft.hg.repo.HgInternals;
 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
 import org.tmatesoft.hg.repo.HgInvalidFileException;
@@ -71,14 +69,14 @@
 			File queues = new File(repoDir, "patches.queues");
 			if (queues.isFile()) {
 				LineReader lr = new LineReader(queues, log).trimLines(true).skipEmpty(true);
-				lr.read(new SimpleLineCollector(), queueNames = new LinkedList<String>());
+				lr.read(new LineReader.SimpleLineCollector(), queueNames = new LinkedList<String>());
 			}
 			final String queueLocation; // path under .hg to patch queue information (status, series and diff files)
 			File activeQueueFile = new File(repoDir, "patches.queue");
 			// file is there only if it's not default queue ('patches') that is active
 			if (activeQueueFile.isFile()) {
 				ArrayList<String> contents = new ArrayList<String>();
-				new LineReader(activeQueueFile, log).read(new SimpleLineCollector(), contents);
+				new LineReader(activeQueueFile, log).read(new LineReader.SimpleLineCollector(), contents);
 				if (contents.isEmpty()) {
 					log.dump(getClass(), Warn, "File %s with active queue name is empty", activeQueueFile.getName());
 					activeQueue = PATCHES_DIR;
@@ -104,7 +102,7 @@
 			final File fileStatus = new File(repoDir, queueLocation + "status");
 			final File fileSeries = new File(repoDir, queueLocation + "series");
 			if (fileStatus.isFile()) {
-				new LineReader(fileStatus, log).read(new LineConsumer<List<PatchRecord>>() {
+				new LineReader(fileStatus, log).read(new LineReader.LineConsumer<List<PatchRecord>>() {
 	
 					public boolean consume(String line, List<PatchRecord> result) throws IOException {
 						int sep = line.indexOf(':');
@@ -125,7 +123,7 @@
 					name2patch.put(pr.getName(), pr);
 				}
 				LinkedList<String> knownPatchNames = new LinkedList<String>();
-				new LineReader(fileSeries, log).read(new SimpleLineCollector(), knownPatchNames);
+				new LineReader(fileSeries, log).read(new LineReader.SimpleLineCollector(), knownPatchNames);
 				// XXX read other queues?
 				allKnown = new ArrayList<PatchRecord>(knownPatchNames.size());
 				for (String name : knownPatchNames) {
@@ -143,14 +141,6 @@
 		}
 	}
 	
-	static class SimpleLineCollector implements LineConsumer<Collection<String>> {
-
-		public boolean consume(String line, Collection<String> result) throws IOException {
-			result.add(line);
-			return true;
-		}
-	}
-	
 	/**
 	 * Number of patches not yet applied
 	 * @return positive value when there are 
@@ -246,87 +236,4 @@
 			return String.format(fmt, name, ni, location);
 		}
 	}
-
-	// TODO refine API and extract into separate classes
-
-	interface LineConsumer<T> {
-//		boolean begin(File f, T paramObj) throws IOException;
-		boolean consume(String line, T paramObj) throws IOException;
-//		boolean end(File f, T paramObj) throws IOException;
-	}
-	
-	class LineReader {
-		
-		private final File file;
-		private final LogFacility log;
-		private boolean trimLines = true;
-		private boolean skipEmpty = true;
-		private String ignoreThatStars = null;
-
-		LineReader(File f, LogFacility logFacility) {
-			file = f;
-			log = logFacility;
-		}
-		
-		/**
-		 * default: <code>true</code>
-		 * <code>false</code> to return line as is
-		 */
-		LineReader trimLines(boolean trim) {
-			trimLines = trim;
-			return this;
-		}
-		
-		/**
-		 * default: <code>true</code>
-		 * <code>false</code> to pass empty lines to consumer
-		 */
-		LineReader skipEmpty(boolean skip) {
-			skipEmpty = skip;
-			return this;
-		}
-		
-		/**
-		 * default: doesn't skip any line.
-		 * set e.g. to "#" or "//" to skip lines that start with such prefix
-		 */
-		LineReader ignoreLineComments(String lineStart) {
-			ignoreThatStars = lineStart;
-			return this;
-		}
-
-		<T> void read(LineConsumer<T> consumer, T paramObj) throws HgInvalidFileException {
-			BufferedReader statusFileReader = null;
-			try {
-//				consumer.begin(file, paramObj);
-				statusFileReader = new BufferedReader(new FileReader(file));
-				String line;
-				boolean ok = true;
-				while (ok && (line = statusFileReader.readLine()) != null) {
-					if (trimLines) {
-						line = line.trim();
-					}
-					if (ignoreThatStars != null && line.startsWith(ignoreThatStars)) {
-						continue;
-					}
-					if (!skipEmpty || line.length() > 0) {
-						ok = consumer.consume(line, paramObj);
-					}
-				}
-			} catch (IOException ex) {
-				throw new HgInvalidFileException(ex.getMessage(), ex, file);
-			} finally {
-				try {
-					statusFileReader.close();
-				} catch (IOException ex) {
-					log.dump(MqManager.class, Warn, ex, null);
-				}
-//				try {
-//					consumer.end(file, paramObj);
-//				} catch (IOException ex) {
-//					log.warn(MqManager.class, ex, null);
-//				}
-			}
-		}
-	}
 }