|
ref_send API 2.17 defensive programming in Java |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.joe_e.var.Milestone<T>
public class Milestone<T>
A variable that can be set only once.
An instance of this class is like a final
variable that can
either be initialized, or left uninitialized. This is useful for keeping
track of whether or not a particular branch of code has been executed. For
example, say you've got some code that operates on a set of objects.
Sometimes, the set is mutated, sometimes not. To remember whether or not a
mutation was made, the set's update method could be coded as:
private final Milestone<Boolean> dirty = Milestone.make(); … public void add(final Object x) { objectSet.add(x); dirty.set(true); }
When the code is done using the set, a commit() method can check to see if any updates need to be written:
public void commit() { if (dirty.is()) { // write out the new set … } }
By using this class, instead of a normal variable, you can better communicate to a code reviewer that a variable is only assigned once. The variable declaration alone ensures this property is enforced, without the need to inspect all the code that can access the variable.
This class is named Milestone, since it keeps track of whether or not a program's execution reached a certain point or not. This program meaningful execution point is a milestone.
Method Summary | ||
---|---|---|
T |
get()
Gets the milestone marker. |
|
boolean |
is()
Has the milestone been passed? |
|
static
|
make()
Constructs an instance. |
|
boolean |
set(T marker)
Sets the milestone marker, if not already set. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static <T> Milestone<T> make()
public boolean is()
true
if set,
else false
public T get()
null
if not setpublic boolean set(T marker)
marker
- initialized value of the variable
false
if previously set, else true
|
ref_send API 2.17 defensive programming in Java |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 1998-2009 Waterken Inc. under the terms of the MIT X license.