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,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!)
|
// NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!)
|
||||||
result.toString();
|
result.toString();
|
||||||
|
|
||||||
|
if (messageRegistry.hasMessage()) {
|
||||||
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
|
final CalculatorLogger logger = CalculatorLocatorImpl.getInstance().getLogger();
|
||||||
try {
|
try {
|
||||||
final List<Message> messages = new ArrayList<Message>();
|
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);
|
fireCalculatorEvent(newCalculationEventData(operation, expression, sequenceId), CalculatorEventType.calculation_messages, messages);
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} 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);
|
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);
|
||||||
@ -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
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
public PreparedExpression prepareExpression(@NotNull String expression) throws CalculatorParseException {
|
||||||
|
@ -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