* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ abstract class BaseStacker extends BaseFrameworkSystem { // Exception codes const EXCEPTION_STACKER_ALREADY_INITIALIZED = 0x050; const EXCEPTION_STACKER_IS_FULL = 0x051; const EXCEPTION_NO_STACKER_FOUND = 0x052; const EXCEPTION_STACKER_IS_EMPTY = 0x053; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); } /** * Initializes given stacker * * @param $stackerName Name of the stack * @param $forceReInit Force re-initialization * @return void * @throws AlreadyInitializedStackerException If the stack is already initialized */ public function initStack ($stackerName, $forceReInit = false) { // Is the stack already initialized? if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) { // Then throw the exception throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED); } // END - if // Initialize the given stack $this->initGenericArrayKey('stacks', $stackerName, 'entries', $forceReInit); } /** * Initializes all stacks * * @return void */ public function initStacks (array $stacks, $forceReInit = false) { // "Walk" through all (more will be added as needed foreach ($stacks as $stackerName) { // Init this stack $this->initStack($stackerName, $forceReInit); } // END - foreach } /** * Checks whether the given stack is initialized (set in array $stackers) * * @param $stackerName Name of the stack * @return $isInitialized Whether the stack is initialized */ public function isStackInitialized ($stackerName) { // Is is there? $isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries')); // Return result return $isInitialized; } /** * Checks whether the given stack is full * * @param $stackerName Name of the stack * @return $isFull Whether the stack is full * @throws NoStackerException If given stack is missing */ protected function isStackFull ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } // END - if // So, is the stack full? $isFull = (($this->getStackCount($stackerName)) == $this->getConfigInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size')); // Return result return $isFull; } /** * Checks whether the given stack is empty * * @param $stackerName Name of the stack * @return $isEmpty Whether the stack is empty * @throws NoStackerException If given stack is missing */ public function isStackEmpty ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } // END - if // So, is the stack empty? $isEmpty = (($this->getStackCount($stackerName)) == 0); // Return result return $isEmpty; } /** * Getter for size of given stack (array count) * * @param $stackerName Name of the stack * @return $count Size of stack (array count) * @throws NoStackerException If given stack is missing */ public function getStackCount ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } // END - if // Now, count the array of entries $count = $this->countGenericArrayElements('stacks', $stackerName, 'entries'); // Return result return $count; } /** * Adds a value to given stack * * @param $stackerName Name of the stack * @param $value Value to add to this stacker * @return void * @throws FullStackerException Thrown if the stack is full */ protected function addValue ($stackerName, $value) { // Is the stack not yet initialized or full? if (!$this->isStackInitialized($stackerName)) { // Then do it here $this->initStack($stackerName); } elseif ($this->isStackFull($stackerName)) { // Stacker is full throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL); } // Now add the value to the stack $this->pushValueToGenericArrayKey('stacks', $stackerName, 'entries', $value); } /** * Get last value from named stacker * * @param $stackerName Name of the stack * @return $value Value of last added value * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ protected function getLastValue ($stackerName) { // Is the stack not yet initialized or full? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now get the last value $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', $this->getStackCount($stackerName) - 1); // Return it return $value; } /** * Get first value from named stacker * * @param $stackerName Name of the stack * @return $value Value of last added value * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ protected function getFirstValue ($stackerName) { // Is the stack not yet initialized or full? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now get the first value $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', 0); // Return it return $value; } /** * "Pops" last entry from stack * * @param $stackerName Name of the stack * @return $value Value "poped" from array * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ protected function popLast ($stackerName) { // Is the stack not yet initialized or full? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now, remove the last entry, we don't care about the return value here, see elseif() block above return $this->popGenericArrayElement('stacks', $stackerName, 'entries'); } /** * "Pops" first entry from stack * * @param $stackerName Name of the stack * @return $value Value "shifted" from array * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ protected function popFirst ($stackerName) { // Is the stack not yet initialized or full? if (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now, remove the last entry, we don't care about the return value here, see elseif() block above return $this->shiftGenericArrayElement('stacks', $stackerName, 'entries'); } }