Java Reference
In-Depth Information
public static Condition getCondition(Bundle b, ConditionInfo i) {
return new AskUserCondition(bundle, i);
}
public boolean isMutable() {
return false;
}
public boolean isPostponed() {
return true;
}
public boolean isSatisfied() {
return false;
}
public synchronized boolean isSatisfied(Condition[], Dictionary) {
...
}
}
You provide a static factory method B which delegates to the private constructor. The
constructor is initialized with the question you need to ask the user, which comes from
the first argument of a corresponding ConditionInfo object. In the constructor, you
automatically replace any occurrence of ${symbolic-name} in the question with the
symbolic name of the bundle making the permission request. Additionally, if there's a
second argument in the ConditionInfo of ! , you use it as an indication to negate the
eventual result.
The condition is immutable and postponed. Because the condition is postponed,
you can stub out the other isSatisfied() method C .
Now let's look at how to implement the postponed isSatisfied() method, which
is shown in the following listing.
Delegates to
constructor B
C
Stubbed
out
Listing 14.7 AskTheUserCondition isSatisfied() method implementation
public synchronized boolean isSatisfied(Condition[] cs, Dictionary) {
if (alreadyAsked) {
return m_result;
}
Boolean result = ((Boolean) AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
AskTheUser question = new AskTheUser(m_question);
try {
return question.ask() ? Boolean.TRUE : Boolean.FALSE;
} catch (Exception e) {
return Boolean.FALSE;
}
}
}));
}
m_alreadyAsked = true;
if (m_not) {
return (m_result = !result.booleanValue());
} else {
return (m_result = result.booleanValue());
}
}
B
Calls ask()
method
C
Creates
AskTheUser
D
Marks question
as already asked
 
Search WWH ::




Custom Search