com.jp.comp.calculator
Class Calculator

java.lang.Object
  extended bycom.jp.comp.calculator.Calculator

public class Calculator
extends java.lang.Object

Performs calculator's computing. The set of operations performed by the calculator can be changed. If there is a need to calclulate x^y a new operation should be created implementing CaclulatorOperation and added to the Calculator by addOperation method

See Also:
CalculatorPanel

Field Summary
protected  CalculatorEvent fCalculatorEvent
           
protected  java.util.Vector fCalculatorListeners
           
protected  java.util.HashMap fCalculatorOperations
           
protected  CalculatorResult fCalculatorResult
           
protected  CalculatorOperation fDefaultClearOperation
           
protected  boolean fProcessingError
           
protected  java.util.HashMap fStateMap
           
static CalculatorOperation MEMORY_ADDITION
          Default memory addition operation
static java.lang.String MEMORY_ADDITION_KEY
          Memory addition operation's key
static CalculatorOperation MEMORY_CLEAR
          Default memory clear operation
static java.lang.String MEMORY_CLEAR_KEY
          Memory clear operation's key
static CalculatorOperation MEMORY_RECALL
          Default memory recall operation
static java.lang.String MEMORY_RECALL_KEY
          Memory recall operation's key
static CalculatorOperation MEMORY_STORE
          Default memory store operation
static java.lang.String MEMORY_STORE_KEY
          Memory store operation's key
static CalculatorOperation MEMORY_SUBTRACTION
          Default memory subtaction operation
static java.lang.String MEMORY_SUBTRACTION_KEY
          Memory subtraction operation's key
static java.lang.String PROPERTY_DEFAULT_CLEAR_OPERATION
          Property name for the default clear operation
static java.lang.String PROPERTY_HASHED_OPERATION
          Property name for the hashed operations
static java.lang.String PROPERTY_MEMORY
          Property name for the memory
static java.lang.String PROPERTY_RESULT
          Property name for the result
protected  java.util.Vector propertyChangeListeners
           
static CalculatorOperation RESULT_ADDITION
          Default addition operation
static java.lang.String RESULT_ADDITION_KEY
          Addition operation's key
static CalculatorOperation RESULT_CLEAR
          Default clear operation
static java.lang.String RESULT_CLEAR_KEY
          Clear operation's key
static CalculatorOperation RESULT_DIVISION
          Default division operation
static java.lang.String RESULT_DIVISION_KEY
          Division operation's key
static CalculatorOperation RESULT_EQUAL
          Default equal operation
static java.lang.String RESULT_EQUAL_KEY
          Equal operation's key
static CalculatorOperation RESULT_MULTIPLICATION
          Default multiplication operation
static java.lang.String RESULT_MULTIPLICATION_KEY
          Multiplication operation's key
static CalculatorOperation RESULT_PERCENT
          Default percent operation
static java.lang.String RESULT_PERCENT_KEY
          Percent operation's key
static CalculatorOperation RESULT_RECIPROCAL
          Default reciprocal operation
static java.lang.String RESULT_RECIPROCAL_KEY
          Reciprocal operation's key
static CalculatorOperation RESULT_SQRT
          Default sqrt operation
static java.lang.String RESULT_SQRT_KEY
          Sqrt operation's key
static CalculatorOperation RESULT_SUBTRACTION
          Default subtaction operation
static java.lang.String RESULT_SUBTRACTION_KEY
          Subtraction operation's key
static java.math.BigDecimal VALUE_0
          Default zero value
 
Constructor Summary
Calculator()
          Creates the class instance
 
Method Summary
 void addCalculatorListener(CalculatorListener aListener)
          Adds Calculator's listener
 void addOperation(java.lang.String anOperationKey, CalculatorOperation anOperation)
          Adds a new operation to the calculator.
 void addPropertyChangeListener(java.beans.PropertyChangeListener aListener)
          Adds state map property change listener
protected  void firePropertyChange(java.beans.PropertyChangeEvent anEvent)
          Fired by the calculator when state map property is changed
protected  void fireResultChanged()
          Fired by the calculator when the result is changed
 CalculatorOperation getDefaultClearOperation()
          Returns default clear operation.
protected  java.math.BigDecimal getResult()
          Returns last caclulator's result sitting in the state map
 java.lang.Object getStateMapProperty(java.lang.String aKey)
          Returns state map property value specified by aKey
 boolean isOperationExist(java.lang.String anOperationKey)
          Returns true if operation, specified by anOperationKey, exist
protected  void performNonStandaloneOperation(java.math.BigDecimal aValue, CalculatorOperation anOperation, boolean aValueChanged)
          Performs nonstandalone operation.
 CalculatorResult performOperation(double aValue, java.lang.String anOperationKey, boolean aValueChanged)
          Seeks and performs operation specified by anOperationKey.
