| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.NoSuchElementException; |
| 10 |
|
| 11 |
/** |
| 12 |
* The stack of byte values. This class is not synchronized and should not be |
| 13 |
* used by multiple threads concurrently. |
| 14 |
*/ |
| 15 |
public final class ByteStack { |
| 16 |
|
| 17 |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
| 18 |
|
| 19 |
private int size; |
| 20 |
|
| 21 |
private byte[] array; |
| 22 |
|
| 23 |
/** |
| 24 |
* Creates a new empty instance. |
| 25 |
*/ |
| 26 |
public ByteStack() { |
| 27 |
array = Utils.EMPTY_BYTES; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Pushes an item onto the top of this stack. |
| 32 |
* |
| 33 |
* @param item |
| 34 |
* the item to push |
| 35 |
*/ |
| 36 |
public void push(byte item) { |
| 37 |
int index = size; |
| 38 |
int oldLength = array.length; |
| 39 |
if (index >= oldLength) { |
| 40 |
grow(oldLength); |
| 41 |
} |
| 42 |
array[index] = item; |
| 43 |
size = index + 1; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Removes the item at the top of this stack and returns that item. |
| 48 |
* |
| 49 |
* @return the item at the top of this stack |
| 50 |
* @throws NoSuchElementException |
| 51 |
* if stack is empty |
| 52 |
*/ |
| 53 |
public byte pop() { |
| 54 |
int index = size - 1; |
| 55 |
if (index < 0) { |
| 56 |
throw new NoSuchElementException(); |
| 57 |
} |
| 58 |
size = index; |
| 59 |
return array[index]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Removes the item at the top of this stack and returns that item. |
| 64 |
* |
| 65 |
* @param defaultValue |
| 66 |
* value to return if stack is empty |
| 67 |
* @return the item at the top of this stack, or default value |
| 68 |
*/ |
| 69 |
public int poll(int defaultValue) { |
| 70 |
int index = size - 1; |
| 71 |
if (index < 0) { |
| 72 |
return defaultValue; |
| 73 |
} |
| 74 |
size = index; |
| 75 |
return array[index]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Looks at the item at the top of this stack without removing it. |
| 80 |
* |
| 81 |
* @param defaultValue |
| 82 |
* value to return if stack is empty |
| 83 |
* @return the item at the top of this stack, or default value |
| 84 |
*/ |
| 85 |
public int peek(int defaultValue) { |
| 86 |
int index = size - 1; |
| 87 |
if (index < 0) { |
| 88 |
return defaultValue; |
| 89 |
} |
| 90 |
return array[index]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns {@code true} if this stack is empty. |
| 95 |
* |
| 96 |
* @return {@code true} if this stack is empty |
| 97 |
*/ |
| 98 |
public boolean isEmpty() { |
| 99 |
return size == 0; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the number of items in this stack. |
| 104 |
* |
| 105 |
* @return the number of items in this stack |
| 106 |
*/ |
| 107 |
public int size() { |
| 108 |
return size; |
| 109 |
} |
| 110 |
|
| 111 |
private void grow(int length) { |
| 112 |
if (length == 0) { |
| 113 |
length = 0x10; |
| 114 |
} else if (length >= MAX_ARRAY_SIZE) { |
| 115 |
throw new OutOfMemoryError(); |
| 116 |
} else if ((length <<= 1) < 0) { |
| 117 |
length = MAX_ARRAY_SIZE; |
| 118 |
} |
| 119 |
array = Arrays.copyOf(array, length); |
| 120 |
} |
| 121 |
|
| 122 |
} |