]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/lists/class_BaseList.php
Continued:
[core.git] / framework / main / classes / lists / class_BaseList.php
index 8d79d1d6c3c70090be933403c05444383e04cfb4..5cf4c15c19c005a2c9c05e59a7a60f7ca3b0c473 100644 (file)
@@ -1,12 +1,13 @@
 <?php
 // Own namespace
-namespace CoreFramework\Lists;
+namespace Org\Mxchange\CoreFramework\Lists;
 
 // Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Generic\FrameworkInterface;
-use CoreFramework\Object\BaseFrameworkSystem;
-use CoreFramework\Visitor\Visitable;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
+use Org\Mxchange\CoreFramework\Visitor\Visitable;
 
 // Import SPL stuff
 use \IteratorAggregate;
@@ -17,7 +18,7 @@ use \Countable;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @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
  *
@@ -34,7 +35,10 @@ use \Countable;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countable {
+abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countable {
+       // Load traits
+       use IteratorTrait;
+
        // Exception constants
        const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20;
        const EXCEPTION_GROUP_NOT_FOUND     = 0xf21;
@@ -43,17 +47,17 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
        /**
         * List groups array
         */
-       private $listGroups = array();
+       private $listGroups = [];
 
        /**
         * List entries array
         */
-       private $listEntries = array();
+       private $listEntries = [];
 
        /**
         * List index array
         */
-       private $listIndex = array();
+       private $listIndex = [];
 
        /**
         * Protected constructor
@@ -61,7 +65,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @param       $className      Name of the class
         * @return      void
         */
-       protected function __construct ($className) {
+       protected function __construct (string $className) {
                // Call parent constructor
                parent::__construct($className);
        }
@@ -78,11 +82,11 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                // Is the instance set?
                if (is_null($iteratorInstance)) {
                        // Prepare a default iterator
-                       $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', array($this));
+                       $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class', [$this]);
 
                        // Set it here
                        $this->setIteratorInstance($iteratorInstance);
-               } // END - if
+               }
 
                // And return it
                return $iteratorInstance;
@@ -94,8 +98,8 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @param       $groupName      Group to check if found in list
         * @return      $isset          Whether the group is valid
         */
-       public function isGroupSet ($groupName) {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName);
+       public function isGroupSet (string $groupName) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName);
                return isset($this->listGroups[$groupName]);
        }
 
@@ -106,17 +110,17 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @return      void
         * @throws      ListGroupAlreadyAddedException  If the given group is already added
         */
-       public function addGroup ($groupName) {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
+       public function addGroup (string $groupName) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
                // Is the group already added?
                if ($this->isGroupSet($groupName)) {
                        // Throw the exception here
                        throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED);
-               } // END - if
+               }
 
                // Add the group which is a simple array
                $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class');
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
        }
 
        /**
@@ -128,31 +132,27 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @return      void
         * @throws      NoListGroupException    If the given group is not found
         */
-       public function addInstance ($groupName, $subGroup, Visitable $visitableInstance) {
+       public function addInstance (string $groupName, string $subGroup, Visitable $visitableInstance) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName  . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName  . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!');
 
                // Is the group there?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
                        throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
-               } // END - if
+               }
 
                // Is the sub group there?
                if (!$this->listGroups[$groupName]->isGroupSet($subGroup)) {
                        // Automatically add it
                        $this->listGroups[$groupName]->addGroup($subGroup);
-               } // END - if
+               }
 
                // Generate the hash
                $hash = $this->generateHash($groupName, $subGroup, $visitableInstance);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName  . ',subGroup=' . $subGroup . ',hash=' . $hash . ' - Calling addEntry() ...');
-
-               // Now add it to the group list and hash it
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
-               //$this->listGroups[$groupName]->addEntry($subGroup, $hash);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',this->listGroups[' . $groupName . ']=' . $this->listGroups[$groupName]->__toString());
 
                // Add the hash to the index
                array_push($this->listIndex, $hash);
@@ -161,7 +161,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                $this->listEntries[$hash] = $visitableInstance;
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName  . ',subGroup=' . $subGroup . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName  . ',subGroup=' . $subGroup . ' - EXIT!');
        }
 
        /**
@@ -173,38 +173,38 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         */
        public final function getArrayFromList ($list) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!');
 
                // Is the group there?
                if ((!is_null($list)) && (!$this->isGroupSet($list))) {
                        // Throw the exception here
                        throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND);
-               } // END - if
+               }
 
                // Init array
-               $array = array();
+               $array = [];
 
                // Is there another list?
                if (!is_null($list)) {
                        // Then get it as well
                        $array = $this->listGroups[$list]->getArrayFromList(NULL);
-               } // END - if
+               }
 
                // Walk through all entries
                foreach ($this->listIndex as $hash) {
                        // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: hash=' . $hash);
 
                        // Is the list entry set?
                        if ($this->isHashValid($hash)) {
                                // Add it
                                array_push($array, $this->listEntries[$hash]);
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, true) . ' - ADDED!');
-                       } // END - if
-               } // END - foreach
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: hash=' . $hash . ',array(' . count($array) . ')=' . print_r($array, true) . ' - ADDED!');
+                       }
+               }
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',array()=' . count($array) . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',[]=' . count($array) . ' - EXIT!');
 
                // Return it
                return $array;
