3 namespace Org\Mxchange\CoreFramework\Stacker;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 abstract class BaseStacker extends BaseFrameworkSystem {
33 const EXCEPTION_STACKER_ALREADY_INITIALIZED = 0x050;
34 const EXCEPTION_STACKER_IS_FULL = 0x051;
35 const EXCEPTION_NO_STACKER_FOUND = 0x052;
36 const EXCEPTION_STACKER_IS_EMPTY = 0x053;
39 * Protected constructor
41 * @param $className Name of the class
44 protected function __construct (string $className) {
45 // Call parent constructor
46 parent::__construct($className);
50 * Initializes given stacker
52 * @param $stackerName Name of the stack
53 * @param $forceReInit Force re-initialization
55 * @throws AlreadyInitializedStackerException If the stack is already initialized
57 public function initStack ($stackerName, $forceReInit = false) {
58 // Is the stack already initialized?
59 if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) {
60 // Then throw the exception
61 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
64 // Initialize the given stack
65 $this->initGenericArrayKey('stacks', $stackerName, 'entries', $forceReInit);
69 * Initializes all stacks
73 public function initStacks (array $stacks, $forceReInit = false) {
74 // "Walk" through all (more will be added as needed
75 foreach ($stacks as $stackerName) {
77 $this->initStack($stackerName, $forceReInit);
82 * Checks whether the given stack is initialized (set in array $stackers)
84 * @param $stackerName Name of the stack
85 * @return $isInitialized Whether the stack is initialized
87 public function isStackInitialized ($stackerName) {
89 $isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries'));
92 return $isInitialized;
96 * Checks whether the given stack is full
98 * @param $stackerName Name of the stack
99 * @return $isFull Whether the stack is full
100 * @throws NoStackerException If given stack is missing
102 protected function isStackFull ($stackerName) {
103 // Is the stack not yet initialized?
104 if (!$this->isStackInitialized($stackerName)) {
105 // Throw an exception
106 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
109 // So, is the stack full?
110 $isFull = (($this->getStackCount($stackerName)) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'));
117 * Checks whether the given stack is empty
119 * @param $stackerName Name of the stack
120 * @return $isEmpty Whether the stack is empty
121 * @throws NoStackerException If given stack is missing
123 public function isStackEmpty ($stackerName) {
124 // Is the stack not yet initialized?
125 if (!$this->isStackInitialized($stackerName)) {
126 // Throw an exception
127 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
130 // So, is the stack empty?
131 $isEmpty = (($this->getStackCount($stackerName)) == 0);
138 * Getter for size of given stack (array count)
140 * @param $stackerName Name of the stack
141 * @return $count Size of stack (array count)
142 * @throws NoStackerException If given stack is missing
144 public function getStackCount ($stackerName) {
145 // Is the stack not yet initialized?
146 if (!$this->isStackInitialized($stackerName)) {
147 // Throw an exception
148 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
151 // Now, count the array of entries
152 $count = $this->countGenericArrayElements('stacks', $stackerName, 'entries');
159 * Adds a value to given stack
161 * @param $stackerName Name of the stack
162 * @param $value Value to add to this stacker
164 * @throws FullStackerException Thrown if the stack is full
166 protected function addValue ($stackerName, $value) {
167 // Is the stack not yet initialized or full?
168 if (!$this->isStackInitialized($stackerName)) {
170 $this->initStack($stackerName);
171 } elseif ($this->isStackFull($stackerName)) {
173 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
176 // Now add the value to the stack
177 $this->pushValueToGenericArrayKey('stacks', $stackerName, 'entries', $value);
181 * Get last value from named stacker
183 * @param $stackerName Name of the stack
184 * @return $value Value of last added value
185 * @throws NoStackerException If the named stacker was not found
186 * @throws EmptyStackerException If the named stacker is empty
188 protected function getLastValue ($stackerName) {
189 // Is the stack not yet initialized or full?
190 if (!$this->isStackInitialized($stackerName)) {
191 // Throw an exception
192 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
193 } elseif ($this->isStackEmpty($stackerName)) {
194 // Throw an exception
195 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
198 // Now get the last value
199 $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', $this->getStackCount($stackerName) - 1);
206 * Get first value from named stacker
208 * @param $stackerName Name of the stack
209 * @return $value Value of last added value
210 * @throws NoStackerException If the named stacker was not found
211 * @throws EmptyStackerException If the named stacker is empty
213 protected function getFirstValue ($stackerName) {
214 // Is the stack not yet initialized or full?
215 if (!$this->isStackInitialized($stackerName)) {
216 // Throw an exception
217 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
218 } elseif ($this->isStackEmpty($stackerName)) {
219 // Throw an exception
220 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
223 // Now get the first value
224 $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', 0);
231 * "Pops" last entry from stack
233 * @param $stackerName Name of the stack
234 * @return $value Value "poped" from array
235 * @throws NoStackerException If the named stacker was not found
236 * @throws EmptyStackerException If the named stacker is empty
238 protected function popLast ($stackerName) {
239 // Is the stack not yet initialized or full?
240 if (!$this->isStackInitialized($stackerName)) {
241 // Throw an exception
242 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
243 } elseif ($this->isStackEmpty($stackerName)) {
244 // Throw an exception
245 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
248 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
249 return $this->popGenericArrayElement('stacks', $stackerName, 'entries');
253 * "Pops" first entry from stack
255 * @param $stackerName Name of the stack
256 * @return $value Value "shifted" from array
257 * @throws NoStackerException If the named stacker was not found
258 * @throws EmptyStackerException If the named stacker is empty
260 protected function popFirst ($stackerName) {
261 // Is the stack not yet initialized or full?
262 if (!$this->isStackInitialized($stackerName)) {
263 // Throw an exception
264 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
265 } elseif ($this->isStackEmpty($stackerName)) {
266 // Throw an exception
267 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
270 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
271 return $this->shiftGenericArrayElement('stacks', $stackerName, 'entries');