3 namespace Org\Mxchange\CoreFramework\Stacker;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 abstract class BaseStacker extends BaseFrameworkSystem {
32 const EXCEPTION_STACKER_ALREADY_INITIALIZED = 0x050;
33 const EXCEPTION_STACKER_IS_FULL = 0x051;
34 const EXCEPTION_NO_STACKER_FOUND = 0x052;
35 const EXCEPTION_STACKER_IS_EMPTY = 0x053;
38 * Protected constructor
40 * @param $className Name of the class
43 protected function __construct ($className) {
44 // Call parent constructor
45 parent::__construct($className);
49 * Initializes given stacker
51 * @param $stackerName Name of the stack
52 * @param $forceReInit Force re-initialization
54 * @throws AlreadyInitializedStackerException If the stack is already initialized
56 public function initStack ($stackerName, $forceReInit = false) {
57 // Is the stack already initialized?
58 if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) {
59 // Then throw the exception
60 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
63 // Initialize the given stack
64 $this->initGenericArrayKey('stacks', $stackerName, 'entries', $forceReInit);
68 * Initializes all stacks
72 public function initStacks (array $stacks, $forceReInit = false) {
73 // "Walk" through all (more will be added as needed
74 foreach ($stacks as $stackerName) {
76 $this->initStack($stackerName, $forceReInit);
81 * Checks whether the given stack is initialized (set in array $stackers)
83 * @param $stackerName Name of the stack
84 * @return $isInitialized Whether the stack is initialized
86 public function isStackInitialized ($stackerName) {
88 $isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries'));
91 return $isInitialized;
95 * Checks whether the given stack is full
97 * @param $stackerName Name of the stack
98 * @return $isFull Whether the stack is full
99 * @throws NoStackerException If given stack is missing
101 protected function isStackFull ($stackerName) {
102 // Is the stack not yet initialized?
103 if (!$this->isStackInitialized($stackerName)) {
104 // Throw an exception
105 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
108 // So, is the stack full?
109 $isFull = (($this->getStackCount($stackerName)) == $this->getConfigInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'));
116 * Checks whether the given stack is empty
118 * @param $stackerName Name of the stack
119 * @return $isEmpty Whether the stack is empty
120 * @throws NoStackerException If given stack is missing
122 public function isStackEmpty ($stackerName) {
123 // Is the stack not yet initialized?
124 if (!$this->isStackInitialized($stackerName)) {
125 // Throw an exception
126 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
129 // So, is the stack empty?
130 $isEmpty = (($this->getStackCount($stackerName)) == 0);
137 * Getter for size of given stack (array count)
139 * @param $stackerName Name of the stack
140 * @return $count Size of stack (array count)
141 * @throws NoStackerException If given stack is missing
143 public function getStackCount ($stackerName) {
144 // Is the stack not yet initialized?
145 if (!$this->isStackInitialized($stackerName)) {
146 // Throw an exception
147 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
150 // Now, count the array of entries
151 $count = $this->countGenericArrayElements('stacks', $stackerName, 'entries');
158 * Adds a value to given stack
160 * @param $stackerName Name of the stack
161 * @param $value Value to add to this stacker
163 * @throws FullStackerException Thrown if the stack is full
165 protected function addValue ($stackerName, $value) {
166 // Is the stack not yet initialized or full?
167 if (!$this->isStackInitialized($stackerName)) {
169 $this->initStack($stackerName);
170 } elseif ($this->isStackFull($stackerName)) {
172 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
175 // Now add the value to the stack
176 $this->pushValueToGenericArrayKey('stacks', $stackerName, 'entries', $value);
180 * Get last value from named stacker
182 * @param $stackerName Name of the stack
183 * @return $value Value of last added value
184 * @throws NoStackerException If the named stacker was not found
185 * @throws EmptyStackerException If the named stacker is empty
187 protected function getLastValue ($stackerName) {
188 // Is the stack not yet initialized or full?
189 if (!$this->isStackInitialized($stackerName)) {
190 // Throw an exception
191 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
192 } elseif ($this->isStackEmpty($stackerName)) {
193 // Throw an exception
194 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
197 // Now get the last value
198 $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', $this->getStackCount($stackerName) - 1);
205 * Get first value from named stacker
207 * @param $stackerName Name of the stack
208 * @return $value Value of last added value
209 * @throws NoStackerException If the named stacker was not found
210 * @throws EmptyStackerException If the named stacker is empty
212 protected function getFirstValue ($stackerName) {
213 // Is the stack not yet initialized or full?
214 if (!$this->isStackInitialized($stackerName)) {
215 // Throw an exception
216 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
217 } elseif ($this->isStackEmpty($stackerName)) {
218 // Throw an exception
219 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
222 // Now get the first value
223 $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', 0);
230 * "Pops" last entry from stack
232 * @param $stackerName Name of the stack
233 * @return $value Value "poped" from array
234 * @throws NoStackerException If the named stacker was not found
235 * @throws EmptyStackerException If the named stacker is empty
237 protected function popLast ($stackerName) {
238 // Is the stack not yet initialized or full?
239 if (!$this->isStackInitialized($stackerName)) {
240 // Throw an exception
241 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
242 } elseif ($this->isStackEmpty($stackerName)) {
243 // Throw an exception
244 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
247 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
248 return $this->popGenericArrayElement('stacks', $stackerName, 'entries');
252 * "Pops" first entry from stack
254 * @param $stackerName Name of the stack
255 * @return $value Value "shifted" from array
256 * @throws NoStackerException If the named stacker was not found
257 * @throws EmptyStackerException If the named stacker is empty
259 protected function popFirst ($stackerName) {
260 // Is the stack not yet initialized or full?
261 if (!$this->isStackInitialized($stackerName)) {
262 // Throw an exception
263 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
264 } elseif ($this->isStackEmpty($stackerName)) {
265 // Throw an exception
266 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
269 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
270 return $this->shiftGenericArrayElement('stacks', $stackerName, 'entries');