Parsing Engine

danbikel.parser
Class SexpSubcatEvent

java.lang.Object
  extended bydanbikel.parser.AbstractEvent
      extended bydanbikel.parser.SexpEvent
          extended bydanbikel.parser.SexpSubcatEvent
All Implemented Interfaces:
Event, Externalizable, MutableEvent, Serializable, SexpConvertible

public class SexpSubcatEvent
extends SexpEvent

Represents an event composed of zero or more Sexp objects and zero or one Subcat object.

Bugs: The fact that no more than one Subcat object can be added to this event type does not follow the general contract of Event, which specifies that when an Event advertises that it can collect elements of a certain type, it should contain some sort of a list of such objects internally, to be accessed via the Event.get(int,int) method. However, since Subcat objects are not simplex entities but are themselves collections, this (current) limitation is not entirely unreasonable.

See Also:
Serialized Form

Field Summary
static Symbol sexpSubcatLabel
          Initial symbol used in the string representation of SexpSubcatEvent objects.
protected  Subcat subcat
          The subcat data member.
 
Fields inherited from class danbikel.parser.SexpEvent
event, sexpLabel
 
Constructor Summary
SexpSubcatEvent()
          Constructs an event containing no components.
SexpSubcatEvent(int initialCapacity)
          Creates a new object capable of efficiently collecting the specified number of components of the type Sexp.
 
Method Summary
 MutableEvent add(int type, Object obj)
          Adds the specified type of object to this event.
 MutableEvent add(Object obj)
          Adds the specified component to this event.
 int canonicalize(Map canonical)
          Canonicalizes the backing Sexp and Subcat components of this event using the specified reflexive map.
 void clear()
          Clears the data stored in this complex event object.
 Event copy()
          Returns a deep copy of this event, using SexpEvent.copy to copy the backing Sexp, and using Event.copy to copy the backing Subcat, if there is one.
 boolean equals(Object o)
          Returns true if o is an instance of Event and if the backing Sexp of o is equal to the backing Sexp of this object; also, this method treats backing Sexp of the two objects equal when one is a symbol and the other is a list of length 1 containing that symbol.
 Object get(int type, int index)
          Gets the indexth component of this event of the specified type.
 Class getClass(int type)
          Gets the class object associated with the specified type index.
 int hashCode()
          Returns the hash code of this event, based on its components.
 int numComponents()
          Returns the number of components of this event: the number of Sexp objects plus the number of Subcat objects.
 int numComponents(int type)
          Returns the number of Sexp components or the number of Subcat components that are part of this event.
 int numTypes()
          Returns 2, as this Event implementation supports only two types.
 void readExternal(ObjectInput in)
          Reconstructs the SexpEvent object that was serialized using writeExternal(ObjectOutput).
 Sexp toSexp()
          If this event has no subcat, then this method simply returns the backing Sexp; otherwise, this method returns a SexpList of the form (sexp subcat), where sexp is the Sexp component(s) of this event and subcat is the result of calling subcat.toSexp().
 String toString()
          Returns a string representation of this object of the form (sexpSubcatLabel event) where event is string representation of the Sexp held by this object.
 int typeIndex(Class cl)
          Gets the type index associated with the specified class.
 void writeExternal(ObjectOutput out)
          Writes a representation of this object to the specified object output stream.
 
Methods inherited from class danbikel.parser.SexpEvent
copySexpEvent, ensureCapacity, ensureCapacity, setSexp
 
Methods inherited from class danbikel.parser.AbstractEvent
genericEquals
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

sexpSubcatLabel

public static final Symbol sexpSubcatLabel
Initial symbol used in the string representation of SexpSubcatEvent objects.

See Also:
toString()

subcat

protected Subcat subcat
The subcat data member.

Constructor Detail

SexpSubcatEvent

public SexpSubcatEvent()
Constructs an event containing no components.


SexpSubcatEvent

public SexpSubcatEvent(int initialCapacity)
Creates a new object capable of efficiently collecting the specified number of components of the type Sexp. The initial capacity does not apply to components of type Subcat, as this Event implementation does not support more than 1 Subcat component.

Parameters:
initialCapacity - the pre-allocated capacity for objects of type Sexp for this event
Method Detail

getClass

public Class getClass(int type)
Gets the class object associated with the specified type index. The two classes/types supported by this Event implementation are Sexp and Subcat.

Specified by:
getClass in interface Event
Overrides:
getClass in class SexpEvent

typeIndex

public int typeIndex(Class cl)
Gets the type index associated with the specified class. The two classes/types supported by this Event implementation are Sexp and Subcat, so this method is guaranteed to return either 0 or 1 for these classes, or -1 if the specified class is neither of these two classes.

Specified by:
typeIndex in interface Event
Overrides:
typeIndex in class SexpEvent

numTypes

public int numTypes()
Returns 2, as this Event implementation supports only two types.

Specified by:
numTypes in interface Event
Overrides:
numTypes in class SexpEvent

numComponents

public int numComponents()
Returns the number of components of this event: the number of Sexp objects plus the number of Subcat objects. The number of Sexp objects is 1 if the backing Sexp is a symbol, or the length of the list otherwise. Due to limitations of the implementation of this event type, there can be at most 1 Subcat object.

Specified by:
numComponents in interface Event
Overrides:
numComponents in class SexpEvent

numComponents

