NoteMap implements Iterable<Note>

We will need to iterate over all notes of a NoteMap, at least this will be
needed for testing purposes. This change also implied making the Note class
public.

Change-Id: I9b0639f9843f457ee9de43504b2499a673cd0e77
Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com>
This commit is contained in:
Sasa Zivkov 2011-01-03 16:03:49 +01:00
parent b7f887f120
commit 7cd812940d
3 changed files with 105 additions and 3 deletions

View File

@ -52,6 +52,7 @@
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.CommitBuilder;
@ -444,6 +445,83 @@ public void testRemoveDeletesTreeFanout2_38() throws Exception {
assertEquals("empty tree", empty, n.getTree()); assertEquals("empty tree", empty, n.getTree());
} }
public void testIteratorEmptyMap() {
Iterator<Note> it = NoteMap.newEmptyMap().iterator();
assertFalse(it.hasNext());
}
public void testIteratorFlatTree() throws Exception {
RevBlob a = tr.blob("a");
RevBlob b = tr.blob("b");
RevBlob data1 = tr.blob("data1");
RevBlob data2 = tr.blob("data2");
RevBlob nonNote = tr.blob("non note");
RevCommit r = tr.commit() //
.add(a.name(), data1) //
.add(b.name(), data2) //
.add("nonNote", nonNote) //
.create();
tr.parseBody(r);
Iterator it = NoteMap.read(reader, r).iterator();
assertEquals(2, count(it));
}
public void testIteratorFanoutTree2_38() throws Exception {
RevBlob a = tr.blob("a");
RevBlob b = tr.blob("b");
RevBlob data1 = tr.blob("data1");
RevBlob data2 = tr.blob("data2");
RevBlob nonNote = tr.blob("non note");
RevCommit r = tr.commit() //
.add(fanout(2, a.name()), data1) //
.add(fanout(2, b.name()), data2) //
.add("nonNote", nonNote) //
.create();
tr.parseBody(r);
Iterator it = NoteMap.read(reader, r).iterator();
assertEquals(2, count(it));
}
public void testIteratorFanoutTree2_2_36() throws Exception {
RevBlob a = tr.blob("a");
RevBlob b = tr.blob("b");
RevBlob data1 = tr.blob("data1");
RevBlob data2 = tr.blob("data2");
RevBlob nonNote = tr.blob("non note");
RevCommit r = tr.commit() //
.add(fanout(4, a.name()), data1) //
.add(fanout(4, b.name()), data2) //
.add("nonNote", nonNote) //
.create();
tr.parseBody(r);
Iterator it = NoteMap.read(reader, r).iterator();
assertEquals(2, count(it));
}
public void testIteratorFullyFannedOut() throws Exception {
RevBlob a = tr.blob("a");
RevBlob b = tr.blob("b");
RevBlob data1 = tr.blob("data1");
RevBlob data2 = tr.blob("data2");
RevBlob nonNote = tr.blob("non note");
RevCommit r = tr.commit() //
.add(fanout(38, a.name()), data1) //
.add(fanout(38, b.name()), data2) //
.add("nonNote", nonNote) //
.create();
tr.parseBody(r);
Iterator it = NoteMap.read(reader, r).iterator();
assertEquals(2, count(it));
}
private RevCommit commitNoteMap(NoteMap map) throws IOException { private RevCommit commitNoteMap(NoteMap map) throws IOException {
tr.tick(600); tr.tick(600);
@ -469,4 +547,13 @@ private static String fanout(int prefix, String name) {
} }
return r.toString(); return r.toString();
} }
private static int count(Iterator it) {
int c = 0;
while (it.hasNext()) {
c++;
it.next();
}
return c;
}
} }

View File

@ -47,7 +47,7 @@
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
/** In-memory representation of a single note attached to one object. */ /** In-memory representation of a single note attached to one object. */
class Note extends ObjectId { public class Note extends ObjectId {
private ObjectId data; private ObjectId data;
/** /**
@ -63,7 +63,8 @@ class Note extends ObjectId {
data = noteData; data = noteData;
} }
ObjectId getData() { /** @return the note content */
public ObjectId getData() {
return data; return data;
} }

View File

@ -44,6 +44,7 @@
package org.eclipse.jgit.notes; package org.eclipse.jgit.notes;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.IncorrectObjectTypeException;
@ -52,6 +53,7 @@
import org.eclipse.jgit.lib.AbbreviatedObjectId; import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.ObjectReader;
@ -66,7 +68,7 @@
* is not released by this class. The caller should arrange for releasing the * is not released by this class. The caller should arrange for releasing the
* shared {@code ObjectReader} at the proper times. * shared {@code ObjectReader} at the proper times.
*/ */
public class NoteMap { public class NoteMap implements Iterable<Note> {
/** /**
* Construct a new empty note map. * Construct a new empty note map.
* *
@ -165,6 +167,18 @@ private NoteMap(ObjectReader reader) {
this.reader = reader; this.reader = reader;
} }
/**
* @return an iterator that iterates over notes of this NoteMap. Non note
* entries are ignored by this iterator.
*/
public Iterator<Note> iterator() {
try {
return root.iterator(new MutableObjectId(), reader);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/** /**
* Lookup a note for a specific ObjectId. * Lookup a note for a specific ObjectId.
* *