2020import java .util .Map ;
2121
2222/**
23- * <p>Efficiently maps each integer in a set to another integer in a set, useful for merging bulk write errors when a bulk write must be
24- * split into multiple batches. Has the ability to switch from a range-based to a hash-based map depending on the mappings that have
23+ * <p>Efficiently maps each integer in a set to another integer in a set, useful for merging bulk write errors when a bulk write must be
24+ * split into multiple batches. Has the ability to switch from a range-based to a hash-based map depending on the mappings that have
2525 * been added.</p>
2626 *
27- * <p>This class is not part of the public API.</p>
27+ * <p>This class should not be considered a part of the public API.</p>
2828 */
29- abstract class IndexMap {
29+ public abstract class IndexMap {
3030
3131 /**
3232 * Create an empty index map.
33+ *
34+ * @return a new index map
3335 */
3436 static IndexMap create () {
3537 return new RangeBased ();
3638 }
3739
3840 /**
3941 * Create an index map that maps the integers 0..count to startIndex..startIndex + count.
42+ *
43+ * @param startIndex the start index
44+ * @param count the count
45+ * @return an index map
4046 */
4147 static IndexMap create (final int startIndex , final int count ) {
4248 return new RangeBased (startIndex , count );
4349 }
4450
51+ /**
52+ * Add a new index to the map
53+ *
54+ * @param index the index
55+ * @param originalIndex the original index
56+ * @return an index map with this index added to it
57+ */
4558 abstract IndexMap add (int index , int originalIndex );
4659
60+ /**
61+ * Return the index that the specified index is mapped to.
62+ *
63+ * @param index the index
64+ * @return the index it's mapped to
65+ */
4766 abstract int map (int index );
4867
4968 private static class HashBased extends IndexMap {
5069 private final Map <Integer , Integer > indexMap = new HashMap <Integer , Integer >();
5170
52- public HashBased (int startIndex , int count ) {
53- for (int i = startIndex ; i <= count ; i ++) {
71+ public HashBased (final int startIndex , final int count ) {
72+ for (int i = startIndex ; i < startIndex + count ; i ++) {
5473 indexMap .put (i - startIndex , i );
5574 }
5675 }
@@ -79,6 +98,12 @@ public RangeBased() {
7998 }
8099
81100 public RangeBased (final int startIndex , final int count ) {
101+ if (startIndex < 0 ) {
102+ throw new IllegalArgumentException ("startIndex must be more than 0" );
103+ }
104+ if (count < 0 ) {
105+ throw new IllegalArgumentException ("count must be more than 0" );
106+ }
82107 this .startIndex = startIndex ;
83108 this .count = count ;
84109 }
@@ -101,8 +126,10 @@ public IndexMap add(final int index, final int originalIndex) {
101126
102127 @ Override
103128 public int map (final int index ) {
104- if (index >= count ) {
105- throw new MongoInternalException ("index should not be greater than count" );
129+ if (index < 0 ) {
130+ throw new MongoInternalException ("no mapping found for index " + index );
131+ } else if (index >= count ) {
132+ throw new MongoInternalException ("index should not be greater than or equal to count" );
106133 }
107134 return startIndex + index ;
108135 }
0 commit comments