5858// a subclass to aid testability of the core logic.
5959class SmallSortedMap <K extends Comparable <K >, V > extends AbstractMap <K , V > {
6060
61+ static final int DEFAULT_FIELD_MAP_ARRAY_SIZE = 16 ;
62+
6163 /**
6264 * Creates a new instance for mapping FieldDescriptors to their values. The {@link
6365 * #makeImmutable()} implementation will convert the List values of any repeated fields to
@@ -67,22 +69,22 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> {
6769 * mappings.
6870 */
6971 static <FieldDescriptorType extends FieldSet .FieldDescriptorLite <FieldDescriptorType >>
70- SmallSortedMap <FieldDescriptorType , Object > newFieldMap (int arraySize ) {
71- return new SmallSortedMap <FieldDescriptorType , Object >(arraySize ) {
72+ SmallSortedMap <FieldDescriptorType , Object > newFieldMap () {
73+ return new SmallSortedMap <FieldDescriptorType , Object >() {
7274 @ Override
7375 @ SuppressWarnings ("unchecked" )
7476 public void makeImmutable () {
7577 if (!isImmutable ()) {
7678 for (int i = 0 ; i < getNumArrayEntries (); i ++) {
7779 final Map .Entry <FieldDescriptorType , Object > entry = getArrayEntryAt (i );
7880 if (entry .getKey ().isRepeated ()) {
79- final List value = (List ) entry .getValue ();
81+ final List <?> value = (List ) entry .getValue ();
8082 entry .setValue (Collections .unmodifiableList (value ));
8183 }
8284 }
8385 for (Map .Entry <FieldDescriptorType , Object > entry : getOverflowEntries ()) {
8486 if (entry .getKey ().isRepeated ()) {
85- final List value = (List ) entry .getValue ();
87+ final List <?> value = (List ) entry .getValue ();
8688 entry .setValue (Collections .unmodifiableList (value ));
8789 }
8890 }
@@ -92,17 +94,11 @@ public void makeImmutable() {
9294 };
9395 }
9496
95- /**
96- * Creates a new instance for testing.
97- *
98- * @param arraySize The size of the entry array containing the lexicographically smallest
99- * mappings.
100- */
101- static <K extends Comparable <K >, V > SmallSortedMap <K , V > newInstanceForTest (int arraySize ) {
102- return new SmallSortedMap <K , V >(arraySize );
97+ /** Creates a new instance for testing. */
98+ static <K extends Comparable <K >, V > SmallSortedMap <K , V > newInstanceForTest () {
99+ return new SmallSortedMap <>();
103100 }
104101
105- private final int maxArraySize ;
106102 // The "entry array" is actually a List because generic arrays are not
107103 // allowed. ArrayList also nicely handles the entry shifting on inserts and
108104 // removes.
@@ -119,8 +115,7 @@ static <K extends Comparable<K>, V> SmallSortedMap<K, V> newInstanceForTest(int
119115 * @code arraySize Size of the array in which the lexicographically smallest mappings are stored.
120116 * (i.e. the {@code k} referred to in the class documentation).
121117 */
122- private SmallSortedMap (int arraySize ) {
123- this .maxArraySize = arraySize ;
118+ private SmallSortedMap () {
124119 this .entryList = Collections .emptyList ();
125120 this .overflowEntries = Collections .emptyMap ();
126121 this .overflowEntriesDescending = Collections .emptyMap ();
@@ -215,14 +210,14 @@ public V put(K key, V value) {
215210 }
216211 ensureEntryArrayMutable ();
217212 final int insertionPoint = -(index + 1 );
218- if (insertionPoint >= maxArraySize ) {
213+ if (insertionPoint >= DEFAULT_FIELD_MAP_ARRAY_SIZE ) {
219214 // Put directly in overflow.
220215 return getOverflowEntriesMutable ().put (key , value );
221216 }
222217 // Insert new Entry in array.
223- if (entryList .size () == maxArraySize ) {
218+ if (entryList .size () == DEFAULT_FIELD_MAP_ARRAY_SIZE ) {
224219 // Shift the last array entry into overflow.
225- final Entry lastEntryInArray = entryList .remove (maxArraySize - 1 );
220+ final Entry lastEntryInArray = entryList .remove (DEFAULT_FIELD_MAP_ARRAY_SIZE - 1 );
226221 getOverflowEntriesMutable ().put (lastEntryInArray .getKey (), lastEntryInArray .getValue ());
227222 }
228223 entryList .add (insertionPoint , new Entry (key , value ));
@@ -357,7 +352,7 @@ private SortedMap<K, V> getOverflowEntriesMutable() {
357352 private void ensureEntryArrayMutable () {
358353 checkMutable ();
359354 if (entryList .isEmpty () && !(entryList instanceof ArrayList )) {
360- entryList = new ArrayList <Entry >( maxArraySize );
355+ entryList = new ArrayList <>( DEFAULT_FIELD_MAP_ARRAY_SIZE );
361356 }
362357 }
363358
0 commit comments