git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[[JGIT PATCH]] Implementation of a copy constructor for FileNameMatcher.

From
FKFlorian Köberle <florianskarten@web.de>
Date
Jun 23, 2008, 17:43 UTC
Message-ID
<1214243014-8701-1-git-send-email-florianskarten@web.de>
In-Reply-To
<485FDE42.1060106@web.de>
Signed-off-by: Florian Köberle <florianskarten@web.de>
---
 .../spearce/jgit/fnmatch/FileNameMatcherTest.java  |   31 ++++++++++++++++++++
 .../org/spearce/jgit/fnmatch/FileNameMatcher.java  |   30 +++++++++++++++++--
 2 files changed, 58 insertions(+), 3 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
index ad72ac8..0c9501b 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
@@ -723,4 +723,35 @@ public class FileNameMatcherTest extends TestCase {
 		assertEquals(true, childMatcher.isMatch());
 		assertEquals(false, childMatcher.canAppendMatch());
 	}
+
+	public void testCopyConstructor() throws Exception {
+		final String pattern = "helloworld";
+		final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
+		matcher.append("hello");
+		final FileNameMatcher copy = new FileNameMatcher(matcher);
+		assertEquals(false, matcher.isMatch());
+		assertEquals(true, matcher.canAppendMatch());
+		assertEquals(false, copy.isMatch());
+		assertEquals(true, copy.canAppendMatch());
+		matcher.append("world");
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(false, copy.isMatch());
+		assertEquals(true, copy.canAppendMatch());
+		copy.append("world");
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(true, copy.isMatch());
+		assertEquals(false, copy.canAppendMatch());
+		copy.reset();
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(false, copy.isMatch());
+		assertEquals(true, copy.canAppendMatch());
+		copy.append("helloworld");
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(true, copy.isMatch());
+		assertEquals(false, copy.canAppendMatch());
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
index 9ac1875..702f7b3 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
@@ -99,10 +99,23 @@ public class FileNameMatcher {
 	 *            must be a list which will never be modified.
 	 */
 	private FileNameMatcher(final List<Head> headsStartValue) {
+		this(headsStartValue, headsStartValue);
+	}
+
+	/**
+	 *
+	 * @param headsStartValue
+	 *            must be a list which will never be modified.
+	 * @param heads
+	 *            a list which will be cloned and then used as current head
+	 *            list.
+	 */
+	private FileNameMatcher(final List<Head> headsStartValue,
+			final List<Head> heads) {
 		this.headsStartValue = headsStartValue;
-		this.heads = new ArrayList<Head>(headsStartValue.size());
-		this.heads.addAll(this.headsStartValue);
-		this.listForLocalUseage = new ArrayList<Head>(headsStartValue.size());
+		this.heads = new ArrayList<Head>(heads.size());
+		this.heads.addAll(heads);
+		this.listForLocalUseage = new ArrayList<Head>(heads.size());
 	}
 
 	/**
@@ -120,6 +133,17 @@ public class FileNameMatcher {
 		this(createHeadsStartValues(patternString, invalidWildgetCharacter));
 	}
 
+	/**
+	 * A Copy Constructor which creates a new {@link FileNameMatcher} with the
+	 * same state and reset point like <code>other</code>.
+	 *
+	 * @param other
+	 *            another {@link FileNameMatcher} instance.
+	 */
+	public FileNameMatcher(FileNameMatcher other) {
+		this(other.headsStartValue, other.heads);
+	}
+
 	private static List<Head> createHeadsStartValues(
 			final String patternString, final Character invalidWildgetCharacter)
 			throws InvalidPatternException {
-- 
1.5.4.3
Previous: Florian Köberle
Message 5 of 5 in “Create a fnmatch-style pattern TreeFilter”
  1. 1/2 Create a fnmatch-style pattern TreeFilterRobin Rosenberg, Jun 22, 2008
  2. 2/2 LsTree: Enable pattern matching in LsTreeRobin Rosenberg, Jun 22, 2008
  3. Shawn O. PearceJun 23, 2008
  4. Florian KöberleJun 23, 2008
  5. ] Implementation of a copy constructor for FileNameMatcher.Florian Köberle, Jun 23, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.