/** * Add a value to a sorted array. Does not check for duplicates. * * @param instance the instance holding the field * @param value the value to add * @param comparator a comparator, or {@code null} to use natural ordering */ public void add(T instance, V value, Comparator<? super V> comparator) { final AtomicReferenceFieldUpdater<T, V[]> updater = this.updater; for (;;) { final V[] oldVal = updater.get(instance); final int oldLen = oldVal.length; final int pos = insertionPoint(Arrays.binarySearch(oldVal, value, comparator)); final V[] newVal = Arrays.copyOf(oldVal, oldLen + 1); newVal[pos] = value; System.arraycopy(oldVal, pos, newVal, pos + 1, oldLen - pos); if (updater.compareAndSet(instance, oldVal, newVal)) { return; } } }
/** * Add a value to a sorted array. Does not check for duplicates. * * @param instance the instance holding the field * @param value the value to add * @param comparator a comparator, or {@code null} to use natural ordering */ public void add(T instance, V value, Comparator<? super V> comparator) { final AtomicReferenceFieldUpdater<T, V[]> updater = this.updater; for (;;) { final V[] oldVal = updater.get(instance); final int oldLen = oldVal.length; final int pos = insertionPoint(Arrays.binarySearch(oldVal, value, comparator)); final V[] newVal = Arrays.copyOf(oldVal, oldLen + 1); newVal[pos] = value; System.arraycopy(oldVal, pos, newVal, pos + 1, oldLen - pos); if (updater.compareAndSet(instance, oldVal, newVal)) { return; } } }
/** * Add a value to a sorted array. Does not check for duplicates. * * @param instance the instance holding the field * @param value the value to add * @param comparator a comparator, or {@code null} to use natural ordering */ public void add(T instance, V value, Comparator<? super V> comparator) { final AtomicReferenceFieldUpdater<T, V[]> updater = this.updater; for (;;) { final V[] oldVal = updater.get(instance); final int oldLen = oldVal.length; final int pos = insertionPoint(Arrays.binarySearch(oldVal, value, comparator)); final V[] newVal = Arrays.copyOf(oldVal, oldLen + 1); newVal[pos] = value; System.arraycopy(oldVal, pos, newVal, pos + 1, oldLen - pos); if (updater.compareAndSet(instance, oldVal, newVal)) { return; } } }