net.ponder2.objects
Class P2Hash

java.lang.Object
  extended by net.ponder2.objects.P2Object
      extended by net.ponder2.objects.P2Hash
All Implemented Interfaces:
java.io.Serializable, java.util.Map<java.lang.String,P2Object>, P2Serializable, ManagedObject, Ponder2Message
Direct Known Subclasses:
Event

public class P2Hash
extends P2Object
implements ManagedObject, java.util.Map<java.lang.String,P2Object>

Implements a dictionary that stores objects indexed by keys. The keys are string types.

Version:
$Id:$
Author:
Kevin Twidle
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
P2Hash()
          Creates an instance of a P2Hash with no contents
P2Hash(java.util.Map<java.lang.String,P2Object> hash)
          Creates an instance of a P2Hash with initials value(s)
 
Method Summary
 P2Hash asHash()
          Returns itself as a Ponder2 hash
 java.util.Map<java.lang.String,P2Object> asMap()
          gets the complete arguments in name, value pairs
protected  P2Array asPonder2Array()
          answers an array containing name, values, name, value ...
 void clear()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set<java.util.Map.Entry<java.lang.String,P2Object>> entrySet()
           
 P2Object get(java.lang.Object key)
           
 boolean isEmpty()
           
protected  boolean isMutable()
          To be overridden by P2Hash subclasses.
 java.util.Set<java.lang.String> keySet()
           
protected  java.lang.String[] names()
          returns an array of the names of the objects in this domain
 P2Object operation_at_ifAbsent(P2Object source, java.lang.String aKey, P2Block aBlock)
          Answer the value associated with the given key.
 P2Object operation_at_put(java.lang.String aKey, P2Object anObject)
          Answer anObject.
protected  P2Object operation_at(java.lang.String aKey)
          Answer the value associated with the given key.
 P2Array operation_collect(P2Object source, P2Block aBlock)
          Takes a block and executes the block once for each entry in the hash.
 void operation_do(P2Object source, P2Block aBlock)
          Takes a block and executes the block once for each entry in the hash.
protected  boolean operation_has(P2Object anObject)
          Answer true if anObject is in the receiver
protected  boolean operation_has(java.lang.String aKey)
          Answer true if the given key exists otherwise false
protected  P2Object operation_listNames()
          answers an array containing the names of all the domain's entries.
protected  P2Object operation_listObjects()
          answers an array of Managed Object names containing all the entries in the domain.
protected  P2Object operation_remove(java.lang.String aKey)
          Answer the value associated with the given key and remove it from the table.
protected  void operation_removeAll()
          Removes all objects stored in the receiver.
 P2Object put(java.lang.String key, P2Object value)
           
 void putAll(java.util.Map<? extends java.lang.String,? extends P2Object> map)
           
 P2Object readXml(com.twicom.qdparser.TaggedElement xml, java.util.Map<java.lang.Integer,P2Serializable> read)
           
 P2Object remove(java.lang.Object key)
           
protected  boolean removeObject(P2Object anObject)
          Removes anObject from the receiver.
protected  void set(P2Hash hash)
          sets the hash with a copy of the contents of another P2Hash
 int size()
          Answer the number of elements in the receiver.
 java.lang.String toString()
           
 java.util.Collection<P2Object> values()
           
 com.twicom.qdparser.TaggedElement writeXml(java.util.Set<P2Object> written)
           
 
Methods inherited from class net.ponder2.objects.P2Object
asArray, asBlock, asBoolean, asClass, asDouble, asFloat, asInteger, asLong, asNumber, asP2Array, asString, create, create, create, create, create, create, create, create, create, create, create, create, create, create, create, create, create, fromXml, getManagedObject, getOID, getSMC, operation, operation, setSMC
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

P2Hash

public P2Hash()
Creates an instance of a P2Hash with no contents


P2Hash

public P2Hash(java.util.Map<java.lang.String,P2Object> hash)
Creates an instance of a P2Hash with initials value(s)

Parameters:
hash - a Java Map containing P2Values
Method Detail

asHash

public P2Hash asHash()
              throws Ponder2ArgumentException
Description copied from class: P2Object
Returns itself as a Ponder2 hash

Overrides:
asHash in class P2Object
Returns:
the hash value
Throws:
Ponder2ArgumentException

asMap

public java.util.Map<java.lang.String,P2Object> asMap()
gets the complete arguments in name, value pairs

Returns:
a Map of the named arguments

set

protected void set(P2Hash hash)
sets the hash with a copy of the contents of another P2Hash

Parameters:
hash - the hash to be copied

operation_do

public void operation_do(P2Object source,
                         P2Block aBlock)
                  throws Ponder2Exception
