X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fstacker%2Fclass_BaseStacker.php;h=0deeeda2a6c43acacf5ba8e1686610b6aeef736f;hb=1203de1295dd16b8f4d89ff58ffe61aa49760be9;hp=ea31904d0797a14ef86c083456115cf78bf19d42;hpb=1d0f8e0fa7346ab3df9d0ee44c4c957047711ddc;p=core.git diff --git a/inc/classes/main/stacker/class_BaseStacker.php b/inc/classes/main/stacker/class_BaseStacker.php index ea31904d..0deeeda2 100644 --- a/inc/classes/main/stacker/class_BaseStacker.php +++ b/inc/classes/main/stacker/class_BaseStacker.php @@ -2,11 +2,11 @@ /** * A general Stacker * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @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 @@ -28,11 +28,6 @@ class BaseStacker extends BaseFrameworkSystem { const EXCEPTION_NO_STACKER_FOUND = 0x052; const EXCEPTION_STACKER_IS_EMPTY = 0x053; - /** - * An array holding all stacks - */ - private $stacks = array(); - /** * Protected constructor * @@ -52,42 +47,52 @@ class BaseStacker extends BaseFrameworkSystem { * @return void * @throws AlreadyInitializedStackerException If the stack is already initialized */ - public final function initStacker ($stackerName, $forceReInit = false) { + public function initStack ($stackerName, $forceReInit = FALSE) { // Is the stack already initialized? - if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) { + if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) { // Then throw the exception - throw new AlreadyInitializedStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_ALREADY_INITIALIZED); + throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED); } // END - if // Initialize the given stack - $this->stacks[$stackerName] = array( - 'max_size' => $this->getConfigInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size'), - 'entries' => array() - ); + $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 wether the given stack is initialized (set in array $stackers) + * Checks whether the given stack is initialized (set in array $stackers) * * @param $stackerName Name of the stack - * @return $isInitialized Wether the stack is initialized + * @return $isInitialized Whether the stack is initialized */ - public final function isStackInitialized ($stackerName) { + public function isStackInitialized ($stackerName) { // Is is there? - $isInitialized = ((isset($this->stacks[$stackerName])) && (is_array($this->stacks[$stackerName]))); + $isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries')); // Return result return $isInitialized; } /** - * Checks wether the given stack is full + * Checks whether the given stack is full * * @param $stackerName Name of the stack - * @return $isFull Wether the stack is full + * @return $isFull Whether the stack is full * @throws NoStackerException If given stack is missing */ - protected final function isStackFull ($stackerName) { + protected function isStackFull ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception @@ -102,13 +107,13 @@ class BaseStacker extends BaseFrameworkSystem { } /** - * Checks wether the given stack is empty + * Checks whether the given stack is empty * * @param $stackerName Name of the stack - * @return $isEmpty Wether the stack is empty + * @return $isEmpty Whether the stack is empty * @throws NoStackerException If given stack is missing */ - public final function isStackEmpty ($stackerName) { + public function isStackEmpty ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception @@ -129,7 +134,7 @@ class BaseStacker extends BaseFrameworkSystem { * @return $count Size of stack (array count) * @throws NoStackerException If given stack is missing */ - public final function getStackCount ($stackerName) { + public function getStackCount ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception @@ -137,7 +142,7 @@ class BaseStacker extends BaseFrameworkSystem { } // END - if // Now, count the array of entries - $count = count($this->stacks[$stackerName]['entries']); + $count = $this->countGenericArrayElements('stacks', $stackerName, 'entries'); // Return result return $count; @@ -151,18 +156,18 @@ class BaseStacker extends BaseFrameworkSystem { * @return void * @throws FullStackerException Thrown if the stack is full */ - protected final function addValue ($stackerName, $value) { + protected function addValue ($stackerName, $value) { // Is the stack not yet initialized or full? if (!$this->isStackInitialized($stackerName)) { // Then do it here - $this->initStacker($stackerName); + $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 - array_push($this->stacks[$stackerName]['entries'], $value); + $this->pushValueToGenericArrayKey('stacks', $stackerName, 'entries', $value); } /** @@ -173,18 +178,18 @@ class BaseStacker extends BaseFrameworkSystem { * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ - protected final function getLastValue ($stackerName) { + 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 an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } // Now get the last value - $value = $this->stacks[$stackerName]['entries'][$this->getStackCount($stackerName) - 1]; + $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', $this->getStackCount($stackerName) - 1); // Return it return $value; @@ -198,18 +203,18 @@ class BaseStacker extends BaseFrameworkSystem { * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ - protected final function getFirstValue ($stackerName) { + 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 an exception throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); } - // Now get the last value - $value = $this->stacks[$stackerName]['entries'][0]; + // Now get the first value + $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', 0); // Return it return $value; @@ -219,44 +224,44 @@ class BaseStacker extends BaseFrameworkSystem { * "Pops" last entry from stack * * @param $stackerName Name of the stack - * @return void + * @return $value Value "poped" from array * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ - protected final function popLast ($stackerName) { + 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 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 - array_pop($this->stacks[$stackerName]['entries']); + return $this->popGenericArrayElement('stacks', $stackerName, 'entries'); } /** * "Pops" first entry from stack * * @param $stackerName Name of the stack - * @return void + * @return $value Value "shifted" from array * @throws NoStackerException If the named stacker was not found * @throws EmptyStackerException If the named stacker is empty */ - protected final function popFirst ($stackerName) { + 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 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 - array_shift($this->stacks[$stackerName]['entries']); + return $this->shiftGenericArrayElement('stacks', $stackerName, 'entries'); } }