0e91c7884a39ce6814e680df786552e601da9ae2
[core.git] / inc / classes / main / stacker / file_stack / class_BaseFileStack.php
1 <?php
2 /**
3  * A general file-based stack class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class BaseFileStack extends BaseStacker {
25         /**
26          * Protected constructor
27          *
28          * @param       $className      Name of the class
29          * @return      void
30          */
31         protected function __construct ($className) {
32                 // Call parent constructor
33                 parent::__construct($className);
34         }
35
36         /**
37          * Initializes this stack.
38          *
39          * @param       $fileName       File name of this stack
40          * @return      void
41          */
42         protected function initStack ($fileName) {
43                 // Get a file i/o pointer instance
44                 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
45
46                 // Get iterator instance
47                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
48
49                 // Is the instance implementing the right interface?
50                 assert($iteratorInstance instanceof SeekableFileIterator);
51
52                 // Set iterator here
53                 $this->setIteratorInstance($iteratorInstance);
54         }
55
56         /**
57          * Checks whether the given stack is initialized (set in array $stackers)
58          *
59          * @param       $stackerName    Name of the stack
60          * @return      $isInitialized  Whether the stack is initialized
61          */
62         public function isStackInitialized ($stackerName) {
63                 // Is is there?
64                 $this->partialStub('stackerName=' . $stackerName);
65                 $isInitialized = TRUE;
66
67                 // Return result
68                 return $isInitialized;
69         }
70
71         /**
72          * Getter for size of given stack (array count)
73          *
74          * @param       $stackerName    Name of the stack
75          * @return      $count                  Size of stack (array count)
76          * @throws      NoStackerException      If given stack is missing
77          */
78         public function getStackCount ($stackerName) {
79                 // Is the stack not yet initialized?
80                 if (!$this->isStackInitialized($stackerName)) {
81                         // Throw an exception
82                         throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
83                 } // END - if
84
85                 // Now, count the array of entries
86                 $this->partialStub('stackerName=' . $stackerName);
87                 $count = 0;
88
89                 // Return result
90                 return $count;
91         }
92
93         /**
94          * Adds a value to given stack
95          *
96          * @param       $stackerName    Name of the stack
97          * @param       $value                  Value to add to this stacker
98          * @return      void
99          * @throws      FullStackerException    Thrown if the stack is full
100          */
101         protected function addValue ($stackerName, $value) {
102                 // Is the stack not yet initialized or full?
103                 if (!$this->isStackInitialized($stackerName)) {
104                         // Then do it here
105                         $this->initStack($stackerName);
106                 } elseif ($this->isStackFull($stackerName)) {
107                         // Stacker is full
108                         throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
109                 }
110
111                 // Now add the value to the stack
112                 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
113         }
114
115         /**
116          * Get last value from named stacker
117          *
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
122          */
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);
131                 }
132
133                 // Now get the last value
134                 $this->partialStub('stackerName=' . $stackerName);
135                 $value = NULL;
136
137                 // Return it
138                 return $value;
139         }
140
141         /**
142          * Get first value from named stacker
143          *
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
148          */
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);
157                 }
158
159                 // Now get the first value
160                 $this->partialStub('stackerName=' . $stackerName);
161                 $value = NULL;
162
163                 // Return it
164                 return $value;
165         }
166
167         /**
168          * "Pops" last entry from stack
169          *
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
174          */
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);
183                 }
184
185                 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
186                 $this->partialStub('stackerName=' . $stackerName);
187                 return NULL;
188         }
189
190         /**
191          * "Pops" first entry from stack
192          *
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
197          */
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);
206                 }
207
208                 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
209                 $this->partialStub('stackerName=' . $stackerName);
210                 return NULL;
211         }
212 }
213
214 // [EOF]
215 ?>