3 * A general file-based stack class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 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 BaseFileStack extends BaseStacker {
26 * Protected constructor
28 * @param $className Name of the class
31 protected function __construct ($className) {
32 // Call parent constructor
33 parent::__construct($className);
37 * Initializes this stack.
39 * @param $fileName File name of this stack
42 protected function initStack ($fileName) {
43 // Get a file i/o pointer instance
44 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
46 // Get iterator instance
47 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
49 // Is the instance implementing the right interface?
50 assert($iteratorInstance instanceof SeekableFileIterator);
53 $this->setIteratorInstance($iteratorInstance);
57 * Checks whether the given stack is initialized (set in array $stackers)
59 * @param $stackerName Name of the stack
60 * @return $isInitialized Whether the stack is initialized
62 public function isStackInitialized ($stackerName) {
64 $this->partialStub('stackerName=' . $stackerName);
65 $isInitialized = TRUE;
68 return $isInitialized;
72 * Getter for size of given stack (array count)
74 * @param $stackerName Name of the stack
75 * @return $count Size of stack (array count)
76 * @throws NoStackerException If given stack is missing
78 public function getStackCount ($stackerName) {
79 // Is the stack not yet initialized?
80 if (!$this->isStackInitialized($stackerName)) {
82 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
85 // Now, count the array of entries
86 $this->partialStub('stackerName=' . $stackerName);
94 * Adds a value to given stack
96 * @param $stackerName Name of the stack
97 * @param $value Value to add to this stacker
99 * @throws FullStackerException Thrown if the stack is full
101 protected function addValue ($stackerName, $value) {
102 // Is the stack not yet initialized or full?
103 if (!$this->isStackInitialized($stackerName)) {
105 $this->initStack($stackerName);
106 } elseif ($this->isStackFull($stackerName)) {
108 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
111 // Now add the value to the stack
112 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
116 * Get last value from named stacker
118 * @param $stackerName Name of the stack
119 * @return $value Value of last added value
120 * @throws NoStackerException If the named stacker was not found
121 * @throws EmptyStackerException If the named stacker is empty
123 protected function getLastValue ($stackerName) {
124 // Is the stack not yet initialized or full?
125 if (!$this->isStackInitialized($stackerName)) {
126 // Throw an exception
127 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
128 } elseif ($this->isStackEmpty($stackerName)) {
129 // Throw an exception
130 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
133 // Now get the last value
134 $this->partialStub('stackerName=' . $stackerName);
142 * Get first value from named stacker
144 * @param $stackerName Name of the stack
145 * @return $value Value of last added value
146 * @throws NoStackerException If the named stacker was not found
147 * @throws EmptyStackerException If the named stacker is empty
149 protected function getFirstValue ($stackerName) {
150 // Is the stack not yet initialized or full?
151 if (!$this->isStackInitialized($stackerName)) {
152 // Throw an exception
153 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
154 } elseif ($this->isStackEmpty($stackerName)) {
155 // Throw an exception
156 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
159 // Now get the first value
160 $this->partialStub('stackerName=' . $stackerName);
168 * "Pops" last entry from stack
170 * @param $stackerName Name of the stack
171 * @return $value Value "poped" from array
172 * @throws NoStackerException If the named stacker was not found
173 * @throws EmptyStackerException If the named stacker is empty
175 protected function popLast ($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)) {
181 // Throw an exception
182 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
185 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
186 $this->partialStub('stackerName=' . $stackerName);
191 * "Pops" first entry from stack
193 * @param $stackerName Name of the stack
194 * @return $value Value "shifted" from array
195 * @throws NoStackerException If the named stacker was not found
196 * @throws EmptyStackerException If the named stacker is empty
198 protected function popFirst ($stackerName) {
199 // Is the stack not yet initialized or full?
200 if (!$this->isStackInitialized($stackerName)) {
201 // Throw an exception
202 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
203 } elseif ($this->isStackEmpty($stackerName)) {
204 // Throw an exception
205 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
208 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
209 $this->partialStub('stackerName=' . $stackerName);