From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 3/6] Add diff.EditList to provide for a list of Edit instances
Date: Fri, 1 May 2009 19:08:44 -0700 [thread overview]
Message-ID: <1241230127-28279-4-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1241230127-28279-3-git-send-email-spearce@spearce.org>
Often a file difference involves more than one modified region, in
which case we need more than one Edit instance to describe the full
change that was made.
List<Edit> could be used with any generic List implementation, but
we may want to offer additional API functionality that is Edit list
specific, so I'm creating a specialized List implementation for it.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
.../tst/org/spearce/jgit/diff/EditListTest.java | 121 ++++++++++++++++++++
.../src/org/spearce/jgit/diff/EditList.java | 93 +++++++++++++++
2 files changed, 214 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java
new file mode 100644
index 0000000..54d5007
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/diff/EditListTest.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2009, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.diff;
+
+import java.util.Iterator;
+
+import junit.framework.TestCase;
+
+public class EditListTest extends TestCase {
+ public void testEmpty() {
+ final EditList l = new EditList();
+ assertEquals(0, l.size());
+ assertTrue(l.isEmpty());
+ assertEquals("EditList[]", l.toString());
+
+ assertTrue(l.equals(l));
+ assertTrue(l.equals(new EditList()));
+ assertFalse(l.equals(""));
+ assertEquals(l.hashCode(), new EditList().hashCode());
+ }
+
+ public void testAddOne() {
+ final Edit e = new Edit(1, 2, 1, 1);
+ final EditList l = new EditList();
+ l.add(e);
+ assertEquals(1, l.size());
+ assertFalse(l.isEmpty());
+ assertSame(e, l.get(0));
+ assertSame(e, l.iterator().next());
+
+ assertTrue(l.equals(l));
+ assertFalse(l.equals(new EditList()));
+
+ final EditList l2 = new EditList();
+ l2.add(e);
+ assertTrue(l.equals(l2));
+ assertTrue(l2.equals(l));
+ assertEquals(l.hashCode(), l2.hashCode());
+ }
+
+ public void testAddTwo() {
+ final Edit e1 = new Edit(1, 2, 1, 1);
+ final Edit e2 = new Edit(8, 8, 8, 12);
+ final EditList l = new EditList();
+ l.add(e1);
+ l.add(e2);
+ assertEquals(2, l.size());
+ assertSame(e1, l.get(0));
+ assertSame(e2, l.get(1));
+
+ final Iterator<Edit> i = l.iterator();
+ assertSame(e1, i.next());
+ assertSame(e2, i.next());
+
+ assertTrue(l.equals(l));
+ assertFalse(l.equals(new EditList()));
+
+ final EditList l2 = new EditList();
+ l2.add(e1);
+ l2.add(e2);
+ assertTrue(l.equals(l2));
+ assertTrue(l2.equals(l));
+ assertEquals(l.hashCode(), l2.hashCode());
+ }
+
+ public void testSet() {
+ final Edit e1 = new Edit(1, 2, 1, 1);
+ final Edit e2 = new Edit(3, 4, 3, 3);
+ final EditList l = new EditList();
+ l.add(e1);
+ assertSame(e1, l.get(0));
+ assertSame(e1, l.set(0, e2));
+ assertSame(e2, l.get(0));
+ }
+
+ public void testRemove() {
+ final Edit e1 = new Edit(1, 2, 1, 1);
+ final Edit e2 = new Edit(8, 8, 8, 12);
+ final EditList l = new EditList();
+ l.add(e1);
+ l.add(e2);
+ l.remove(e1);
+ assertEquals(1, l.size());
+ assertSame(e2, l.get(0));
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java b/org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java
new file mode 100644
index 0000000..0ccf366
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/diff/EditList.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2009, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.diff;
+
+import java.util.AbstractList;
+import java.util.ArrayList;
+
+/** Specialized list of {@link Edit}s in a document. */
+public class EditList extends AbstractList<Edit> {
+ private final ArrayList<Edit> container;
+
+ /** Create a new, empty edit list. */
+ public EditList() {
+ container = new ArrayList<Edit>();
+ }
+
+ @Override
+ public int size() {
+ return container.size();
+ }
+
+ @Override
+ public Edit get(final int index) {
+ return container.get(index);
+ }
+
+ @Override
+ public Edit set(final int index, final Edit element) {
+ return container.set(index, element);
+ }
+
+ @Override
+ public void add(final int index, final Edit element) {
+ container.add(index, element);
+ }
+
+ @Override
+ public Edit remove(final int index) {
+ return container.remove(index);
+ }
+
+ @Override
+ public int hashCode() {
+ return container.hashCode();
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (o instanceof EditList)
+ return container.equals(((EditList) o).container);
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return "EditList" + container.toString();
+ }
+}
--
1.6.3.rc3.212.g8c698
next prev parent reply other threads:[~2009-05-02 2:09 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-02 2:08 [JGIT PATCH 0/6] Diff processing utilities Shawn O. Pearce
2009-05-02 2:08 ` [JGIT PATCH 1/6] Add set to IntList Shawn O. Pearce
2009-05-02 2:08 ` [JGIT PATCH 2/6] Add diff.Edit to describe an edit region within a file Shawn O. Pearce
2009-05-02 2:08 ` Shawn O. Pearce [this message]
2009-05-02 2:08 ` [JGIT PATCH 4/6] Add diff.RawText to index a file content for later compares Shawn O. Pearce
2009-05-02 2:08 ` [JGIT PATCH 5/6] Teach FileHeader, HunkHeader how to create an EditList Shawn O. Pearce
2009-05-02 2:08 ` [JGIT PATCH 6/6] Add diff.DiffFormatter to create Git style unified patch scripts Shawn O. Pearce
2009-05-03 0:03 ` [JGIT PATCH v2 " Shawn O. Pearce
2009-05-03 0:29 ` [JGIT PATCH v3 " Shawn O. Pearce
2009-05-03 7:07 ` [JGIT PATCH 1/6] Add set to IntList Robin Rosenberg
2009-05-04 14:22 ` Shawn O. Pearce
2009-05-04 14:50 ` Johannes Schindelin
2009-05-04 14:55 ` Shawn O. Pearce
2009-05-04 15:10 ` Johannes Schindelin
2009-05-03 14:24 ` [JGIT PATCH 0/6] Diff processing utilities Johannes Schindelin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1241230127-28279-4-git-send-email-spearce@spearce.org \
--to=spearce@spearce.org \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=robin.rosenberg@dewire.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).