• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LinkedDeque.java
1
/*
2
 * Copyright 2014 Ben Manes. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.github.benmanes.caffeine.cache;
17

18
import static java.util.Objects.requireNonNull;
19

20
import java.util.Comparator;
21
import java.util.ConcurrentModificationException;
22
import java.util.Deque;
23
import java.util.Iterator;
24
import java.util.NoSuchElementException;
25

26
import org.jspecify.annotations.Nullable;
27

28
/**
29
 * A linked list extension of the {@link Deque} interface where the link pointers are tightly
30
 * integrated with the element. Linked deques have no capacity restrictions; they grow as necessary
31
 * to support usage. They are not thread-safe; in the absence of external synchronization, they do
32
 * not support concurrent access by multiple threads. Null elements are prohibited.
33
 * <p>
34
 * Most {@code LinkedDeque} operations run in constant time by assuming that the element parameter
35
 * is associated with the deque instance. Any usage that violates this assumption will result in
36
 * non-deterministic behavior.
37
 * <p>
38
 * An element can exist in only one instance of a deque implementation, but may exist in multiple
39
 * implementations. Each implementation must define unique names for accessing and modifying its
40
 * link pointers.
41
 * <p>
42
 * The iterators returned by this class are <i>fail-fast</i>: If the deque is structurally modified
43
 * at any time after the iterator is created, in any way except through the iterator's own
44
 * {@link Iterator#remove() remove} method, then the iterator will throw a
45
 * {@link ConcurrentModificationException}. Thus, in the face of concurrent modification, the
46
 * iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at
47
 * an undetermined time in the future. Note that the fail-fast behavior of an iterator cannot be
48
 * guaranteed and should be used only to detect bugs.
49
 *
50
 * @author ben.manes@gmail.com (Ben Manes)
51
 * @param <E> the type of elements held in this collection
52
 */
53
interface LinkedDeque<E> extends Deque<E> {
54

55
  /**
56
   * Returns if the element is at the front of the deque.
57
   *
58
   * @param e the linked element
59
   */
60
  boolean isFirst(@Nullable E e);
61

62
  /**
63
   * Returns if the element is at the back of the deque.
64
   *
65
   * @param e the linked element
66
   */
67
  boolean isLast(@Nullable E e);
68

69
  /**
70
   * Moves the element to the front of the deque so that it becomes the first element.
71
   *
72
   * @param e the linked element
73
   */
74
  void moveToFront(E e);
75

76
  /**
77
   * Moves the element to the back of the deque so that it becomes the last element.
78
   *
79
   * @param e the linked element
80
   */
81
  void moveToBack(E e);
82

83
  /**
84
   * Retrieves the previous element or {@code null} if either the element is unlinked or the first
85
   * element on the deque.
86
   */
87
  @Nullable E getPrevious(E e);
88

89
  /** Sets the previous element or {@code null} if there is no link. */
90
  void setPrevious(E e, @Nullable E prev);
91

92
  /**
93
   * Retrieves the next element or {@code null} if either the element is unlinked or the last
94
   * element on the deque.
95
   */
96
  @Nullable E getNext(E e);
97

98
  /** Sets the next element or {@code null} if there is no link. */
99
  void setNext(E e, @Nullable E next);
100

101
  @Override
102
  PeekingIterator<E> iterator();
103

104
  @Override
105
  PeekingIterator<E> descendingIterator();
106

107
  interface PeekingIterator<E> extends Iterator<E> {
108

109
    /** Returns the next element in the iteration, without advancing the iteration. */
110
    @Nullable E peek();
111

112
    /** Returns an iterator that returns the first iteration followed by the second iteration. */
113
    static <E> PeekingIterator<E> concat(PeekingIterator<E> first, PeekingIterator<E> second) {
114
      return new PeekingIterator<>() {
×
115
        @Override public boolean hasNext() {
116
          return first.hasNext() || second.hasNext();
×
117
        }
118
        @Override public E next() {
119
          if (first.hasNext()) {
×
120
            return first.next();
×
121
          } else if (second.hasNext()) {
×
122
            return second.next();
×
123
          }
124
          throw new NoSuchElementException();
×
125
        }
126
        @Override public @Nullable E peek() {
127
          return first.hasNext() ? first.peek() : second.peek();
×
128
        }
129
      };
130
    }
131

132
    /** Returns an iterator that selects the greater element from the backing iterators. */
133
    static <E> PeekingIterator<E> comparing(PeekingIterator<E> first,
134
          PeekingIterator<E> second, Comparator<E> comparator) {
135
      return new PeekingIterator<>() {
×
136
        @Override public boolean hasNext() {
137
          return first.hasNext() || second.hasNext();
×
138
        }
139
        @Override public E next() {
140
          if (!first.hasNext()) {
×
141
            return second.next();
×
142
          } else if (!second.hasNext()) {
×
143
            return first.next();
×
144
          }
145
          E o1 = requireNonNull(first.peek());
×
146
          E o2 = requireNonNull(second.peek());
×
147
          boolean greaterOrEqual = (comparator.compare(o1, o2) >= 0);
×
148
          return greaterOrEqual ? first.next() : second.next();
×
149
        }
150
        @Override public @Nullable E peek() {
151
          if (!first.hasNext()) {
×
152
            return second.peek();
×
153
          } else if (!second.hasNext()) {
×
154
            return first.peek();
×
155
          }
156
          E o1 = requireNonNull(first.peek());
×
157
          E o2 = requireNonNull(second.peek());
×
158
          boolean greaterOrEqual = (comparator.compare(o1, o2) >= 0);
×
159
          return greaterOrEqual ? o1 : o2;
×
160
        }
161
      };
162
    }
163
  }
164
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc