create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / util / Invalidator.java
1 package net.sf.openrocket.util;
2
3 import net.sf.openrocket.logging.LogHelper;
4 import net.sf.openrocket.logging.TraceException;
5 import net.sf.openrocket.startup.Application;
6
7 /**
8  * A class that performs object invalidation functions.
9  * 
10  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
11  */
12 public class Invalidator implements Invalidatable {
13         private static final boolean USE_CHECKS = Application.useSafetyChecks();
14         
15         private static final LogHelper log = Application.getLogger();
16         
17         private final Object monitorable;
18         private TraceException invalidated = null;
19         
20         
21         /**
22          * Sole constructor.  The parameter is used when writing error messages, and
23          * is not referenced otherwise.
24          * 
25          * @param monitorable   the object this invalidator is monitoring (may be null or a descriptive string)
26          */
27         public Invalidator(Object monitorable) {
28                 this.monitorable = monitorable;
29         }
30         
31         
32         /**
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.
35          * 
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.
39          */
40         public boolean check(boolean throwException) {
41                 if (invalidated != null) {
42                         if (throwException) {
43                                 throw new BugException(monitorable + ": This object has been invalidated", invalidated);
44                         } else {
45                                 log.warn(1, monitorable + ": This object has been invalidated",
46                                                 new TraceException("Usage was attempted here", invalidated));
47                         }
48                         return false;
49                 }
50                 return true;
51         }
52         
53         
54         /**
55          * Check whether the object has been invalidated.
56          * @return      <code>true</code> if the object has been invalidated, <code>false</code> otherwise.
57          */
58         public boolean isInvalidated() {
59                 return invalidated != null;
60         }
61         
62         
63         @Override
64         public void invalidate() {
65                 if (USE_CHECKS) {
66                         if (invalidated != null) {
67                                 log.warn(1, monitorable + ": This object has already been invalidated, ignoring", invalidated);
68                         }
69                         invalidated = new TraceException("Invalidation occurred here");
70                 }
71         }
72         
73 }