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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Florian Köberle <florianskarten@xxxxxx>
---
 .../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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux