Collection Framework - craterdog-archives/js-bali-component-framework GitHub Wiki

Collection Framework

Collection components contain zero or more items in a sequence, each of which is also a component. Items can be added and removed from a collection. In fact, an item may be contained in multiple collections at the same time. The lifetime of an item is not necessarily tied to the lifetime of a collection that contains it. Different types of collections serve different purposes, the following flow chart shows how to choose the right collection type for your needs.

Collection Flow Chart

The following UML class diagram shows a high-level view of the collection component types (gray) and the agent classes (green) with which they interact.

Collection Framework

Abstract type names are italicized whereas concrete type names are not.

Collection Classes

The following concrete classes implement the collection classes in this framework.

Association

An association maintains a binding between a key element and a value component. It is used by the catalog collection as its item type.

Catalog

A catalog maintains a sequence of associations between key elements and value components. The order of the associations within the catalog will be the same order in which they were added to the catalog, but the associations can be sorted into their natural order if desired.

List

A list maintains a sequence of component items. The order of the items within the list will be the same order in which they were added to the list, but again the items can be sorted into their natural order if desired.

Queue

A queue maintains a sequence of items that are added to and removed from the queue using "first in, first out" (aka FIFO) semantics. It behaves like a line of people waiting to be served. The first person in line is served first.

Range

A range [first..last] captures the first and last elements in an implicit or explicit sequence of elements. An implicit range of integers can be iterated over using an iterator. Either the first or last element of the range may be omitted in which case either all elements from the beginning of the sequence or all elements to the end of the sequence are included respectively.

Set

A set maintains an ordered sequence of items that are automatically placed in their natural order by the set. A set does not allow duplicate items to be added.

Stack

A stack maintains a sequence of items that are added to and removed from the stack using "last in, first out" (aka LIFO) semantics. It behaves like a dish stack in a cafeteria where all dishes are added and removed from the top of the stack.

Agent Classes

Iterator

An iterator, not surprisingly, is used to iterate over the items in a collection. It can move forward or backwards through the items. The iterator is positioned in the slots between items starting with the slot before the first item and ending in the slot after the last item. From a specific slot it can either retrieve the next item or the previous item. After retrieving the item the iterator moves to the next (or previous) slot.

Comparator

A comparator is used to compare two components (often items in a collection) to determine their ordering. A function implementing a comparison algorithm may be specified when creating the comparator. The default comparison algorithm determines the natural ordering of the two components.

Sorter

A sorter implements a specific sorting algorithm that can be used to sort a sortable collection like a list or catalog. The sorting algorithm used by the Bali Component Framework™ is a merge sort.