[ogs-changes] dist/java/ogs/core Modifier.java,1.1,1.2 Modifiers.java,1.1,1.2
Status: Alpha
Brought to you by:
elemings
From: <ele...@us...> - 2003-05-15 02:14:57
|
Update of /cvsroot/ogs/dist/java/ogs/core In directory sc8-pr-cvs1:/tmp/cvs-serv378/ogs/core Modified Files: Modifier.java Modifiers.java Log Message: See Java ChangeLog (May 14) for details. Index: Modifier.java =================================================================== RCS file: /cvsroot/ogs/dist/java/ogs/core/Modifier.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Modifier.java 7 Jan 2003 07:20:33 -0000 1.1 --- Modifier.java 15 May 2003 02:14:54 -0000 1.2 *************** *** 28,78 **** /** ! * A modifier is an integer value added to another integer value. If ! * the value of the modifier is negative, the modifier is subtracted ! * from the value. */ public class Modifier extends Object { /** ! * An event indicating that a modifier has changed value. */ public class Event extends EventObject { ! private int previousValue; /** ! * Create a new modifier event. The event <EM>must</EM> be created ! * <EM>before</EM> the value of the modifier is actually changed. * ! * @param modifier Modifier that changed value. */ Event (Modifier modifier) { super ((java.lang.Object) modifier); ! this.previousValue = modifier.getValue (); } /** ! * Determine previous value of modifier. * ! * @return Previous value of modifier. */ public int getPreviousValue () { ! return (this.previousValue); } } - private int value = 0; - /** ! * Create a modifier with a zero (0) value. */ public Modifier () { ! // All field declarations above have instance initializers. } /** ! * Create a modifier with a specified value. * ! * @param value Value of the modifier. */ public Modifier (int value) { --- 28,93 ---- /** ! * An integer value added to another integer value. If the value of the ! * modifier is negative, the value of the modifier is subtracted from ! * the other value. The "other" value may be an object that has a ! * modifiable integer attribute. The integer value is encapsulated in ! * a class to allow modifier objects to notify observers of events that ! * cause the value of the modifier to change. <p> */ public class Modifier extends Object { + /* + * TODO: The type of modifier values should probably be <code>short + * int</code>. For some reason though , Java programmers don't seem to + * like any integer type except plain old <code>int</code>. <p> + */ + + private int value = 0; /** ! * An event that allows observers to determine the previous value of ! * the modifier. <em>Modifier events must be created before the ! * modifier value is changed so the event can store the previous ! * value!</em> */ public class Event extends EventObject { ! private int value; /** ! * Create a new modifier event. The event must be created ! * <em>before</em> the value of the modifier is actually changed. * ! * @param modifier Modifier causing this event. */ Event (Modifier modifier) { + /* + * TODO: Make constructor visible only to Modifier.setValue() + * method if possible. + */ super ((java.lang.Object) modifier); ! this.value = modifier.getValue (); } /** ! * Determine the previous value of the modifier. * ! * @return Previous value of the modifier. */ public int getPreviousValue () { ! return (this.value); } } /** ! * Create a new modifier. The value of the new modifier is zero (0). */ public Modifier () { ! this.value = 0; } /** ! * Create a new modifier with a specified value. * ! * @param value Value of this modifier. */ public Modifier (int value) { *************** *** 81,87 **** /** ! * Determine the value of the modifier. * ! * @return Value of the modifier. */ public int getValue () { --- 96,102 ---- /** ! * Determine the value of this modifier. * ! * @return Value of this modifier. */ public int getValue () { *************** *** 90,96 **** /** ! * Change the value of the modifier. Notifies listeners. * ! * @param value Value of the modifier. */ public void setValue (int value) { --- 105,112 ---- /** ! * Change the value of this modifier. This method notifies ! * listeners of this modifier with a modifier event. * ! * @param value Value of this modifier. */ public void setValue (int value) { *************** *** 99,103 **** notifyListeners (event); } - } --- 115,118 ---- Index: Modifiers.java =================================================================== RCS file: /cvsroot/ogs/dist/java/ogs/core/Modifiers.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Modifiers.java 7 Jan 2003 07:20:33 -0000 1.1 --- Modifiers.java 15 May 2003 02:14:55 -0000 1.2 *************** *** 23,26 **** --- 23,27 ---- package ogs.core; + import java.util.EventObject; import java.util.Vector; *************** *** 29,42 **** /** ! * A list of modifiers. */ public class Modifiers extends ogs.support.Object { ! private Vector vector; ! private int value; /** ! * Add a modifier to this list of modifiers. The total value is ! * updated accordingly and observers are notified. * * @param modifier A modifier. --- 30,128 ---- /** ! * A list of modifiers. A modifier list allows objects to observe a ! * single object rather than each modifier in the list. A modifier list ! * intercepts events caused by each modifier in the list. It also ! * generates its own events when a modifier is added or removed from the ! * list. In either case, the list updates a total value, cached ! * internally, and transmits an event to observers of the list. Note ! * that a modifier list behaves just like an STL list. If dynamically ! * allocated modifiers are added to it, they must be deallocated by the ! * caller. */ public class Modifiers extends ogs.support.Object { ! private Vector vector = null; ! private int value = 0; /** ! * An event that allows observers to determine changes to the list. ! * The changes to a modifiers list include the previous total value of ! * the list, the type of event (whether a modifier changed, was added, ! * or removed), and the modifier described by the event type. ! * <em>Modifier list events must be created before the list is changed ! * so the event can store this information!</em> ! */ ! public class Event extends EventObject { ! ! private int value; ! private int type; ! private Modifier modifier; ! ! /** A constant that indicates a modifier was added to the list. */ ! public static final int ADDED = 1; ! ! /** A constant that indicates a modifier was removed from the list. */ ! public static final int REMOVED = 2; ! ! /** A constant that indicates a modifier in the list changed value. */ ! public static final int CHANGED = 3; ! ! /** ! * Create a new modifier list event. The event must be created ! * <em>before</em> the list of modifiers is changed. ! * ! * @param modifiers Modifier list causing this event. ! * @param type Type of this event. ! * @param modifier Modifier referenced by this event. ! */ ! public Event (Modifiers modifiers, int type, Modifier modifier) { ! super ((java.lang.Object) modifiers); ! this.value = modifiers.getValue (); ! this.type = type; ! this.modifier = modifier; ! } ! ! /** ! * Determine the previous value of this list of modifiers. ! * ! * @return Previous value of the list of modifiers. ! */ ! public int getPreviousValue () { ! return (this.value); ! } ! ! /** ! * Determine the type of this event. The modifier that caused this ! * type of event can be determined using the getModifier() method. ! * ! * @return Type of this event. ! */ ! public int getType () { ! return (this.type); ! } ! ! /** ! * Determine the modifier described by the type of this event. The ! * type of modifier event can be determined using the getType() ! * method. ! * ! * @return Modifier described by the type of this event. ! */ ! public Modifier getModifier () { ! return (this.modifier); ! } ! } ! ! /** ! * Create an empty list of modifiers. ! */ ! public Modifiers () { ! this.vector = new Vector (); ! this.value = 0; ! } ! ! /** ! * Add a modifier to this list of modifiers. The total value of the ! * list is updated and observers are notified of the event. * * @param modifier A modifier. *************** *** 44,60 **** public void addModifier (Modifier modifier) { // Add modifier to list. ! if (! vector.contains ((Object) modifier)) { ! modifier.addListener ((Object) this); ! vector.add ((Object) modifier); ! // Update total value of modifiers. ! this.value += modifier.getValue (); ! notifyListeners (null); ! } } /** ! * Remove a modifier from this list of modifiers. The total value is ! * updated accordingly and observers are notified. * * @param modifier A modifier. --- 130,145 ---- public void addModifier (Modifier modifier) { // Add modifier to list. ! modifier.addListener ((Object) this); ! vector.add ((Object) modifier); ! // Update total value of modifiers. ! Event event = new Event (this, Event.ADDED, modifier); ! this.value += modifier.getValue (); ! notifyListeners (event); } /** ! * Remove a modifier from this list of modifiers. The total value of ! * the list is updated and observers are notified of the event. * * @param modifier A modifier. *************** *** 66,71 **** // Update total value of modifiers. this.value -= modifier.getValue (); ! notifyListeners (null); } } --- 151,157 ---- // Update total value of modifiers. + Event event = new Event (this, Event.REMOVED, modifier); this.value -= modifier.getValue (); ! notifyListeners (event); } } *************** *** 81,90 **** /** ! * Update the total value when a modifier changes value. * ! * @param event An event. */ ! public void handleEvent (Modifier.Event event) { } } --- 167,194 ---- /** ! * Update the total value for this list of modifiers. The total value ! * is updated when the value of a modifier in the list changes values. ! * Events other than modifier events are ignored. * ! * @param eventObject An event object. */ ! public void handleEvent (java.util.EventObject eventObject) { ! try { ! Modifier.Event event = (Modifier.Event) eventObject; ! Modifier modifier = (Modifier) event.getSource (); + /* The cached value is updated by subtracting the previous value + * of the modifier that changed and then adding its new value. + * The only other way to update the cached value would be to reset + * it to zero (0), then add every modifier in the list. + */ + Modifiers.Event newEvent = + new Modifiers.Event (this, Event.CHANGED, modifier); + this.value -= event.getPreviousValue (); + this.value += modifier.getValue (); + notifyListeners (newEvent); + } catch (ClassCastException exception) { + // Event is not a modifier event. Ignore it. + } } } |