Package ax.antpick.k2hash
Class K2hashQueue
- java.lang.Object
-
- ax.antpick.k2hash.K2hashQueue
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class K2hashQueue extends Object implements Closeable
Queue holds elements in a FIFO (first-in-first-out) manner. Order of elements is configurable by passing a boolean variable to the 2nd parameter of constructors. Passing a true variable constructs a Queue instance, Otherwise a Stack instance is created.The difference of KeyQueue and Queue: KeyQueue stores references to keys in k2hash database whereas Queue holds any kinds of elements.
Usage Examples. Suppose you want to make a new sorted data collection in a K2hashQueue. You could write this as:
package com.example; import java.io.IOException; import java.util.*; import java.util.stream.*; import ax.antpick.*; public class App { public static void main(String[] args) { // 1. make a random string array for test String[] suffleArray = {"e", "b", "d", "a", "c"}; // 2. sort it! Arrays.sort(suffleArray); // 3. offer to a Queue Stream<String> stream = Stream.of(suffleArray); try (K2hash db = K2hash.of("App.k2h")) { long handle = db.getHandle(); K2hashQueue queue = K2hashQueue.of(handle); stream.forEach(s -> { queue.offer(s); }); } catch (IOException ex) { System.out.println(ex.getMessage()); return; } // 4. poll from the Queue try (K2hash db = K2hash.of("App.k2h")) { long handle = db.getHandle(); K2hashQueue queue = K2hashQueue.of(handle); String s = null; do { s = queue.poll(); System.out.println(s); } while (s != null); } catch (IOException ex) { System.out.println(ex.getMessage()); return; } } }
-
-
Field Summary
Fields Modifier and Type Field Description static K2hashAttrPack[]
DEFAULT_ATTRPACK
Default K2hashAttrPack array isnull
.static int
DEFAULT_ATTRPACK_SIZE
Default size of a K2hashAttrPack array is0
.static long
DEFAULT_EXPIRATION_DURATION
Default data expiration duration is0
.static boolean
DEFAULT_FIFO
Default FIFO istrue
.static String
DEFAULT_PASS
Default passphrase string isnull
.static int
DEFAULT_POSITION
Default position of a peek operation is0
.static String
DEFAULT_PREFIX
Default prefix string isnull
.static long
K2H_INVALID_HANDLE
Default k2hash data handle.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(Object obj)
Inserts an element into the tail of this queue.void
clear()
Removes all of the elements from this collection (optional operation).void
close()
Free KeyQueueHandlelong
count()
Returns the number of queue.String
element()
Finds and gets a object from the head of this queue.String
element(int position)
Finds and gets a object from the head of this queue.long
getQueueHandle()
Returns a K2hashQueue handle.boolean
isEmpty()
Returns true if, and only if, queue size is 0.static K2hashQueue
of(long handle)
Constructs a K2hashQueue instance.static K2hashQueue
of(long handle, boolean fifo, String prefix)
Constructs a K2hashQueue instance.static K2hashQueue
of(long handle, boolean fifo, String prefix, String pass, long expirationDuration)
Constructs a K2hashQueue instance.boolean
offer(Object obj)
Inserts an element into the tail of this queue.String
peek()
Finds and gets a object at the head of this queue.String
peek(int position)
Finds and gets a object from the head of this queue.String
poll()
Finds and gets a object from the head of this queue.void
print()
Print the objects in this queue.String
remove()
Finds and removes the object at the top of this queue and returns the object.List<String>
removeList(long count)
Removes objects from this queue.String
toString()
-
-
-
Field Detail
-
K2H_INVALID_HANDLE
public static final long K2H_INVALID_HANDLE
Default k2hash data handle.- See Also:
- Constant Field Values
-
DEFAULT_FIFO
public static final boolean DEFAULT_FIFO
Default FIFO istrue
.- See Also:
- Constant Field Values
-
DEFAULT_PREFIX
public static final String DEFAULT_PREFIX
Default prefix string isnull
.
-
DEFAULT_POSITION
public static final int DEFAULT_POSITION
Default position of a peek operation is0
.- See Also:
- Constant Field Values
-
DEFAULT_PASS
public static final String DEFAULT_PASS
Default passphrase string isnull
.
-
DEFAULT_EXPIRATION_DURATION
public static final long DEFAULT_EXPIRATION_DURATION
Default data expiration duration is0
.- See Also:
- Constant Field Values
-
DEFAULT_ATTRPACK
public static final K2hashAttrPack[] DEFAULT_ATTRPACK
Default K2hashAttrPack array isnull
.
-
DEFAULT_ATTRPACK_SIZE
public static final int DEFAULT_ATTRPACK_SIZE
Default size of a K2hashAttrPack array is0
.- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static K2hashQueue of(long handle) throws IOException
Constructs a K2hashQueue instance.- Parameters:
handle
- a K2hash data handle- Returns:
- a K2hashQueue instance
- Throws:
IOException
- if a K2hash data handle is invalid
-
of
public static K2hashQueue of(long handle, boolean fifo, String prefix) throws IOException
Constructs a K2hashQueue instance.- Parameters:
handle
- a K2hash data handlefifo
-true
if elements are in a FIFO (first-in-first-out) mannerprefix
- a prefix string of this queue- Returns:
- a K2hashQueue instance
- Throws:
IOException
- if a K2hash data handle is invalid
-
of
public static K2hashQueue of(long handle, boolean fifo, String prefix, String pass, long expirationDuration) throws IOException
Constructs a K2hashQueue instance.- Parameters:
handle
- a K2hash data handlefifo
-true
if elements are in a FIFO (first-in-first-out) mannerprefix
- a prefix string of this queuepass
- a passphrase string to access dataexpirationDuration
- a duration to expire data in seconds- Returns:
- a K2hashQueue instance
- Throws:
IOException
- if a K2hash handle is invalidIllegalArgumentException
- if an illegal augment exists
-
close
public void close() throws IOException
Free KeyQueueHandle- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getQueueHandle
public long getQueueHandle()
Returns a K2hashQueue handle.- Returns:
- a K2hashQueue handle
-
isEmpty
public boolean isEmpty()
Returns true if, and only if, queue size is 0.- Returns:
true
if emptyfalse
otherwise
-
count
public long count()
Returns the number of queue.- Returns:
- the number of queue.
-
peek
public String peek()
Finds and gets a object at the head of this queue. Null returns if this queue is empty. This operation is a read-only access. The object will not remove from this queue. This method is the same function as k2h_q_read in C API.- Returns:
- an object at the head of this queue
-
peek
public String peek(int position)
Finds and gets a object from the head of this queue. Null returns if this queue is empty. This operation is a read-only access. The object will not remove from this queue. This method is the same function as k2h_q_read in C API.- Parameters:
position
- a position to peek in this queue- Returns:
- an object at the position of this queue
- Throws:
IllegalArgumentException
- if the position is less than zero
-
offer
public boolean offer(Object obj)
Inserts an element into the tail of this queue.- Parameters:
obj
- an object to be inserted to this queue- Returns:
true
if successfalse
otherwise- Throws:
IllegalArgumentException
- - if arguments are illegal.
-
poll
public String poll()
Finds and gets a object from the head of this queue. Null returns if this queue is empty. This operation is a write access. The object will remove from this queue.- Returns:
- the object at the head of this queue. returns null if empty.
-
add
public boolean add(Object obj)
Inserts an element into the tail of this queue.- Parameters:
obj
- an object to be inserted into this queue- Returns:
true
if successfalse
otherwise- Throws:
NoSuchElementException
- - if this queue is emptyIllegalArgumentException
- if an illegal augment exists
-
remove
public String remove()
Finds and removes the object at the top of this queue and returns the object.- Returns:
- the object at the top of this queue
- Throws:
NoSuchElementException
- - if this queue is empty
-
removeList
public List<String> removeList(long count)
Removes objects from this queue.- Parameters:
count
- the number of objects to be removed- Returns:
- list of removed objects
- Throws:
NoSuchElementException
- - if this queue is emptyIllegalArgumentException
- if an illegal augment exists
-
element
public String element()
Finds and gets a object from the head of this queue. Throws NoSuchElementException if this queue is empty. This operation is a read-only access. The object will not remove from this queue.- Returns:
- a copy of the string in queue
- Throws:
NoSuchElementException
- - if this queue is empty
-
element
public String element(int position)
Finds and gets a object from the head of this queue. Throws NoSuchElementException if this queue is empty. This operation is a read-only access. The object will not remove from this queue.- Parameters:
position
- position where the data exists in queue- Returns:
- a copy of the string in queue
- Throws:
NoSuchElementException
- - if this queue is emptyIllegalArgumentException
- if an illegal augment exists
-
clear
public void clear()
Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.
-
print
public void print()
Print the objects in this queue.- Throws:
NoSuchElementException
- - if this queue is empty
-
-