3 namespace Org\Mxchange\CoreFramework\Stack;
5 // Import framework-specific stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use \InvalidArgumentException;
12 * A FiFo Stacker class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class FiFoStacker extends BaseStacker implements Stackable {
35 * Protected constructor
39 private function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of the class Stacker and prepares it for usage
47 * @return $stackInstance An instance of FiFoStacker
49 public static final function createFiFoStacker () {
51 $stackInstance = new FiFoStacker();
53 // Init generic stacker
54 $stackInstance->initStack('generic');
56 // Return the prepared instance
57 return $stackInstance;
61 * Pushs a value on a named stacker
63 * @param $stackerName Name of the stack
64 * @param $value Value to push on it
66 * @throws InvalidArgumentException If a parameter is invalid
67 * @throws BadMethodCallException If the stack is full
69 public function pushNamed (string $stackerName, $value) {
71 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
72 if (empty($stackerName)) {
73 // No empty stack name
74 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
77 // Call the protected method
78 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
79 parent::addValueToStack($stackerName, $value);
82 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!');
86 * 'Pops' a value from a named stacker and returns it's value
88 * @param $stackerName Name of the stack
89 * @return $value Value of the current stack entry
90 * @throws InvalidArgumentException If a parameter is invalid
91 * @throws BadMethodCallException If the named stacker was not found
92 * @throws BadMethodCallException If the named stacker is empty
94 public function popNamed (string $stackerName) {
96 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
97 if (empty($stackerName)) {
98 // No empty stack name
99 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
103 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking this->getNamed(%s) ...', $stackerName));
104 $value = $this->getNamed($stackerName);
106 // Call the protected method
107 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::popFirst(%s) ...', $stackerName));
108 parent::popFirst($stackerName);
111 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
116 * Get value from named stacker
118 * @param $stackerName Name of the stack
119 * @return $value Value of last added value
120 * @throws InvalidArgumentException If a parameter is invalid
121 * @throws BadMethodCallException If the named stacker was not found
122 * @throws BadMethodCallException If the named stacker is empty
124 public function getNamed (string $stackerName) {
125 // Validate parameter
126 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
127 if (empty($stackerName)) {
128 // No empty stack name
129 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
132 // Call the protected method
133 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Invoking parent::getFirstValue(%s) ...', $stackerName));
134 $value = parent::getFirstValue($stackerName);
137 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));