Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

92
LINES

< > BotCompany Repo | #1036407 // SynchronizedCircularFifoBuffer

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (10091L/55K).

1  
sclass SynchronizedCircularFifoBuffer<E> is Iterable<E>, IntSize {
2  
  int capacity;
3  
  long base;
4  
  ArrayDeque<E> coreStorage; // Unsynched core list
5  
  Cl<E> storage; // synched coreStorage
6  
  
7  
  *(int capacity) {
8  
    this.capacity = capacity;
9  
    coreStorage = new ArrayDeque<E>(capacity);
10  
    storage = synchronizedCollection(coreStorage);
11  
  }
12  
  
13  
  /**
14  
   * Adds the given element to the buffer. If the buffer is full, the least recently added element is discarded so
15  
   * that a new element can be inserted.
16  
   */
17  
  public void add(E e) {
18  
    synchronized(storage) {
19  
      if (isFull()) remove();
20  
      coreStorage.addLast(e);
21  
    }
22  
  }
23  
  
24  
  /**
25  
   * Removes and returns the least recently inserted element from this buffer.
26  
   */
27  
  public E remove() {
28  
    synchronized(storage) {
29  
      ++base;
30  
      return coreStorage.removeFirst();
31  
    }
32  
  }
33  
  
34  
  /**
35  
   * Returns true iff the buffers remaining capacity is 0.
36  
   */
37  
  public boolean isFull() {
38  
    return storage.size() == capacity;
39  
  }
40  
  
41  
  /**
42  
   * Returns true iff the buffers size is 0.
43  
   */
44  
  public boolean isEmpty() {
45  
    return storage.isEmpty();
46  
  }
47  
  
48  
  /**
49  
   * Returns the number of elements in the buffer.
50  
   */
51  
  public int size() {
52  
    return storage.size();
53  
  }
54  
  
55  
  public Object[] toArray() {
56  
    return storage.toArray();
57  
  }
58  
  
59  
  public<T> T[] toArray(T[] a) {
60  
    return storage.toArray(a);
61  
  }
62  
  
63  
  public List<E> asList() {
64  
    return new ArrayList<E>(storage);
65  
  }
66  
  
67  
  public Cl<E> getBackingStore() {
68  
    ret storage;
69  
  }
70  
  
71  
  public Iterator<E> iterator() {
72  
    return storage.iterator();
73  
  }
74  
  
75  
  public E last() {
76  
    synchronized(storage) {
77  
      ret coreStorage.peekLast();
78  
    }
79  
  }
80  
  
81  
  public void clear {
82  
    synchronized(storage) {
83  
      coreStorage.clear();
84  
      base = 0;
85  
    }
86  
  }
87  
  
88  
  // how many elements were discarded
89  
  synchronized long getBase() { ret base; }
90  
  
91  
  synchronized int getCapacity() { ret capacity; }
92  
}

Author comment

Began life as a copy of #1000774

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036407
Snippet name: SynchronizedCircularFifoBuffer
Eternal ID of this version: #1036407/11
Text MD5: f45e5ca17e3cce4d20837252608d2023
Transpilation MD5: ddb3c294c5badb6a8ba0da44af35bbac
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-11 15:09:44
Source code size: 2006 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 148 / 247
Version history: 10 change(s)
Referenced in: [show references]