This commit is contained in:
Sergey Solovyev 2012-11-18 16:22:14 +04:00
parent 27ae9ea12c
commit 344018a44c
3 changed files with 68 additions and 17 deletions

View File

@ -38,6 +38,25 @@ import java.util.concurrent.atomic.AtomicLong;
*/
public class CalculatorImpl implements Calculator, CalculatorEventListener {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
// one minute
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@NotNull
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
@ -56,10 +75,29 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
private volatile boolean calculateOnFly = true;
private volatile long lastPreferenceCheck = 0L;
/*
**********************************************************************
*
* CONSTRUCTORS
*
**********************************************************************
*/
public CalculatorImpl() {
this.addCalculatorEventListener(this);
}
/*
**********************************************************************
*
* METHODS
*
**********************************************************************
*/
@NotNull
private CalculatorEventData nextEventData() {
long eventId = counter.incrementAndGet();
@ -163,7 +201,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
@NotNull String expression,
@Nullable MessageRegistry mr) {
CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false);
checkPreferredPreferences();
PreparedExpression preparedExpression = null;
@ -190,6 +228,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
result.toString();
if (messageRegistry.hasMessage()) {
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
try {
final List<Message> messages = new ArrayList<Message>();
@ -200,9 +239,10 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
}
} catch (Throwable e) {
// todo serso: not good be we need proper synchronization
// todo serso: not good but we need proper synchronization
logger.error("Calculator", e.getMessage(), e);
}
}
final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result);
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
@ -228,6 +268,15 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
}
}
private void checkPreferredPreferences() {
final long currentTime = System.currentTimeMillis();
if ( currentTime - lastPreferenceCheck > PREFERENCE_CHECK_INTERVAL ) {
lastPreferenceCheck = currentTime;
CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false);
}
}
@NotNull
@Override
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {

View File

@ -9,7 +9,9 @@
<TextView a:id="@+id/calculation_messages_text_view"
a:layout_width="0dp"
a:layout_height="wrap_content"
a:layout_weight="5"/>
a:layout_weight="5"
a:layout_gravity="fill"
a:text="@+id/calculation_messages_text_view"/>
<Button
a:id="@+id/fix_button"

View File

@ -20,7 +20,7 @@ import java.util.List;
*/
public class AndroidCalculatorPreferenceService implements CalculatorPreferenceService {
// ont hour
// one hour
private static final Long PREFERRED_PREFS_INTERVAL_TIME = 1000L * 60L * 60L;
@NotNull