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 {
|
public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
||||||
|
|
||||||
|
/*
|
||||||
|
**********************************************************************
|
||||||
|
*
|
||||||
|
* CONSTANTS
|
||||||
|
*
|
||||||
|
**********************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
// one minute
|
||||||
|
private static final long PREFERENCE_CHECK_INTERVAL = 1000L * 60L;
|
||||||
|
|
||||||
|
/*
|
||||||
|
**********************************************************************
|
||||||
|
*
|
||||||
|
* FIELDS
|
||||||
|
*
|
||||||
|
**********************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
private final CalculatorEventContainer calculatorEventContainer = new ListCalculatorEventContainer();
|
||||||
|
|
||||||
@ -56,10 +75,29 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
|
|
||||||
private volatile boolean calculateOnFly = true;
|
private volatile boolean calculateOnFly = true;
|
||||||
|
|
||||||
|
private volatile long lastPreferenceCheck = 0L;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
**********************************************************************
|
||||||
|
*
|
||||||
|
* CONSTRUCTORS
|
||||||
|
*
|
||||||
|
**********************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
public CalculatorImpl() {
|
public CalculatorImpl() {
|
||||||
this.addCalculatorEventListener(this);
|
this.addCalculatorEventListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
**********************************************************************
|
||||||
|
*
|
||||||
|
* METHODS
|
||||||
|
*
|
||||||
|
**********************************************************************
|
||||||
|
*/
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private CalculatorEventData nextEventData() {
|
private CalculatorEventData nextEventData() {
|
||||||
long eventId = counter.incrementAndGet();
|
long eventId = counter.incrementAndGet();
|
||||||
@ -163,7 +201,7 @@ public class CalculatorImpl implements Calculator, CalculatorEventListener {
|
|||||||
@NotNull String expression,
|
@NotNull String expression,
|
||||||
@Nullable MessageRegistry mr) {
|
@Nullable MessageRegistry mr) {
|
||||||
|
|
||||||
CalculatorLocatorImpl.getInstance().getPreferenceService().checkPreferredPreferences(false);
|
checkPreferredPreferences();
|
||||||
|
|
||||||
PreparedExpression preparedExpression = null;
|
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!)
|
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||||
result.toString();
|
result.toString();
|
||||||
|
|
||||||
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
|
if (messageRegistry.hasMessage()) {
|
||||||
try {
|
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
|
||||||
final List<Message> messages = new ArrayList<Message>();
|
try {
|
||||||
while (messageRegistry.hasMessage()) {
|
final List<Message> messages = new ArrayList<Message>();
|
||||||
messages.add(messageRegistry.getMessage());
|
while (messageRegistry.hasMessage()) {
|
||||||
}
|
messages.add(messageRegistry.getMessage());
|
||||||
if (!messages.isEmpty()) {
|
}
|
||||||
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
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);
|
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_result, data);
|
||||||
|
|
||||||
} catch (AbstractJsclArithmeticException e) {
|
} 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
|
@Override
|
||||||
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
||||||
return preprocessor.process(expression);
|
return preprocessor.process(expression);
|
||||||
|
@ -9,7 +9,9 @@
|
|||||||
<TextView a:id="@+id/calculation_messages_text_view"
|
<TextView a:id="@+id/calculation_messages_text_view"
|
||||||
a:layout_width="0dp"
|
a:layout_width="0dp"
|
||||||
a:layout_height="wrap_content"
|
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
|
<Button
|
||||||
a:id="@+id/fix_button"
|
a:id="@+id/fix_button"
|
||||||
|
@ -20,7 +20,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class AndroidCalculatorPreferenceService implements CalculatorPreferenceService {
|
public class AndroidCalculatorPreferenceService implements CalculatorPreferenceService {
|
||||||
|
|
||||||
// ont hour
|
// one hour
|
||||||
private static final Long PREFERRED_PREFS_INTERVAL_TIME = 1000L * 60L * 60L;
|
private static final Long PREFERRED_PREFS_INTERVAL_TIME = 1000L * 60L * 60L;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
Loading…
Reference in New Issue
Block a user