Takes a block and executes the block once for each entry in the hash. The arguments to the block are the name of the entry and the value of the entry. Answers with the receiver.

Throws:
Ponder2Exception

operation_collect

public P2Array operation_collect(P2Object source,
                                 P2Block aBlock)
                          throws Ponder2Exception
Takes a block and executes the block once for each entry in the hash. The arguments to the block are the name of the entry and the value of the entry. The result of each block is collected and returned in an array.

Throws:
Ponder2Exception

names

protected java.lang.String[] names()
returns an array of the names of the objects in this domain

Returns:
a String array with the object names

operation_listNames

protected P2Object operation_listNames()
answers an array containing the names of all the domain's entries.

Returns:
a Ponder2 array with the object names

operation_listObjects

protected P2Object operation_listObjects()
answers an array of Managed Object names containing all the entries in the domain.

Returns:
an array containing all the object in this domain

asPonder2Array

protected P2Array asPonder2Array()
answers an array containing name, values, name, value ... entries from the receiver

Returns:
a Ponder2 array containing the hash

operation_at_put

public P2Object operation_at_put(java.lang.String aKey,
                                 P2Object anObject)
Answer anObject. Store anObject in the table with aKey. If aKey already exists the previous value is overridden.


operation_remove

protected P2Object operation_remove(java.lang.String aKey)
Answer the value associated with the given key and remove it from the table. Answer Null if it is not found. TODO Should fail if not found


removeObject

protected boolean removeObject(P2Object anObject)
                        throws Ponder2Exception
Removes anObject from the receiver. All copies of anObject will be removed. Answers true if one or more were removed.

Parameters:
anObject - the object to be removed
Returns:
true if an object was removed
Throws:
Ponder2Exception

operation_removeAll

protected void operation_removeAll()
Removes all objects stored in the receiver. Answers self.


operation_has

protected boolean operation_has(java.lang.String aKey)
Answer true if the given key exists otherwise false


operation_has

protected boolean operation_has(P2Object anObject)
Answer true if anObject is in the receiver


operation_at_ifAbsent

public P2Object operation_at_ifAbsent(P2Object source,
                                      java.lang.String aKey,
                                      P2Block aBlock)
                               throws Ponder2Exception
Answer the value associated with the given key. If not found evaluate block (with no arguments) and return its result

Throws:
Ponder2Exception

operation_at

protected P2Object operation_at(java.lang.String aKey)
                         throws Ponder2ArgumentException
Answer the value associated with the given key. Throws a Ponder2ArgumentException error if not found.

Throws:
Ponder2ArgumentException

clear

public void clear()
Specified by:
clear in interface java.util.Map<java.lang.String,P2Object>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<java.lang.String,P2Object>

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map<java.lang.String,P2Object>

entrySet

public java.util.Set<java.util.Map.Entry<java.lang.String,P2Object>> entrySet()
Specified by:
entrySet in interface java.util.Map<java.lang.String,P2Object>

get

public P2Object get(java.lang.Object key)
Specified by:
get in interface java.util.Map<java.lang.String,P2Object>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<java.lang.String,P2Object>

keySet

public java.util.Set<java.lang.String> keySet()
Specified by:
keySet in interface java.util.Map<java.lang.String,P2Object>

put

public P2Object put(java.lang.String key,
                    P2Object value)
Specified by:
put in interface java.util.Map<java.lang.String,P2Object>

putAll

public void putAll(java.util.Map<? extends java.lang.String,? extends P2Object> map)
Specified by:
putAll in interface java.util.Map<java.lang.String,P2Object>

remove

public P2Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map<java.lang.String,P2Object>

size

public int size()
Answer the number of elements in the receiver.

Specified by:
size in interface java.util.Map<java.lang.String,P2Object>
Returns:
the number of elements held

values

public java.util.Collection<P2Object> values()
Specified by:
values in interface java.util.Map<java.lang.String,P2Object>

isMutable

protected boolean isMutable()
To be overridden by P2Hash subclasses. Affects whether the hash is copied remotely or a reference is sent

Returns:
true if mutable

writeXml

public com.twicom.qdparser.TaggedElement writeXml(java.util.Set<P2Object> written)
                                           throws Ponder2OperationException
Specified by:
writeXml in interface P2Serializable
Overrides:
writeXml in class P2Object
Throws:
Ponder2OperationException

readXml

public P2Object readXml(com.twicom.qdparser.TaggedElement xml,
                        java.util.Map<java.lang.Integer,P2Serializable> read)
                 throws Ponder2OperationException,
                        Ponder2ArgumentException
Specified by:
readXml in interface P2Serializable
Specified by:
readXml in class P2Object
Throws:
Ponder2OperationException
Ponder2ArgumentException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2008 Imperial College. All Rights Reserved.