@@ -218,33 +218,33 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @return      void
         * @throws      NoListGroupException    If the given group is not found
         */
-       public function addEntry ($groupName, $entry) {
+       public function addEntry (string $groupName, $entry) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
 
                // Is the group already added?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
                        throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
-               } // END - if
+               }
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, true) . ', hash=' . $hash);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . print_r($entry, true) . ', hash=' . $hash);
 
                // Add the hash to the index
                array_push($this->listIndex, $hash);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries));
 
                // Now add the entry to the list
                $this->listEntries[$hash] = $entry;
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ',listEntries()=' . count($this->listEntries) . ' - EXIT!');
        }
 
        /**
@@ -255,21 +255,21 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @return      void
         * @throws      NoListGroupException    If the given group is not found
         */
-       public function removeEntry ($groupName, $entry) {
+       public function removeEntry (string $groupName, $entry) {
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!');
 
                // Is the group already added?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
                        throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
-               } // END - if
+               }
 
                // Generate hash
                $hash = $this->generateHash($groupName, $groupName, $entry);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ',entry=' . $entry . ', hash=' . $hash);
 
                // Remove it from the list ...
                unset($this->listEntries[$hash]);
@@ -278,7 +278,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                unset($this->listIndex[array_search($hash, $this->listIndex)]);
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - EXIT!');
        }
 
        /**
@@ -289,7 +289,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @param       $entry          An entry of any type
         * @return      $hash           The generated
         */
-       private function generateHash ($groupName, $subGroup, $entry) {
+       private function generateHash (string $groupName, string $subGroup, $entry) {
                // Created entry, 'null' is default
                $entry2 = 'null';
 
@@ -308,15 +308,15 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                } elseif ((is_array($entry)) && (isset($entry['id']))) {
                        // Supported array found
                        $entry2 = crc32($entry['id']) . ':' . count($entry);
-               } elseif ((is_array($entry)) && (isset($entry[BasePool::SOCKET_ARRAY_RESOURCE])) && (isset($entry[BasePool::SOCKET_ARRAY_CONN_TYPE]))) {
-                       // Is a socket resource array
-                       $entry2 = crc32($entry[BasePool::SOCKET_ARRAY_RESOURCE] . ':' . $entry[BasePool::SOCKET_ARRAY_CONN_TYPE]);
+               } elseif (($this->getCallbackInstance() instanceof FrameworkInterface) && (method_exists($this->getCallbackInstance(), 'generateListHashFromEntry'))) {
+                       // Call it instead
+                       $entry2 = $this->getCallbackInstance()->generateListHashFromEntry($entry);
                } else {
                        // Unsupported type detected
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST[' . __METHOD__ . ':' . __LINE__ . ']: Entry type ' . gettype($entry) . ' is unsupported.');
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: Entry type ' . gettype($entry) . ' is unsupported (this->callbackInstance=' . $this->getCallbackInstance() . ').');
 
-                       // @TODO Extend this somehow?
-                       $entry2 = gettype($entry);
+                       // At least take all keys from array
+                       $entry2 = gettype($entry) . ':' . implode(':', array_keys($entry));
                }
 
                // Construct string which we shall hash
@@ -340,7 +340,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                foreach ($groupNames as $groupName) {
                        // Clear this group
                        $this->clearGroup($groupName);
-               } // END - foreach
+               }
        }
 
        /**
@@ -349,19 +349,19 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @param       $groupName      Name of an existing group to clear
         * @return      void
         */
-       protected function clearGroup ($groupName) {
+       protected function clearGroup (string $groupName) {
                // Does this group exist?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
                        throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
-               } // END - if
+               }
 
                // Then clear this group list
                $this->listGroups[$groupName]->clearList();
 
                // Clear this list
-               $this->listIndex = array();
-               $this->listEntries = array();
+               $this->listIndex = [];
+               $this->listEntries = [];
        }
        
        /**
@@ -416,7 +416,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                if (!$this->isHashValid($hash)) {
                        // Throw an exception here
                        throw new InvalidListHashException(array($this, $hash, $hashIndex), self::EXCEPTION_INVALID_HASH);
-               } // END - if
+               }
 
                // Now copy the entry
                $entry = $this->listEntries[$hash];
@@ -432,15 +432,15 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
         * @return      $entries        The array with all entries
         * @throws      NoListGroupException    If the specified group is invalid
         */
-       public function getArrayFromProtocolInstance ($groupName) {
+       public function getArrayFromProtocolInstance (string $groupName) {
                // Is the group valid?
                if (!$this->isGroupSet($groupName)) {
                        // Throw the exception here
                        throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
-               } // END - if
+               }
 
                // Init the entries' array
-               $entries = array();
+               $entries = [];
 
                // Get an iterator
                $iteratorInstance = $this->listGroups[$groupName]->getIterator();
@@ -464,7 +464,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
 
                        // Skip to next one
                        $iteratorInstance->next();
-               } // END - while
+               }
 
                // Return the list
                return $entries;
@@ -483,7 +483,7 @@ class BaseList extends BaseFrameworkSystem implements IteratorAggregate, Countab
                if (!$this->isHashValid($hash)) {
                        // Throw an exception here, hashIndex is unknown at this point
                        throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH);
-               } // END - if
+               }
 
                // Set the entry
                $this->listEntries[$hash] = $entryArray;