From: Florian Köberle Date: Mon, 23 Jun 2008 17:43:34 GMT Subject: [[JGIT PATCH]] Implementation of a copy constructor for FileNameMatcher. Message-ID: <1214243014-8701-1-git-send-email-florianskarten@web.de> In-Reply-To: <485FDE42.1060106@web.de> Signed-off-by: Florian Köberle --- .../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 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 headsStartValue, + final List heads) { this.headsStartValue = headsStartValue; - this.heads = new ArrayList(headsStartValue.size()); - this.heads.addAll(this.headsStartValue); - this.listForLocalUseage = new ArrayList(headsStartValue.size()); + this.heads = new ArrayList(heads.size()); + this.heads.addAll(heads); + this.listForLocalUseage = new ArrayList(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 other. + * + * @param other + * another {@link FileNameMatcher} instance. + */ + public FileNameMatcher(FileNameMatcher other) { + this(other.headsStartValue, other.heads); + } + private static List createHeadsStartValues( final String patternString, final Character invalidWildgetCharacter) throws InvalidPatternException { -- 1.5.4.3