Refactor: Make retriveCompressed an method of the Bitmap class

Make retrieveCompressed() a method of Bitmap interface to avoid type
casting and later reuse in improving the memory footprint of GC's bitmap
generation phase.

Change-Id: I098d85105cf17af845d43b8c71b4ca48b02fd7da
Signed-off-by: Yunjie Li <yunjieli@google.com>
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/BitmapIndexImpl.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/BitmapIndexImpl.java
index 6aa1a0e..0d3a2b4 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/BitmapIndexImpl.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/BitmapIndexImpl.java
@@ -252,6 +252,11 @@
 			return bitmapIndex;
 		}
 
+		@Override
+		public EWAHCompressedBitmap retrieveCompressed() {
+			return build().retrieveCompressed();
+		}
+
 		private EWAHCompressedBitmap ewahBitmap(Bitmap other) {
 			if (other instanceof CompressedBitmap) {
 				CompressedBitmap b = (CompressedBitmap) other;
@@ -372,7 +377,8 @@
 			};
 		}
 
-		EWAHCompressedBitmap getEwahCompressedBitmap() {
+		@Override
+		public EWAHCompressedBitmap retrieveCompressed() {
 			return bitmap;
 		}
 
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexBuilder.java
index 9538cc5..d87b699 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexBuilder.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexBuilder.java
@@ -17,11 +17,9 @@
 import java.util.NoSuchElementException;
 
 import org.eclipse.jgit.internal.JGitText;
-import org.eclipse.jgit.internal.storage.file.BitmapIndexImpl.CompressedBitmap;
 import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
 import org.eclipse.jgit.lib.AnyObjectId;
 import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
-import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectIdOwnerMap;
@@ -134,16 +132,7 @@
 	 *            the flags to be stored with the bitmap
 	 */
 	public void addBitmap(AnyObjectId objectId, Bitmap bitmap, int flags) {
-		if (bitmap instanceof BitmapBuilder)
-			bitmap = ((BitmapBuilder) bitmap).build();
-
-		EWAHCompressedBitmap compressed;
-		if (bitmap instanceof CompressedBitmap)
-			compressed = ((CompressedBitmap) bitmap).getEwahCompressedBitmap();
-		else
-			throw new IllegalArgumentException(bitmap.getClass().toString());
-
-		addBitmap(objectId, compressed, flags);
+		addBitmap(objectId, bitmap.retrieveCompressed(), flags);
 	}
 
 	/**
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BitmapIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BitmapIndex.java
index f61286d..f6695bd 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BitmapIndex.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BitmapIndex.java
@@ -14,6 +14,8 @@
 
 import org.eclipse.jgit.internal.storage.file.PackBitmapIndex;
 
+import com.googlecode.javaewah.EWAHCompressedBitmap;
+
 /**
  * A compressed bitmap representation of the entire object graph.
  *
@@ -81,6 +83,14 @@
 		 */
 		@Override
 		Iterator<BitmapObject> iterator();
+
+		/**
+		 * Returns the corresponding raw compressed EWAH bitmap of the bitmap.
+		 * 
+		 * @return the corresponding {@code EWAHCompressedBitmap}
+		 * @since 5.8
+		 */
+		EWAHCompressedBitmap retrieveCompressed();
 	}
 
 	/**