# PrioryFiFoTaskQueue

`com.jsmacrosce.PrioryFiFoTaskQueue`

A queue that deduplicates entries by equality and groups them into integer-priority buckets,  
preserving insertion order within each bucket. When adding to an empty membership set, the entry
becomes the cached head. Removing the head through the queue's head-removal operation selects the
next head from the lowest-priority bucket; removing entries by value or clearing the queue does not
refresh the cached head. `size()` counts occupied priority buckets, not entries.  
```js
const Queue = Java.type("com.jsmacrosce.PrioryFiFoTaskQueue");  
const Integer = Java.type("java.lang.Integer");  
const queue = new Queue(JavaWrapper.methodToJava(function(value) {
  return Integer.valueOf(String(value));  
}));  
queue.add("2");  
queue.add("1");  
Chat.log(queue.poll());  
```

## Type Parameters

- `E`: queued entry type

## Constructors

### new PrioryFiFoTaskQueue {#com.jsmacrosce.PrioryFiFoTaskQueue_constructor-Function-}

```js
new PrioryFiFoTaskQueue(Function<E, Integer> priorityFunction)
```

Stores the function used to obtain each entry's stable integer priority; it is not evaluated here.

**Parameters:**
- `priorityFunction`: non-null function providing a stable integer priority per entry

## Methods

### add {#com.jsmacrosce.PrioryFiFoTaskQueue_add-E-}

```js
add(E e): boolean
```

Adds an entry if no equal entry is already present. The priority function is evaluated for a new entry;  
 adding a lower-priority entry does not replace the cached head while the membership set is nonempty.

**Parameters:**
- `e`: non-null entry with a stable priority

**Returns:** `true` if added; `false` if already present

### addAll {#com.jsmacrosce.PrioryFiFoTaskQueue_addAll-Collection-}

```js
addAll(Collection<? extends E> collection): boolean
```

Adds entries in collection iteration order; additions completed before a failure are not rolled back.

**Parameters:**
- `collection`: entries to add

**Returns:** whether at least one entry was added

### clear {#com.jsmacrosce.PrioryFiFoTaskQueue_clear-}

```js
clear(): void
```

Clears the membership set and priority buckets without resetting the cached head or notifying waiters.

### contains {#com.jsmacrosce.PrioryFiFoTaskQueue_contains-Object-}

```js
contains(Object o): boolean
```

Checks whether the membership set contains the candidate entry.

**Parameters:**
- `o`: candidate entry

**Returns:** whether the entry is present

### containsAll {#com.jsmacrosce.PrioryFiFoTaskQueue_containsAll-Collection-}

```js
containsAll(Collection<?> collection): boolean
```

Checks whether every supplied entry is in the membership set.

**Parameters:**
- `collection`: entries to check

**Returns:** whether all entries are present

### element {#com.jsmacrosce.PrioryFiFoTaskQueue_element-}

```js
element(): E
```

Returns the cached head without checking membership or removing it.

**Returns:** the cached head, or `null` if the cache is empty; it may be stale

### isEmpty {#com.jsmacrosce.PrioryFiFoTaskQueue_isEmpty-}

```js
isEmpty(): boolean
```

Returns whether the priority-bucket map is empty.

**Returns:** whether the priority-bucket map is empty

### iterator {#com.jsmacrosce.PrioryFiFoTaskQueue_iterator-}

```js
iterator(): Iterator<E>
```

Returns a live iterator over the membership set; iteration order is unspecified and is not priority order.  
 Removing through the iterator changes only the membership set, not the priority buckets or cached head.

**Returns:** live membership-set iterator

### offer {#com.jsmacrosce.PrioryFiFoTaskQueue_offer-E-}

```js
offer(E e): boolean
```

Adds an entry using the queue's deduplication and priority behavior.

**Parameters:**
- `e`: non-null entry with a stable priority

**Returns:** `true` if newly added; `false` if an equal entry is already present

### peek {#com.jsmacrosce.PrioryFiFoTaskQueue_peek-}

```js
peek(): E
```

Returns the cached head without checking membership or removing it.

**Returns:** the cached head, or `null` if the cache is empty; it may be stale

### peekWaiting {#com.jsmacrosce.PrioryFiFoTaskQueue_peekWaiting-}

```js
peekWaiting(): E
```