protected  void performStandaloneOperation(java.math.BigDecimal aValue, CalculatorOperation anOperation, boolean aValueChanged)
          Performs standalone operation.
 void removeCalculatorListener(CalculatorListener aListener)
          Removes Calculator's listener
 void removeOperation(java.lang.String anOperationKey)
          Removes the operation, specified by anOperationKey, from the calculator
 void removePropertyChangeListener(java.beans.PropertyChangeListener aListener)
          Removes state map property change listener
 void setDefaultClearOperation(CalculatorOperation aDefaultClearOperation)
          Sets default clear operation.
 void setStateMapProperty(java.lang.String aKey, java.lang.Object aValue)
          Sets a new state map property value
 void updateOperation(java.lang.String anOperationKey, CalculatorOperation anOperation)
          Updates an existing operation in the calculator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_MEMORY

public static final java.lang.String PROPERTY_MEMORY
Property name for the memory

See Also:
Constant Field Values

PROPERTY_RESULT

public static final java.lang.String PROPERTY_RESULT
Property name for the result

See Also:
Constant Field Values

PROPERTY_HASHED_OPERATION

public static final java.lang.String PROPERTY_HASHED_OPERATION
Property name for the hashed operations

See Also:
Constant Field Values

PROPERTY_DEFAULT_CLEAR_OPERATION

public static final java.lang.String PROPERTY_DEFAULT_CLEAR_OPERATION
Property name for the default clear operation

See Also:
Constant Field Values

MEMORY_CLEAR_KEY

public static final java.lang.String MEMORY_CLEAR_KEY
Memory clear operation's key

See Also:
Constant Field Values

MEMORY_STORE_KEY

public static final java.lang.String MEMORY_STORE_KEY
Memory store operation's key

See Also:
Constant Field Values

MEMORY_RECALL_KEY

public static final java.lang.String MEMORY_RECALL_KEY
Memory recall operation's key

See Also:
Constant Field Values

MEMORY_ADDITION_KEY

public static final java.lang.String MEMORY_ADDITION_KEY
Memory addition operation's key

See Also:
Constant Field Values

MEMORY_SUBTRACTION_KEY

public static final java.lang.String MEMORY_SUBTRACTION_KEY
Memory subtraction operation's key

See Also:
Constant Field Values

RESULT_ADDITION_KEY

public static final java.lang.String RESULT_ADDITION_KEY
Addition operation's key

See Also:
Constant Field Values

RESULT_SUBTRACTION_KEY

public static final java.lang.String RESULT_SUBTRACTION_KEY
Subtraction operation's key

See Also:
Constant Field Values

RESULT_MULTIPLICATION_KEY

public static final java.lang.String RESULT_MULTIPLICATION_KEY
Multiplication operation's key

See Also:
Constant Field Values

RESULT_DIVISION_KEY

public static final java.lang.String RESULT_DIVISION_KEY
Division operation's key

See Also:
Constant Field Values

RESULT_SQRT_KEY

public static final java.lang.String RESULT_SQRT_KEY
Sqrt operation's key

See Also:
Constant Field Values

RESULT_PERCENT_KEY

public static final java.lang.String RESULT_PERCENT_KEY
Percent operation's key

See Also:
Constant Field Values

RESULT_RECIPROCAL_KEY

public static final java.lang.String RESULT_RECIPROCAL_KEY
Reciprocal operation's key

See Also:
Constant Field Values

RESULT_EQUAL_KEY

public static final java.lang.String RESULT_EQUAL_KEY
Equal operation's key

See Also:
Constant Field Values

RESULT_CLEAR_KEY

public static final java.lang.String RESULT_CLEAR_KEY
Clear operation's key

See Also:
Constant Field Values

MEMORY_CLEAR

public static final CalculatorOperation MEMORY_CLEAR
Default memory clear operation


MEMORY_STORE

public static final CalculatorOperation MEMORY_STORE
Default memory store operation


MEMORY_RECALL

public static final CalculatorOperation MEMORY_RECALL
Default memory recall operation


MEMORY_ADDITION

public static final CalculatorOperation MEMORY_ADDITION
Default memory addition operation


MEMORY_SUBTRACTION

public static final CalculatorOperation MEMORY_SUBTRACTION
Default memory subtaction operation


RESULT_ADDITION

public static final CalculatorOperation RESULT_ADDITION
Default addition operation


RESULT_SUBTRACTION

public static final CalculatorOperation RESULT_SUBTRACTION
Default subtaction operation


RESULT_MULTIPLICATION

public static final CalculatorOperation RESULT_MULTIPLICATION
Default multiplication operation


RESULT_DIVISION

public static final CalculatorOperation RESULT_DIVISION
Default division operation


RESULT_SQRT

public static final CalculatorOperation RESULT_SQRT
Default sqrt operation


RESULT_PERCENT

public static final CalculatorOperation RESULT_PERCENT
Default percent operation


RESULT_RECIPROCAL

public static final CalculatorOperation RESULT_RECIPROCAL
Default reciprocal operation


RESULT_EQUAL

public static final CalculatorOperation RESULT_EQUAL
Default equal operation


RESULT_CLEAR

public static final CalculatorOperation RESULT_CLEAR
Default clear operation


VALUE_0

public static final java.math.BigDecimal VALUE_0
Default zero value


