class KXI::Collections::ArrayCollection

Makes array enumerable

Public Class Methods

new(array = []) click to toggle source

Instantiates the {KXI::Collections::ArrayCollection} class @param array [Array] Array for enumeration

Calls superclass method KXI::Collections::Enumerable::new
# File lib/kxi/collections/array_collection.rb, line 9
def initialize(array = [])
        super()
        @arr = array
end

Public Instance Methods

[](index) click to toggle source

Obtains value in array at specific index @param index [Number] Index in array to obtain @return [Object] Value in array at specified index @raise [KXI::Exceptions::OutOfRangeException] Raised when given index is out of range of array

# File lib/kxi/collections/array_collection.rb, line 24
def [](index)
        raise(KXI::Exceptions::OutOfRangeException.new(index)) if @arr.length == 0
        raise(KXI::Exceptions::OutOfRangeException.new(index, 0, @arr.length - 1)) if index < 0 or index >= @arr.length
        lock do
                return @arr[index]
        end
end
[]=(index, value) click to toggle source

Sets value of array at specific index @param index [Number] Index in array to set @param value [Object] Value to set @return [Object] Set value @raise [KXI::Exceptions::OutOfRangeException] Raised when given index is out of range of array @raise [KXI::Exceptions::CollectionException] Raised when collection cannot be locked for writing

# File lib/kxi/collections/array_collection.rb, line 38
def []=(index, value)
        raise(KXI::Exceptions::OutOfRangeException.new(index)) if @arr.length == 0
        raise(KXI::Exceptions::OutOfRangeException.new(index, 0, @arr.length - 1)) if index < 0 or index >= @arr.length
        lock(true) do
                @arr[index] = value
        end
        return value
end
add(value) click to toggle source

Adds value into array @param value [Object] Value to set @return [Number] Assigned index @raise [KXI::Exceptions::CollectionException] Raised when collection cannot be locked for writing

# File lib/kxi/collections/array_collection.rb, line 51
def add(value)
        idx = @arr.length
        lock(true) do
                @arr.push(value)
        end
        return idx
end
remove_at(index) click to toggle source

Removes item at specific index @param index [Number] Index to remove @return [Object] Removed item @raise [KXI::Exceptions::OutOfRangeException] Raised when given index is out of range of array @raise [KXI::Exceptions::CollectionException] Raised when collection cannot be locked for writing

# File lib/kxi/collections/array_collection.rb, line 64
def remove_at(index)
        raise(KXI::Exceptions::OutOfRangeException.new(index)) if @arr.length == 0
        raise(KXI::Exceptions::OutOfRangeException.new(index, 0, @arr.length - 1)) if index < 0 or index >= @arr.length
        ret = nil
        lock(true) do
                ret = @arr.delete_at(index)
        end
        return ret
end

Private Instance Methods

create_enumerator() click to toggle source

Creates a new {KXI::Collections::Enumerator} bound to this instance @return [KXI::Collections::Enumerator] Enumerator bound to this instance

# File lib/kxi/collections/array_collection.rb, line 16
def create_enumerator
        ArrayEnumerator.new(@arr)
end