5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseStacker extends BaseFrameworkSystem {
26 const EXCEPTION_STACKER_ALREADY_INITIALIZED = 0x050;
27 const EXCEPTION_STACKER_IS_FULL = 0x051;
28 const EXCEPTION_NO_STACKER_FOUND = 0x052;
29 const EXCEPTION_STACKER_IS_EMPTY = 0x053;
32 * An array holding all stacks
34 private $stacks = array();
37 * Protected constructor
39 * @param $className Name of the class
42 protected function __construct ($className) {
43 // Call parent constructor
44 parent::__construct($className);
48 * Initializes given stacker
50 * @param $stackerName Name of the stack
52 * @throws AlreadyInitializedStackerException If the stack is already initialized
54 protected final function initStacker ($stackerName) {
55 // Is the stack already initialized?
56 if ($this->isStackInitialized($stackerName)) {
57 // Then throw the exception
58 throw new AlreadyInitializedStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
61 // Initialize the given stack
62 $this->stacks[$stackerName] = array(
63 'max_size' => $this->getConfigInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'),
69 * Checks wether the given stack is initialized (set in array $stackers)
71 * @param $stackerName Name of the stack
72 * @return $isInitialized Wether the stack is initialized
74 protected final function isStackInitialized ($stackerName) {
76 $isInitialized = ((isset($this->stacks[$stackerName])) && (is_array($this->stacks[$stackerName])));
79 return $isInitialized;
83 * Checks wether the given stack is full
85 * @param $stackerName Name of the stack
86 * @return $isFull Wether the stack is full
87 * @throws NoStackerException If given stack is missing
89 protected final function isStackFull($stackerName) {
90 // Is the stack not yet initialized?
91 if (!$this->isStackInitialized($stackerName)) {
93 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
96 // So, is the stack full?
97 $isFull = (($this->getStackCount($stackerName)) == $this->getConfigInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'));
104 * Checks wether the given stack is empty
106 * @param $stackerName Name of the stack
107 * @return $isEmpty Wether the stack is empty
108 * @throws NoStackerException If given stack is missing
110 protected final function isStackEmpty($stackerName) {
111 // Is the stack not yet initialized?
112 if (!$this->isStackInitialized($stackerName)) {
113 // Throw an exception
114 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
117 // So, is the stack full?
118 $isFull = (($this->getStackCount($stackerName)) == 0);
125 * Getter for size of given stack (array count)
127 * @param $stackerName Name of the stack
128 * @return $count Size of stack (array count)
129 * @throws NoStackerException If given stack is missing
131 protected final function getStackCount ($stackerName) {
132 // Is the stack not yet initialized?
133 if (!$this->isStackInitialized($stackerName)) {
134 // Throw an exception
135 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
138 // Now, count the array of entries
139 $count = count($this->stacks[$stackerName]['entries']);
146 * Adds a value to given stack
148 * @param $stackerName Name of the stack
149 * @param $value Value to add to this stacker
151 * @throws FullStackerException Thrown if the stack is full
153 protected final function addValue ($stackerName, $value) {
154 // Is the stack not yet initialized or full?
155 if (!$this->isStackInitialized($stackerName)) {
157 $this->initStacker($stackerName);
158 } elseif ($this->isStackFull($stackerName)) {
160 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
163 // Now add the value to the stack
164 array_push($this->stacks[$stackerName]['entries'], $value);
168 * Get last value from named stacker
170 * @param $stackerName Name of the stack
171 * @return $value Value of last added value
172 * @throws NoStackerException If the named stacker was not found
173 * @throws EmptyStackerException If the named stacker is empty
175 protected final function getLastValue ($stackerName) {
176 // Is the stack not yet initialized or full?
177 if (!$this->isStackInitialized($stackerName)) {
178 // Throw an exception
179 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
180 } elseif ($this->isStackEmpty($stackerName)) {
182 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
185 // Now get the last value
186 $value = $this->stacks[$stackerName]['entries'][$this->getStackCount($stackerName) - 1];
193 * "Pops" last entry from stack
195 * @param $stackerName Name of the stack
197 * @throws NoStackerException If the named stacker was not found
198 * @throws EmptyStackerException If the named stacker is empty
200 protected final function popLast ($stackerName) {
201 // Is the stack not yet initialized or full?
202 if (!$this->isStackInitialized($stackerName)) {
203 // Throw an exception
204 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
205 } elseif ($this->isStackEmpty($stackerName)) {
207 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
210 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
211 array_pop($this->stacks[$stackerName]['entries']);