X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fstacker%2Fclass_BaseStacker.php;h=a3419fe486db58647b1463e2b3eea823e44180db;hp=2fdc0d2556dd62494b3902bc066e053f5b2861d4;hb=01cc80f06529313b40cb6858eae3f361c8ed1aee;hpb=7dd76b61aa39deab983fecadef400ee9e8c95ebc diff --git a/inc/classes/main/stacker/class_BaseStacker.php b/inc/classes/main/stacker/class_BaseStacker.php index 2fdc0d25..a3419fe4 100644 --- a/inc/classes/main/stacker/class_BaseStacker.php +++ b/inc/classes/main/stacker/class_BaseStacker.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -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 * @@ -48,42 +43,40 @@ class BaseStacker extends BaseFrameworkSystem { * Initializes given stacker * * @param $stackerName Name of the stack + * @param $forceReInit Force re-initialization * @return void * @throws AlreadyInitializedStackerException If the stack is already initialized */ - protected final function initStacker ($stackerName) { + public final function initStacker ($stackerName, $forceReInit = FALSE) { // Is the stack already initialized? - if ($this->isStackInitialized($stackerName)) { + if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) { // Then throw the exception throw new AlreadyInitializedStackerException(array($this, $stackerName), 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); } /** - * 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) { // 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) { @@ -101,10 +94,10 @@ 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 + * @param $stackerName Name of the stack + * @return $isEmpty Whether the stack is empty * @throws NoStackerException If given stack is missing */ public final function isStackEmpty ($stackerName) { @@ -114,11 +107,11 @@ class BaseStacker extends BaseFrameworkSystem { throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } // END - if - // So, is the stack full? - $isFull = (($this->getStackCount($stackerName)) == 0); + // So, is the stack empty? + $isEmpty = (($this->getStackCount($stackerName)) == 0); // Return result - return $isFull; + return $isEmpty; } /** @@ -128,7 +121,7 @@ class BaseStacker extends BaseFrameworkSystem { * @return $count Size of stack (array count) * @throws NoStackerException If given stack is missing */ - protected final function getStackCount ($stackerName) { + public final function getStackCount ($stackerName) { // Is the stack not yet initialized? if (!$this->isStackInitialized($stackerName)) { // Throw an exception @@ -136,7 +129,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; @@ -161,7 +154,7 @@ class BaseStacker extends BaseFrameworkSystem { } // Now add the value to the stack - array_push($this->stacks[$stackerName]['entries'], $value); + $this->pushValueToGenericArrayElement('stacks', $stackerName, 'entries', $value); } /** @@ -178,12 +171,12 @@ class BaseStacker extends BaseFrameworkSystem { // 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; @@ -203,12 +196,12 @@ class BaseStacker extends BaseFrameworkSystem { // 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; @@ -218,7 +211,7 @@ 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 */ @@ -228,19 +221,19 @@ class BaseStacker extends BaseFrameworkSystem { // 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 */ @@ -250,12 +243,12 @@ class BaseStacker extends BaseFrameworkSystem { // 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'); } }