public int numComponents(int type)
Returns the number of Sexp components or the number of Subcat components that are part of this event. Due to limitations of the implementation of this event type, there can be at most 1 Subcat object.

Specified by:
numComponents in interface Event
Overrides:
numComponents in class SexpEvent

get

public Object get(int type,
                  int index)
Gets the indexth component of this event of the specified type. If the type is that for Sexp and the backing Sexp is a symbol, then the specified index must be 0; otherwise, the index must be within the range of the backing SexpList. If the type is that for Subcat, then the specified index must be 0, as this Event implementation only supports the storage of at most 1 Subcat object.

Specified by:
get in interface Event
Overrides:
get in class SexpEvent
Parameters:
type - the type index of the component to get
index - the index of the component to get
Throws:
IndexOutOfBoundsException - if type < 0 or if type >= numTypes() or if index < 0 or if index >= numComponents(type)
See Also:
typeIndex(Class)

add

public MutableEvent add(Object obj)
Adds the specified component to this event.

N.B.: Given that this implementation only supports at most 1 Subcat component, if the specified object is of type Subcat and this event already has such a component, then the existing subcat will be (silently!) replaced by the specified subcat. Put another way, it is an error to add more than one Subcat object to a SexpSubcatEvent object.

Specified by:
add in interface MutableEvent
Overrides:
add in class SexpEvent
Parameters:
obj - the component to add to this event
Returns:
this SexpEvent object

add

public MutableEvent add(int type,
                        Object obj)
Adds the specified type of object to this event. If the specified object type is typeIndex(Subcat.class), then the specified object must be an instance of Subcat and is added as this event's subcat. If the specified object type is typeIndex(Sexp.class), then the object must be of type Sexp and is added to this event's list of Sexp elements.

Specified by:
add in interface MutableEvent
Overrides:
add in class SexpEvent
Parameters:
type - the type of object to be added to this event
obj - the object to be added to this event
Returns:
this SexpSubcatEvent object
See Also:
typeIndex(Class)

canonicalize

public int canonicalize(Map canonical)
Canonicalizes the backing Sexp and Subcat components of this event using the specified reflexive map.

Specified by:
canonicalize in interface Event
Overrides:
canonicalize in class SexpEvent
Parameters:
canonical - a reflexive map used for canonicalization
Returns:
1 if the backing Sexp was a list and was therefore canonicalized, 0 if it was a list but was not canonicalized (and had to be added to canonical) or -1 if this event was a Symbol and was therefore not even eligible for canonicalization

copy

public Event copy()
Returns a deep copy of this event, using SexpEvent.copy to copy the backing Sexp, and using Event.copy to copy the backing Subcat, if there is one.

Bugs: The subcat is not deeply copied, as we are using "caller-copy" semantics, that is, that any thread that wishes to use a modified version of a subcat must take care to copy an existing subcat before making the modification. This convention is potentially dangerous, but is in place for efficiency.

Specified by:
copy in interface Event
Overrides:
copy in class SexpEvent
See Also:
SexpEvent.copy(), Event.copy()

hashCode

public int hashCode()
Returns the hash code of this event, based on its components.

Specified by:
hashCode in interface Event
Overrides:
hashCode in class SexpEvent

equals

public boolean equals(Object o)
Description copied from class: SexpEvent
Returns true if o is an instance of Event and if the backing Sexp of o is equal to the backing Sexp of this object; also, this method treats backing Sexp of the two objects equal when one is a symbol and the other is a list of length 1 containing that symbol.

As per the general contract of Event.equals(Object), the specified object need not be of type SexpEvent, but merely support the same types as this Event, namely Sexp objects, and store the same number of equal Sexp objects in the same order.

Specified by:
equals in interface Event
Overrides:
equals in class SexpEvent

toString

public String toString()
Returns a string representation of this object of the form (sexpSubcatLabel event) where event is string representation of the Sexp held by this object.

Overrides:
toString in class SexpEvent

toSexp

public Sexp toSexp()
If this event has no subcat, then this method simply returns the backing Sexp; otherwise, this method returns a SexpList of the form (sexp subcat), where sexp is the Sexp component(s) of this event and subcat is the result of calling subcat.toSexp().

Specified by:
toSexp in interface SexpConvertible
Overrides:
toSexp in class SexpEvent
See Also:
SexpConvertible, Subcat.toSexp()

clear

public void clear()
Description copied from class: SexpEvent
Clears the data stored in this complex event object.

Specified by:
clear in interface MutableEvent
Overrides:
clear in class SexpEvent

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Description copied from class: SexpEvent
Writes a representation of this object to the specified object output stream.

Specified by:
writeExternal in interface Externalizable
Overrides:
writeExternal in class SexpEvent
Parameters:
out - the stream to which to write a representation of this object
Throws:
IOException - if there is a problem writing to the specified output stream

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Description copied from class: SexpEvent
Reconstructs the SexpEvent object that was serialized using SexpEvent.writeExternal(ObjectOutput).

Specified by:
readExternal in interface Externalizable
Overrides:
readExternal in class SexpEvent
Parameters:
in - the input stream from which to reconstruct a SexpEvent object
Throws:
ClassNotFoundException - if the concrete type of the serialized object read from the specified input stream cannot be found
IOException - if there is a problem reading from the specified input stream

Parsing Engine

Author: Dan Bikel.