@@ -92,6 +92,49 @@ public boolean addAll(final Collection<? extends E> c) {
9292 return changed ;
9393 }
9494
95+ // -- Internal methods --
96+
97+ @ Override
98+ protected boolean addToList (final E obj , final List <E > list ,
99+ final boolean batch )
100+ {
101+ if (batch ) {
102+ // adding multiple values; append to end of list, and sort afterward
103+ return super .addToList (obj , list , batch );
104+ }
105+
106+ // search for the correct location to insert the object
107+ final int result = Collections .binarySearch (list , obj );
108+ // NB: The objects' natural ordering may not be consistent with equals.
109+ // Hence, the index reported may indicate a match with an unequal object
110+ // (i.e., obj.compareTo(match) == 0 but !obj.equals(match)).
111+ // But since we allow duplicate items in the index, this situation is fine;
112+ // either way, we want to insert the object at the given point.
113+ final int index = result < 0 ? -result - 1 : result ;
114+
115+ // insert object at the appropriate location
116+ list .add (index , obj );
117+ return true ;
118+ }
119+
120+ // -- Helper methods --
121+
122+ private void sort () {
123+ for (final List <E > list : hoard .values ()) {
124+ Collections .sort (list );
125+ }
126+ }
127+
128+ private int findInList (final Object o , final List <E > list ) {
129+ if (!getBaseClass ().isAssignableFrom (o .getClass ())) {
130+ // wrong type
131+ return list .size ();
132+ }
133+ @ SuppressWarnings ("unchecked" )
134+ final E typedObj = (E ) o ;
135+ return Collections .binarySearch (list , typedObj );
136+ }
137+
95138 private void mergeAfterSorting (final Collection <? extends E > c ) {
96139 final List <E > listToMerge = new ArrayList <>(c );
97140 Collections .sort (listToMerge );
@@ -145,47 +188,4 @@ private void mergeInto(final List<? extends E> sorted, final List<E> into) {
145188 }
146189 }
147190
148- // -- Internal methods --
149-
150- @ Override
151- protected boolean addToList (final E obj , final List <E > list ,
152- final boolean batch )
153- {
154- if (batch ) {
155- // adding multiple values; append to end of list, and sort afterward
156- return super .addToList (obj , list , batch );
157- }
158-
159- // search for the correct location to insert the object
160- final int result = Collections .binarySearch (list , obj );
161- // NB: The objects' natural ordering may not be consistent with equals.
162- // Hence, the index reported may indicate a match with an unequal object
163- // (i.e., obj.compareTo(match) == 0 but !obj.equals(match)).
164- // But since we allow duplicate items in the index, this situation is fine;
165- // either way, we want to insert the object at the given point.
166- final int index = result < 0 ? -result - 1 : result ;
167-
168- // insert object at the appropriate location
169- list .add (index , obj );
170- return true ;
171- }
172-
173- // -- Helper methods --
174-
175- private void sort () {
176- for (final List <E > list : hoard .values ()) {
177- Collections .sort (list );
178- }
179- }
180-
181- private int findInList (final Object o , final List <E > list ) {
182- if (!getBaseClass ().isAssignableFrom (o .getClass ())) {
183- // wrong type
184- return list .size ();
185- }
186- @ SuppressWarnings ("unchecked" )
187- final E typedObj = (E ) o ;
188- return Collections .binarySearch (list , typedObj );
189- }
190-
191191}
0 commit comments