20
20
import java .util .Map ;
21
21
22
22
/**
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
25
25
* been added.</p>
26
26
*
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>
28
28
*/
29
- abstract class IndexMap {
29
+ public abstract class IndexMap {
30
30
31
31
/**
32
32
* Create an empty index map.
33
+ *
34
+ * @return a new index map
33
35
*/
34
36
static IndexMap create () {
35
37
return new RangeBased ();
36
38
}
37
39
38
40
/**
39
41
* 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
40
46
*/
41
47
static IndexMap create (final int startIndex , final int count ) {
42
48
return new RangeBased (startIndex , count );
43
49
}
44
50
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
+ */
45
58
abstract IndexMap add (int index , int originalIndex );
46
59
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
+ */
47
66
abstract int map (int index );
48
67
49
68
private static class HashBased extends IndexMap {
50
69
private final Map <Integer , Integer > indexMap = new HashMap <Integer , Integer >();
51
70
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 ++) {
54
73
indexMap .put (i - startIndex , i );
55
74
}
56
75
}
@@ -79,6 +98,12 @@ public RangeBased() {
79
98
}
80
99
81
100
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
+ }
82
107
this .startIndex = startIndex ;
83
108
this .count = count ;
84
109
}
@@ -101,8 +126,10 @@ public IndexMap add(final int index, final int originalIndex) {
101
126
102
127
@ Override
103
128
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" );
106
133
}
107
134
return startIndex + index ;
108
135
}
0 commit comments