﻿//Copyright Set-Soft A.Ş.
//http://www.set-soft.com 

///<reference path="Type.js" />
TYPE.registerNamespace('Web.Collections');

Web.Collections.List=function()
{
    this.items=new Array();
    this.count=0;
    
    this.__refreshItemCount=function()
    {
        this.count = this.items.length;
    }
    
    this.add=function(item)
    {
       this.items.push(item);
       this.__refreshItemCount();
    }
    
    this.getItem=function(itemIndex)
    {
        if(this.items.length>itemIndex)
        {
            return this.items[itemIndex];
        }
        return null;
    }
    
    this.getItemIndex=function(item)
    {
        for(var i=0;i<this.items.length;i++)
            if(this.items[i] == item)
                return i;
        return -1;
    }

    this.remove=function(item)
    {
        var itemIndex = this.getItemIndex(item);
        if(itemIndex != -1)
            this.items.splice(itemIndex,1);
        this.__refreshItemCount();    
    }
    
    this.removeByIndex=function(itemIndex)
    {
        if(this.items.length>itemIndex)
            this.items.splice(itemIndex,1);    
        this.__refreshItemCount();
    }
    
    this.clear=function()
    {
        if(this.items.length>0)
            this.items.splice(0,this.items.length);
        this.__refreshItemCount();
    }
}

Web.Collections.DictionaryEntry=function(entryKey,entryValue)
{
    this.key=entryKey;
    this.value=entryValue;
}

Web.Collections.Dictionary=function()
{
    this.entries=new Web.Collections.List();
    this.count=0;
    
    this.__refreshItemCount=function()
    {
        this.count = this.entries.count; 
    }
    
    this.getKeyIndex=function(key)
    {
        for(var i=0;i<this.entries.count;i++)
            if(this.entries.getItem(i).key==key)
                return i;
        return -1;
    }
    
    this.add=function(key,value)
    {
        if(this.getKeyIndex(key) != -1)
            throw new Web.Core.Error('Key ' + key + ' alredy exists!');
        var entry = new Web.Collections.DictionaryEntry(key,value);
        this.entries.add(entry);
        this.__refreshItemCount();
    }
    
    this.getValue=function(key)
    {
        var entryIndex = this.getKeyIndex(key);
        if(entryIndex == -1)
            throw new Web.Core.Error('Key ' + key + ' not found!');   
        return this.entries.getItem(entryIndex).value;
    }
    
    this.getValueByIndex=function(index)
    {
        return this.entries.getItem(index).value;
    }
    
    this.setValue=function(key,value)
    {
        var entryIndex = this.getKeyIndex(key);
        if(entryIndex == -1)
        {
            this.add(key,value);
        }
        else
        {
            this.entries.getItem(entryIndex).value=value;
        }
    }
    
    this.containsKey=function(key)
    {
        var entryIndex = this.getKeyIndex(key);
        return entryIndex != -1;   
    }
    
    this.remove=function(key)
    {
        var entryIndex = this.getKeyIndex(key);
        if(entryIndex != -1)
            this.entries.removeByIndex(entryIndex); 
        this.__refreshItemCount();
    }
    
    this.removeByIndex=function(index)
    {
        this.entries.removeByIndex(index);
        this.__refreshItemCount();
    }
    
    this.clear=function()
    {
        this.entries.clear();
        this.__refreshItemCount();
    }
}