Changes
This commit is contained in:
parent
27ae9ea12c
commit
344018a44c
@ -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,21 +228,23 @@ 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();
|
||||
|
||||
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
|
||||
try {
|
||||
final List<Message> messages = new ArrayList<Message>();
|
||||
while (messageRegistry.hasMessage()) {
|
||||
messages.add(messageRegistry.getMessage());
|
||||
}
|
||||
if (!messages.isEmpty()) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
||||
if (messageRegistry.hasMessage()) {
|
||||
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
|
||||
try {
|
||||
final List<Message> messages = new ArrayList<Message>();
|
||||
while (messageRegistry.hasMessage()) {
|
||||
messages.add(messageRegistry.getMessage());
|
||||
}
|
||||
if (!messages.isEmpty()) {
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// todo serso: not good but we need proper synchronization
|
||||
logger.error("Calculator", e.getMessage(), e);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
// todo serso: not good be we need proper synchronization
|
||||
logger.error("Calculator", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result);
|
||||
final CalculatorOutput data = CalculatorOutputImpl.newOutput(operation.getFromProcessor().process(result), operation, result);
|
||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
|
||||
|
||||
} catch (AbstractJsclArithmeticException e) {
|
||||
@ -228,7 +268,16 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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 {
|
||||
return preprocessor.process(expression);
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user