001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package threaddemo.util;
043:
044: import java.util.EventObject;
045:
046: /**
047: * Event indicating something happened to a two-way support.
048: * Always indicates a state change, not just a return to the same state.
049: * @author Jesse Glick
050: * @see TwoWaySupport
051: */
052: public abstract class TwoWayEvent<DM, UMD, DMD> extends EventObject {
053:
054: private TwoWayEvent(TwoWaySupport<DM, UMD, DMD> s) {
055: super (s);
056: assert s != null;
057: }
058:
059: /**
060: * Get the associated two-way support.
061: * @return the support
062: */
063: public TwoWaySupport<DM, UMD, DMD> getTwoWaySupport() {
064: @SuppressWarnings("unchecked")
065: TwoWaySupport<DM, UMD, DMD> source = (TwoWaySupport<DM, UMD, DMD>) getSource();
066: return source;
067: }
068:
069: /**
070: * Event indicating a derived value has been produced.
071: */
072: public static final class Derived<DM, UMD, DMD> extends
073: TwoWayEvent<DM, UMD, DMD> {
074:
075: private final DM oldValue, newValue;
076: private final DMD derivedDelta;
077: private final UMD underlyingDelta;
078:
079: Derived(TwoWaySupport<DM, UMD, DMD> s, DM oldValue,
080: DM newValue, DMD derivedDelta, UMD underlyingDelta) {
081: super (s);
082: this .oldValue = oldValue;
083: assert newValue != null;
084: this .newValue = newValue;
085: assert oldValue != null ^ derivedDelta == null;
086: this .derivedDelta = derivedDelta;
087: this .underlyingDelta = underlyingDelta;
088: }
089:
090: /**
091: * Get the old value of the derived model.
092: * @return the old value, or null if it was never calculated
093: */
094: public DM getOldValue() {
095: return oldValue;
096: }
097:
098: /**
099: * Get the new value of the derived model.
100: * @return the new value
101: */
102: public DM getNewValue() {
103: return newValue;
104: }
105:
106: /**
107: * Get the change to the derived model.
108: * @return the delta, or null if the old value was null
109: */
110: public DMD getDerivedDelta() {
111: return derivedDelta;
112: }
113:
114: /**
115: * Get the change to the underlying model that triggered this derivation.
116: * Only applicable in case the derived model had been invalidated and
117: * was stale before this derivation.
118: * @return the invalidating change to the underlying model, or null if
119: * the derived model is simply being computed for the first time
120: */
121: public UMD getUnderlyingDelta() {
122: return underlyingDelta;
123: }
124:
125: public String toString() {
126: return "TwoWayEvent.Derived[" + getTwoWaySupport()
127: + ",oldValue=" + oldValue + ",newValue=" + newValue
128: + ",derivedDelta=" + derivedDelta
129: + ",underlyingDelta=" + underlyingDelta + "]";
130: }
131:
132: }
133:
134: /**
135: * Event indicating a derived model has been invalidated.
136: */
137: public static final class Invalidated<DM, UMD, DMD> extends
138: TwoWayEvent<DM, UMD, DMD> {
139:
140: private final DM oldValue;
141: private final UMD underlyingDelta;
142:
143: Invalidated(TwoWaySupport<DM, UMD, DMD> s, DM oldValue,
144: UMD underlyingDelta) {
145: super (s);
146: assert oldValue != null;
147: this .oldValue = oldValue;
148: assert underlyingDelta != null;
149: this .underlyingDelta = underlyingDelta;
150: }
151:
152: /**
153: * Get the old value of the derived model that is now invalid.
154: * @return the old value
155: */
156: public DM getOldValue() {
157: return oldValue;
158: }
159:
160: /**
161: * Get the change to the underlying model that triggered this invalidation.
162: * @return the invalidating change to the underlying model
163: */
164: public UMD getUnderlyingDelta() {
165: return underlyingDelta;
166: }
167:
168: public String toString() {
169: return "TwoWayEvent.Invalidated[" + getTwoWaySupport()
170: + ",oldValue=" + oldValue + ",underlyingDelta="
171: + underlyingDelta + "]";
172: }
173:
174: }
175:
176: /**
177: * Event indicating the derived model was changed and the underlying model recreated.
178: */
179: public static final class Recreated<DM, UMD, DMD> extends
180: TwoWayEvent<DM, UMD, DMD> {
181:
182: private final DM oldValue, newValue;
183: private final DMD derivedDelta;
184:
185: Recreated(TwoWaySupport<DM, UMD, DMD> s, DM oldValue,
186: DM newValue, DMD derivedDelta) {
187: super (s);
188: assert oldValue != null;
189: this .oldValue = oldValue;
190: assert newValue != null;
191: this .newValue = newValue;
192: assert derivedDelta != null;
193: this .derivedDelta = derivedDelta;
194: }
195:
196: /**
197: * Get the old value of the derived model that is now invalid.
198: * @return the old value
199: */
200: public DM getOldValue() {
201: return oldValue;
202: }
203:
204: /**
205: * Get the new value of the derived model.
206: * @return the new value
207: */
208: public DM getNewValue() {
209: return newValue;
210: }
211:
212: /**
213: * Get the change to the derived model that should be applied to the underlying
214: * model as well.
215: * @return the delta to the derived model
216: */
217: public DMD getDerivedDelta() {
218: return derivedDelta;
219: }
220:
221: public String toString() {
222: return "TwoWayEvent.Recreated[" + getTwoWaySupport()
223: + ",oldValue=" + oldValue + ",newValue=" + newValue
224: + ",derivedDelta=" + derivedDelta + "]";
225: }
226:
227: }
228:
229: /**
230: * Event indicating changes in the underlying model were clobbered by changes to
231: * the derived model.
232: */
233: public static final class Clobbered<DM, UMD, DMD> extends
234: TwoWayEvent<DM, UMD, DMD> {
235:
236: private final DM oldValue, newValue;
237: private final DMD derivedDelta;
238:
239: Clobbered(TwoWaySupport<DM, UMD, DMD> s, DM oldValue,
240: DM newValue, DMD derivedDelta) {
241: super (s);
242: this .oldValue = oldValue;
243: assert newValue != null;
244: this .newValue = newValue;
245: assert derivedDelta != null;
246: this .derivedDelta = derivedDelta;
247: }
248:
249: /**
250: * Get the old value of the derived model that is now invalid.
251: * @return the old value, or null if it was never calculated
252: */
253: public DM getOldValue() {
254: return oldValue;
255: }
256:
257: /**
258: * Get the new value of the derived model.
259: * @return the new value
260: */
261: public DM getNewValue() {
262: return newValue;
263: }
264:
265: /**
266: * Get the change to the derived model that should be applied to the underlying
267: * model as well whether it is applicable or not.
268: * @return the delta to the derived model
269: */
270: public DMD getDerivedDelta() {
271: return derivedDelta;
272: }
273:
274: public String toString() {
275: return "TwoWayEvent.Clobbered[" + getTwoWaySupport()
276: + ",oldValue=" + oldValue + ",newValue=" + newValue
277: + ",derivedDelta=" + derivedDelta + "]";
278: }
279:
280: }
281:
282: /**
283: * Event indicating the reference to the derived model was garbage collected.
284: * This does not apply if the support itself was already collected.
285: * Also, only supports overriding {@link TwoWaySupport#createReference} will
286: * ever fire this event, since the default implementation creates a strong
287: * reference that cannot be collected.
288: */
289: public static final class Forgotten<DM, UMD, DMD> extends
290: TwoWayEvent<DM, UMD, DMD> {
291:
292: Forgotten(TwoWaySupport<DM, UMD, DMD> s) {
293: super (s);
294: }
295:
296: public String toString() {
297: return "TwoWayEvent.Forgotten[" + getTwoWaySupport() + "]";
298: }
299:
300: }
301:
302: /**
303: * Event indicating an attempted derivation failed with an exception.
304: * The underlying model is thus considered to be in an inconsistent state.
305: */
306: public static final class Broken<DM, UMD, DMD> extends
307: TwoWayEvent<DM, UMD, DMD> {
308:
309: private final DM oldValue;
310: private final UMD underlyingDelta;
311:
312: private final Exception exception;
313:
314: Broken(TwoWaySupport<DM, UMD, DMD> s, DM oldValue,
315: UMD underlyingDelta, Exception exception) {
316: super (s);
317: this .oldValue = oldValue;
318: this .underlyingDelta = underlyingDelta;
319: assert exception != null;
320: this .exception = exception;
321: }
322:
323: /**
324: * Get the old value of the derived model that is now invalid.
325: * @return the old value, or null if it was never calculated
326: */
327: public DM getOldValue() {
328: return oldValue;
329: }
330:
331: /**
332: * Get the change to the underlying model that triggered this derivation.
333: * Only applicable in case the derived model had been invalidated and
334: * was stale before this derivation.
335: * @return the invalidating change to the underlying model, or null if
336: * the derived model is simply being computed for the first time
337: */
338: public UMD getUnderlyingDelta() {
339: return underlyingDelta;
340: }
341:
342: /**
343: * Get the exception encountered when trying to derive a new model.
344: * @return the exception that prevented a new derived model from being created
345: */
346: public Exception getException() {
347: return exception;
348: }
349:
350: public String toString() {
351: return "TwoWayEvent.Broken[" + getTwoWaySupport()
352: + ",oldValue=" + oldValue + ",underlyingDelta="
353: + underlyingDelta + ",exception=" + exception + "]";
354: }
355:
356: }
357:
358: }
|