Lines Matching refs:element

33  * the least element may be swapped out for a new element at any time. Elements
36 * This class enforces the invariant that a new element can always be swapped
37 * in. Thus, requests to pin an element for a particular task may be denied if
48 * Called if the buffer is under-capacity and a new element is being
51 * @return the new element to add.
56 * Called if the buffer is full and an old element must be swapped out
57 * to make room for the new element.
59 * @param oldElement the element being removed from the buffer.
60 * @return the new element to add.
65 * Called if the buffer already has an element with the specified key.
66 * Note that the element may currently be pinned for processing by other
70 * @param existingElement the element to be updated.
75 * Returns the key of the element that the ring buffer should prefer
77 * unpinned element then ring buffer will replace the element with least
80 * @return a key of an existing unpinned element or a negative value.
86 * Callback for selecting an element to pin. See
91 * @param element The element to select or not select.
92 * @return true if the element should be selected, false otherwise.
94 public boolean select(E element);
99 * Invoked whenever the ability to pin an element for processing
115 /** Reference-counting for the number of tasks holding this element. */
118 public Pinnable(E element) {
119 mElement = element;
172 /** This must be acquired while an element is pinned. */
194 // element available to swap out as the head of the buffer.
203 * element changes.
213 * Places a new element in the ring buffer, removing the least (by key)
214 * non-pinned element if necessary. The existing element (or {@code null} if
218 * may modify the element in-place. See {@link SwapTask}. <br>
222 * @param newKey the key with which to store the swapped-in element.
224 * @return true if the swap was successful and the new element was saved to
225 * the buffer, false if the swap was not possible and the element
246 // If we are under capacity, insert the new element and return.
254 // Add the new element and release another permit to pin
255 // allow pinning another element.
269 // attempting to remove more than one unpinned element at a
295 // We can get here if no unpinned element was found.
301 // We must remove the element from both mElements and
325 * Attempts to pin the element with the given key and return it. <br>
329 * @return the key and object of the pinned element, if one could be pinned,
353 // If the element is already pinned by another task, simply
357 // We must ensure that there will still be an unpinned element
384 Pinnable<E> element = mElements.get(key);
386 if (element == null) {
391 if (!element.isPinned()) {
392 throw new IllegalArgumentException("Calling release() with unpinned element.");
395 // Unpin the element
396 element.mPins--;
398 if (!element.isPinned()) {
399 // If there are now 0 tasks pinning this element...
400 mUnpinnedElements.put(key, element);
402 // Allow pinning another element.
413 * Attempts to pin the greatest element and return it. <br>
414 * Note that, if a non-null element is returned, the caller <em>must</em>
415 * call {@link #release} with the element. Furthermore, behavior is
416 * undefined if the element's {@code compareTo} behavior changes between
419 * @return the key and object of the pinned element, if one could be pinned,
437 * Attempts to pin the greatest element for which {@code selector} returns
459 // Pin each element, from greatest key to least, until we find the one
460 // we want (the element with the greatest key for which
509 for (Pinnable<E> element : mElements.values()) {
510 task.run(element.mElement);
521 * Attempts to get a pinned element for the given key.
523 * @param key the key of the pinned element.
531 for (java.util.Map.Entry<Long, Pinnable<E>> element : mElements.entrySet()) {
532 if (element.getKey() == key) {
533 if (element.getValue().isPinned()) {
534 return Pair.create(element.getKey(), element.getValue().getElement());
579 * Releases a pinned element for the given key.
581 * If element is unpinned, it is not released.
583 * @param key the key of the element, if the element is not present an
588 Pinnable<E> element = mElements.get(key);
590 if (element == null) {
594 if (element.isPinned()) {
603 * Note: it only calls {@link #release(long)} only once on a pinned element.