3 namespace Org\Mxchange\CoreFramework\Stack;
6 use \InvalidArgumentException;
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 class FiFoStacker extends BaseStacker implements Stackable {
32 * Protected constructor
36 private function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of the class Stacker and prepares it for usage
44 * @return $stackInstance An instance of FiFoStacker
46 public static final function createFiFoStacker () {
48 $stackInstance = new FiFoStacker();
50 // Init generic stacker
51 $stackInstance->initStack('generic');
53 // Return the prepared instance
54 return $stackInstance;
58 * Pushs a value on a named stacker
60 * @param $stackerName Name of the stack
61 * @param $value Value to push on it
63 * @throws InvalidArgumentException If a parameter is invalid
64 * @throws StackerFullException If the stack is full
66 public function pushNamed (string $stackerName, $value) {
68 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
69 if (empty($stackerName)) {
70 // No empty stack name
71 throw new InvalidArgumentException('Parameter "stackerName" is empty');
74 // Call the protected method
75 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
76 parent::addValueToStack($stackerName, $value);
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!');
83 * 'Pops' a value from a named stacker and returns it's value
85 * @param $stackerName Name of the stack
86 * @return $value Value of the current stack entry
87 * @throws InvalidArgumentException If a parameter is invalid
88 * @throws BadMethodCallException If the named stacker was not found
89 * @throws BadMethodCallException If the named stacker is empty
91 public function popNamed (string $stackerName) {
93 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
94 if (empty($stackerName)) {
95 // No empty stack name
96 throw new InvalidArgumentException('Parameter "stackerName" is empty');
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling this->getNamed(%s) ...', $stackerName));
101 $value = $this->getNamed($stackerName);
103 // Call the protected method
104 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::popFirst(%s) ...', $stackerName));
105 parent::popFirst($stackerName);
108 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));
113 * Get value from named stacker
115 * @param $stackerName Name of the stack
116 * @return $value Value of last added value
117 * @throws InvalidArgumentException If a parameter is invalid
118 * @throws BadMethodCallException If the named stacker was not found
119 * @throws BadMethodCallException If the named stacker is empty
121 public function getNamed (string $stackerName) {
122 // Validate parameter
123 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName));
124 if (empty($stackerName)) {
125 // No empty stack name
126 throw new InvalidArgumentException('Parameter "stackerName" is empty');
129 // Call the protected method
130 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::getFirstValue(%s) ...', $stackerName));
131 $value = parent::getFirstValue($stackerName);
134 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value)));