All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.benmanes.caffeine.cache.WriteOrderDeque Maven / Gradle / Ivy

/*
 * Copyright 2014 Ben Manes. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.benmanes.caffeine.cache;

import java.util.Deque;

import org.checkerframework.checker.nullness.qual.Nullable;

import com.github.benmanes.caffeine.cache.WriteOrderDeque.WriteOrder;

/**
 * A linked deque implementation used to represent a write-order queue.
 *
 * @author [email protected] (Ben Manes)
 * @param  the type of elements held in this collection
 */
final class WriteOrderDeque> extends AbstractLinkedDeque {

  @Override
  public boolean contains(Object o) {
    return (o instanceof WriteOrder) && contains((WriteOrder) o);
  }

  // A fast-path containment check
  boolean contains(WriteOrder e) {
    return (e.getPreviousInWriteOrder() != null)
        || (e.getNextInWriteOrder() != null)
        || (e == first);
  }

  @Override
  @SuppressWarnings("unchecked")
  public boolean remove(Object o) {
    return (o instanceof WriteOrder) && remove((E) o);
  }

  // A fast-path removal
  public boolean remove(E e) {
    if (contains(e)) {
      unlink(e);
      return true;
    }
    return false;
  }

  @Override
  public @Nullable E getPrevious(E e) {
    return e.getPreviousInWriteOrder();
  }

  @Override
  public void setPrevious(E e, @Nullable E prev) {
    e.setPreviousInWriteOrder(prev);
  }

  @Override
  public @Nullable E getNext(E e) {
    return e.getNextInWriteOrder();
  }

  @Override
  public void setNext(E e, @Nullable E next) {
    e.setNextInWriteOrder(next);
  }

  /**
   * An element that is linked on the {@link Deque}.
   */
  interface WriteOrder> {

    /**
     * Retrieves the previous element or null if either the element is unlinked or the
     * first element on the deque.
     */
    @Nullable T getPreviousInWriteOrder();

    /** Sets the previous element or null if there is no link. */
    void setPreviousInWriteOrder(@Nullable T prev);

    /**
     * Retrieves the next element or null if either the element is unlinked or the last
     * element on the deque.
     */
    @Nullable T getNextInWriteOrder();

    /** Sets the next element or null if there is no link. */
    void setNextInWriteOrder(@Nullable T next);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy