X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fstacker%2Fclass_BaseStacker.php;h=1bfd88063616920d943e4430c44d2c9798af975e;hp=5c25e928ee0dabcb1e8088727d8fd1a81a9a276e;hb=647d34b9da7d7492a5e89fc33544fbb769fa0b57;hpb=b3649e68029a4b262010b35e2394ef39e17e112c diff --git a/inc/classes/main/stacker/class_BaseStacker.php b/inc/classes/main/stacker/class_BaseStacker.php index 5c25e928..1bfd8806 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 - 2011 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -48,12 +48,13 @@ 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 @@ -114,7 +115,7 @@ class BaseStacker extends BaseFrameworkSystem { throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } // END - if - // So, is the stack full? + // So, is the stack empty? $isFull = (($this->getStackCount($stackerName)) == 0); // Return result @@ -128,7 +129,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 @@ -189,6 +190,31 @@ class BaseStacker extends BaseFrameworkSystem { 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 final 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 last value + $value = $this->stacks[$stackerName]['entries'][0]; + + // Return it + return $value; + } + /** * "Pops" last entry from stack * @@ -210,6 +236,28 @@ class BaseStacker extends BaseFrameworkSystem { // Now, remove the last entry, we don't care about the return value here, see elseif() block above array_pop($this->stacks[$stackerName]['entries']); } + + /** + * "Pops" first entry from stack + * + * @param $stackerName Name of the stack + * @return void + * @throws NoStackerException If the named stacker was not found + * @throws EmptyStackerException If the named stacker is empty + */ + protected final 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 + array_shift($this->stacks[$stackerName]['entries']); + } } // [EOF]