1 package net.sf.openrocket.util;
3 import net.sf.openrocket.logging.LogHelper;
4 import net.sf.openrocket.logging.TraceException;
5 import net.sf.openrocket.startup.Application;
8 * A class that performs object invalidation functions.
10 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12 public class Invalidator implements Invalidatable {
13 private static final boolean USE_CHECKS = Prefs.useSafetyChecks();
15 private static final LogHelper log = Application.getLogger();
17 private final Object monitorable;
18 private TraceException invalidated = null;
22 * Sole constructor. The parameter is used when writing error messages, and
23 * is not referenced otherwise.
25 * @param monitorable the object this invalidator is monitoring (may be null or a descriptive string)
27 public Invalidator(Object monitorable) {
28 this.monitorable = monitorable;
33 * Check whether the object has been invalidated. Depending on the parameter either
34 * a BugException is thrown or a warning about the object access is logged.
36 * @param throwException whether to throw an exception or log a warning.
37 * @return <code>true</code> when the object has not been invalidated, <code>false</code> if it has
38 * @throws BugException if the object has been invalidated and <code>throwException</code> is true.
40 public boolean check(boolean throwException) {
41 if (invalidated != null) {
43 throw new BugException(monitorable + ": This object has been invalidated", invalidated);
45 log.warn(1, monitorable + ": This object has been invalidated",
46 new TraceException("Usage was attempted here", invalidated));
55 * Check whether the object has been invalidated.
56 * @return <code>true</code> if the object has been invalidated, <code>false</code> otherwise.
58 public boolean isInvalidated() {
59 return invalidated != null;
64 public void invalidate() {
66 if (invalidated != null) {
67 log.warn(1, monitorable + ": This object has already been invalidated, ignoring", invalidated);
69 invalidated = new TraceException("Invalidation occurred here");