Waits once if membership is empty, then returns the cached head without removing it or rechecking membership.  
 Wait interruption propagates as `InterruptedException`.

**Returns:** the cached head, which may be `null` or stale

### peekWaiting {#com.jsmacrosce.PrioryFiFoTaskQueue_peekWaiting-long-}

```js
peekWaiting(long timeout): E
```

Waits once if membership is empty, then returns the cached head without removing it or rechecking membership.  
 Wait interruption propagates as `InterruptedException`.

**Parameters:**
- `timeout`: wait duration in milliseconds; zero waits indefinitely, and a negative value is invalid if waiting occurs

**Returns:** the cached head, which may be `null` or stale

### poll {#com.jsmacrosce.PrioryFiFoTaskQueue_poll-}

```js
poll(): E
```

Removes and returns the cached head using the queue's head-removal behavior.

**Returns:** the previous cached head, or `null` if the cache was empty; the cached entry may be stale

### pollWaiting {#com.jsmacrosce.PrioryFiFoTaskQueue_pollWaiting-}

```js
pollWaiting(): E
```

Waits until the membership set is nonempty, then removes and returns the cached head. Wait interruption propagates as
 `InterruptedException`.

**Returns:** the cached-head removal result, which may be `null` or stale

### pollWaiting {#com.jsmacrosce.PrioryFiFoTaskQueue_pollWaiting-long-}

```js
pollWaiting(long timeout): E
```

While membership is empty, tracks wall-clock elapsed wait time against the budget, then attempts to remove and return
 the cached head even if the budget has expired or membership remains empty; this does not guarantee an exact deadline.  
 Nonpositive budgets skip waiting. Wait interruption propagates as `InterruptedException`.

**Parameters:**
- `timeout`: wait budget in milliseconds; nonpositive values skip waiting

**Returns:** the cached-head removal result, which may be `null` or stale

### remove {#com.jsmacrosce.PrioryFiFoTaskQueue_remove-Object-}

```js
remove(Object o): boolean
```

Removes an entry and its bucket membership using its current priority-function result. This does not
 refresh the cached head; entries therefore need stable priorities.

**Parameters:**
- `o`: entry to remove

**Returns:** `true` if present and removed; `false` if absent

### remove {#com.jsmacrosce.PrioryFiFoTaskQueue_remove-}

```js
remove(): E
```

Removes and returns the cached head, then refreshes it from the lowest-priority bucket when non-null.

**Returns:** the previous cached head, or `null` if the cache was empty; the cached entry may be stale

### removeAll {#com.jsmacrosce.PrioryFiFoTaskQueue_removeAll-Collection-}

```js
removeAll(Collection<?> collection): boolean
```

Removes entries in collection iteration order through value removal; completed removals are not rolled back on failure.  
 The cached head is not refreshed.

**Parameters:**
- `collection`: entries to remove

**Returns:** whether any entry was removed, if the operation completes

### retainAll {#com.jsmacrosce.PrioryFiFoTaskQueue_retainAll-Collection-}

```js
retainAll(Collection<?> collection): boolean
```

Removes entries absent from the collection while iterating the live membership set; structural removals during
 iteration can cause `ConcurrentModificationException` after partial removal, so complete retention is not guaranteed.  
 The cached head is not refreshed.

**Parameters:**
- `collection`: entries to keep

**Returns:** whether any entry was removed, if the operation completes

### size {#com.jsmacrosce.PrioryFiFoTaskQueue_size-}

```js
size(): int
```

Returns the number of priority buckets containing entries, not the number of entries.

**Returns:** occupied priority bucket count

### toArray {#com.jsmacrosce.PrioryFiFoTaskQueue_toArray-}

```js
toArray(): Object[]
```

Returns an array of membership-set entries in unspecified order.

**Returns:** membership entries

### toArray {#com.jsmacrosce.PrioryFiFoTaskQueue_toArray-T[]-}

```js
toArray(T[] ts): T[]
```

Copies membership-set entries into an array, using the supplied array when it is large enough.

**Parameters:**
- `ts`: destination array

**Type Parameters:**
- `T`: destination array component type

**Returns:** membership entries in the supplied array when large enough, or a new array otherwise

### toArray {#com.jsmacrosce.PrioryFiFoTaskQueue_toArray-IntFunction-}

```js
toArray(IntFunction<T[]> arg0): T[]
```

**Returns:** `T[]`
