/*
* CPDictionary.j
* Foundation
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//import "CPRange.j"
import "CPObject.j"
import "CPEnumerator.j"
import "CPException.j"
/* @ignore */
@implementation _CPDictionaryValueEnumerator : CPEnumerator
{
CPEnumerator _keyEnumerator;
CPDictionary _dictionary;
}
- (id)initWithDictionary:(CPDictionary)aDictionary
{
self = [super init];
if (self)
{
_keyEnumerator = [aDictionary keyEnumerator];
_dictionary = aDictionary;
}
return self;
}
- (id)nextObject
{
var key = [_keyEnumerator nextObject];
if (!key)
return nil;
return [_dictionary objectForKey:key];
}
@end
/*
A dictionary is the standard way of passing around key-value pairs in
the Cappuccino framework. It is similar to the
Java map interface,
except all keys are
If you are familiar with dictionaries in Cocoa, you'll notice that
there is no CPMutableDictionary class. The regular setObject: and removeObjectForKey: methods.
In Cappuccino there is no distinction between immutable and mutable classes.
They are all mutable.
*/
@implementation CPDictionary : CPObject
{
}
/*
@ignore
*/
+ (id)alloc
{
return new objj_dictionary();
}
/*
Returns a new empty aDictionary.
@param aDictionary the dictionary to copy key-value pairs from
@return the new aKey.
@param aKey the key for the object's entry
@return the object for the entry
*/
- (id)objectForKey:(CPString)aKey
{
// We should really do this with inlining or something of that nature.
return _buckets[aKey];
//return dictionary_getValue(self, aKey);
}
/*
Instance.objectsForKeys(keys, aNotFoundMarker)
{
var i= keys.length,
objects= CPArray.array();
while(i--)
{
var object= this.objectForKey(keys[i]);
objects.addObject(object==nil?aNotFoundMarker:object);
}
return objects;
}
Instance.valueForKey(aKey)
{
if(aKey.length && aKey[0]=="@") return this.objectForKey(aKey.substr(1));
return base.valueForKey(aKey);
}
//
Instance.addEntriesFromDictionary(aDictionary)
{
var key,
keyEnumerator= aDictionary.keyEnumerator();
while(key= keyEnumerator.nextObject()) this.setObjectForKey(aDictionary.objectForKey(key), key);
}
*/
/*
Removes all the entries from the dictionary.
*/
- (void)removeAllObjects
{
_keys = [];
count = 0;
_buckets = {};
}
/*
Removes the entry for the specified key.
@param aKey the key of the entry to be removed
*/
- (void)removeObjectForKey:(id)aKey
{
dictionary_removeValue(self, aKey);
}
/*
Instance.removeObjectForKey(aKey)
{
var entry= this._dictionary[aKey];
if(entry)
{
var range= CPMakeRange(entry.index, 1);
this._keys.removeObjectsInRange(range);
this._objects.removeObjectsInRange(range);
delete this._dictionary[aKey];
}
}
Instance.setDictionary(aDictionary)
{
this._keys= CPArray.arrayWithArray(aDictionary.allKeys());
this._objects= CPArray.arrayWithArray(aDictionary.allValues());
this._dictionary= { };
var i= this._keys.count();
while(i--) this._dictionary[this._keys[i]]= { object: this._objects[i], index: i };
}
*/
/*
Adds an entry into the dictionary.
@param anObject the object for the entry
@param aKey the entry's key
*/
- (void)setObject:(id)anObject forKey:(id)aKey
{
dictionary_setValue(self, aKey, anObject);
}
/*
Instance.setValueForKey(aValue, aKey)
{
if(!aValue) this.removeObjectForKey(aKey);
else this.setObjectForKey(aValue, aKey);
}
Instance.copy()
{
return CPDictionary.alloc().dictionaryWithDictionary(this);
}
*/
/*
Returns a human readable description of the dictionary.
*/
- (CPString)description
{
var description = @"CPDictionary {\n";
var i = _keys.length;
while (i--)
description += _keys[i] +":"+[_buckets[_keys[i]] description]+"\n";
description += "}";
return description;
}
@end
@implementation CPDictionary (CPCoding)
/*
Initializes the dictionary by unarchiving the data from a coder.
@param aCoder the coder from which the data will be unarchived.
@return the initialized dictionary
*/
- (id)initWithCoder:(CPCoder)aCoder
{
return [aCoder _decodeDictionaryOfObjectsForKey:@"CP.objects"];
}
/*
Archives the dictionary to a provided coder.
@param aCoder the coder to which the dictionary data will be archived.
*/
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder _encodeDictionaryOfObjects:self forKey:@"CP.objects"];
}
@end
objj_dictionary.prototype.isa = CPDictionary;