fDefaultClearOperation

protected CalculatorOperation fDefaultClearOperation

fCalculatorOperations

protected java.util.HashMap fCalculatorOperations

fStateMap

protected java.util.HashMap fStateMap

fProcessingError

protected boolean fProcessingError

fCalculatorResult

protected CalculatorResult fCalculatorResult

fCalculatorEvent

protected CalculatorEvent fCalculatorEvent

fCalculatorListeners

protected transient java.util.Vector fCalculatorListeners

propertyChangeListeners

protected transient java.util.Vector propertyChangeListeners
Constructor Detail

Calculator

public Calculator()
Creates the class instance

Method Detail

performOperation

public CalculatorResult performOperation(double aValue,
                                         java.lang.String anOperationKey,
                                         boolean aValueChanged)
Seeks and performs operation specified by anOperationKey. Invoked by CalculatorPanel to perform an operation associated with the particular button

Parameters:
aValue - the value to perform operation on
anOperationKey - the operation key
aValueChanged - if true the value was changed in CalculatorPanel
Returns:
computing's result

performStandaloneOperation

protected void performStandaloneOperation(java.math.BigDecimal aValue,
                                          CalculatorOperation anOperation,
                                          boolean aValueChanged)
Performs standalone operation. Standalone operations perform computations and calculator's state map changes itself returning the result of its computation. The result is returned to CalculatorPanel to be shown on the display

Parameters:
aValue - the value to perform operation on
anOperation - the operation key
aValueChanged - if true the value was changed in CalculatorPanel
See Also:
CalculatorOperation.isStandalone()

performNonStandaloneOperation

protected void performNonStandaloneOperation(java.math.BigDecimal aValue,
                                             CalculatorOperation anOperation,
                                             boolean aValueChanged)
Performs nonstandalone operation. Nonstandalone operations just perform computations on two operandsands (like subtraction and multiplication), return the result of its computation and do nothing with calculator's state map. Calculator decides itself can this operation be performed immediately or put to the state map to be performed later when second operand is received. The result is returned to CalculatorPanel to be shown on the display

Parameters:
aValue - the value to perform operation on
anOperation - the operation key
aValueChanged - if true the value was changed in CalculatorPanel
See Also:
CalculatorOperation.isStandalone()

getResult

protected java.math.BigDecimal getResult()
Returns last caclulator's result sitting in the state map

Returns:
last caclulator's result sitting in the state map

getStateMapProperty

public java.lang.Object getStateMapProperty(java.lang.String aKey)
Returns state map property value specified by aKey

Parameters:
aKey - property key
Returns:
state map property value

setStateMapProperty

public void setStateMapProperty(java.lang.String aKey,
                                java.lang.Object aValue)
Sets a new state map property value

Parameters:
aKey - property key
aValue - property value

addOperation

public void addOperation(java.lang.String anOperationKey,
                         CalculatorOperation anOperation)
Adds a new operation to the calculator. If anOperationKey already exist IllegalArgumentException will be thrown

Parameters:
anOperationKey - operation key
anOperation - operation
Throws:
java.lang.IllegalArgumentException

updateOperation

public void updateOperation(java.lang.String anOperationKey,
                            CalculatorOperation anOperation)
Updates an existing operation in the calculator. If anOperationKey doesn't exist IllegalArgumentException will be thrown

Parameters:
anOperationKey - operation key
anOperation - operation
Throws:
java.lang.IllegalArgumentException

removeOperation

public void removeOperation(java.lang.String anOperationKey)
Removes the operation, specified by anOperationKey, from the calculator

Parameters:
anOperationKey - operation key

isOperationExist

public boolean isOperationExist(java.lang.String anOperationKey)
Returns true if operation, specified by anOperationKey, exist

Parameters:
anOperationKey - operation key
Returns:
true if operation exist

getDefaultClearOperation

public CalculatorOperation getDefaultClearOperation()
Returns default clear operation. Default clear operation is used by the calculator to clear its state map when an error happend during operation performance

Returns:
default clear operation

setDefaultClearOperation

public void setDefaultClearOperation(CalculatorOperation aDefaultClearOperation)
Sets default clear operation. Default clear operation is used by the calculator to clear its state map when an error happend during operation performance

Parameters:
aDefaultClearOperation - default clear operation

fireResultChanged

protected void fireResultChanged()
Fired by the calculator when the result is changed


firePropertyChange

protected void firePropertyChange(java.beans.PropertyChangeEvent anEvent)
Fired by the calculator when state map property is changed

Parameters:
anEvent - property change event

addCalculatorListener

public void addCalculatorListener(CalculatorListener aListener)
Adds Calculator's listener

Parameters:
aListener - Calculator's listener

removeCalculatorListener

public void removeCalculatorListener(CalculatorListener aListener)
Removes Calculator's listener

Parameters:
aListener - Calculator's listener

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener aListener)
Adds state map property change listener

Parameters:
aListener - state map property change listener

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener aListener)
Removes state map property change listener

Parameters:
aListener - state map property change listener