org.decisiondeck.xmcda_oo.utils.matrix
Class ConstrainedMatrix<RowType,ColumnType>

java.lang.Object
  extended by org.decisiondeck.xmcda_oo.utils.matrix.ConstrainedMatrix<RowType,ColumnType>
Type Parameters:
RowType - the type of row objects used to reference positions.
ColumnType - the type of column objects used to reference positions.
All Implemented Interfaces:
IRdFloatMatrix<RowType,ColumnType>

public class ConstrainedMatrix<RowType,ColumnType>
extends Object
implements IRdFloatMatrix<RowType,ColumnType>

A matrix associating double values to positions specified with a row and a column. The matrix is constrained, i.e., it uses a ConstrainedMatrix.FuzzyValidator to ensure that every value it contains is valid. This also guarantees that any value given by this object is valid according to the ConstrainedMatrix.FuzzyValidator associated with this object.

TODO replace with a filtered Table, probably.


Nested Class Summary
static class ConstrainedMatrix.AllValidator
           
static class ConstrainedMatrix.FuzzyValidator
           
static interface ConstrainedMatrix.Validator
          An object able to discriminate between valid and invalid values.
static class ConstrainedMatrix.ValidatorType
           
 
Constructor Summary
ConstrainedMatrix(ConstrainedMatrix.ValidatorType validator)
           
ConstrainedMatrix(ConstrainedMatrix.ValidatorType actualValidator, Set<ConstrainedMatrix.ValidatorType> watchedValidators)
           
 
Method Summary
 boolean approxEquals(IRdFloatMatrix<RowType,ColumnType> m2, double imprecision)
          Two matrix are "approximately equal" to a given degree of precision iff they contain values for the same mappings and the value they contain for each mapping are not more different than the given allowed imprecision.
 boolean equals(Object obj)
          Two matrix are equal iff they are the same type and contain the same values at the same positions.
 Set<ColumnType> getColumns()
          Returns a read-only view of the columns existing in this matrix.
 Double getEntry(RowType row, ColumnType column)
          Returns the value at the position composed by the given row and column, or null if there is none.
 Set<RowType> getRows()
          Returns a view of the rows existing in this matrix.
 int getValueCount()
           Gets the number of values in this matrix.
 int hashCode()
           
 boolean isCompatible(ConstrainedMatrix.ValidatorType validator)
          Checks whether the existing values in this matrix are compatible (i.e., satisfy) the given validator.
 boolean isComplete()
           A matrix is complete iff it contains a value for every possible position (row, column) where row and column are rows and columns existing in this matrix (i.e. corresponding to at least one value).
 boolean isEmpty()
          Checks whether this matrix contains no value.
 boolean isValid(double value)
           
 boolean populateFrom(IRdFloatMatrix<RowType,ColumnType> matrix)
          Copies all values from the given matrix in this matrix, provided they are valid (i.e. they all meet the constraint bound to this object).
 boolean put(RowType row, ColumnType column, double value)
          Puts the given value at the position specified by the given row and column, provided that it is valid (according to the ConstrainedMatrix.FuzzyValidator bound to this object).
 Double remove(RowType row, ColumnType column)
          Removes the value in this matrix at the position specified by the given row and column.
 void setValidator(ConstrainedMatrix.ValidatorType validator)
          Changes the validator this object uses to validate values put into it.
 String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConstrainedMatrix

public ConstrainedMatrix(ConstrainedMatrix.ValidatorType validator)
Parameters:
validator - not null.

ConstrainedMatrix

public ConstrainedMatrix(ConstrainedMatrix.ValidatorType actualValidator,
                         Set<ConstrainedMatrix.ValidatorType> watchedValidators)
Parameters:
actualValidator - not null.
watchedValidators - the validators to watch (optimization hint to allow for faster transition to one of the given validators using setValidator(ValidatorType)). May be empty, but not null.
Method Detail

approxEquals

public boolean approxEquals(IRdFloatMatrix<RowType,ColumnType> m2,
                            double imprecision)
Description copied from interface: IRdFloatMatrix
Two matrix are "approximately equal" to a given degree of precision iff they contain values for the same mappings and the value they contain for each mapping are not more different than the given allowed imprecision.

Specified by:
approxEquals in interface IRdFloatMatrix<RowType,ColumnType>
Parameters:
m2 - the matrix to which to compare this object for approximate equality. If null, this method returns false.
imprecision - the maximal imprecision allowed.
Returns:
true iff the given matrix is approximately equal to this one.

equals

public boolean equals(Object obj)
Description copied from interface: IRdFloatMatrix
Two matrix are equal iff they are the same type and contain the same values at the same positions. Note that the values are compared exactly, thus a very small difference in one value will cause two matrixes to be unequal. See IRdFloatMatrix.approxEquals(IRdFloatMatrix, double) if this can be a problem.

Specified by:
equals in interface IRdFloatMatrix<RowType,ColumnType>
Overrides:
equals in class Object
Parameters:
obj - the object to compare. May be null.
Returns:
true iff the given object is equal to this matrix.
See Also:
Object.equals(java.lang.Object)

getColumns

public Set<ColumnType> getColumns()
Description copied from interface: IRdFloatMatrix
Returns a read-only view of the columns existing in this matrix. The returned set "read through" to the matrix, thus modifications of this matrix are reflected in the returned set.

Specified by:
getColumns in interface IRdFloatMatrix<RowType,ColumnType>
Returns:
the objects such that at least one value exists in this matrix at a position having the object as column. Not null. Empty iff this matrix is empty.

getEntry

public Double getEntry(RowType row,
                       ColumnType column)
Description copied from interface: IRdFloatMatrix
Returns the value at the position composed by the given row and column, or null if there is none.

Specified by:
getEntry in interface IRdFloatMatrix<RowType,ColumnType>
Parameters:
row - not null.
column - not null.
Returns:
the double value at that position, or null.

getRows

public Set<RowType> getRows()
Description copied from interface: IRdFloatMatrix
Returns a view of the rows existing in this matrix. The returned set "read through" to the matrix, thus modifications of this matrix are reflected in the returned set. The view may be read-only (some objects may also support objects removal from the returned set).

Specified by:
getRows in interface IRdFloatMatrix<RowType,ColumnType>
Returns:
the objects such that at least one value exists in this matrix at a position having the object as row. Not null. Empty iff this matrix is empty.

getValueCount

public int getValueCount()
Description copied from interface: IRdFloatMatrix

Gets the number of values in this matrix.

Note that the word "size" is not used here because the size of the matrix could be understood as meaning its row count times its column count, which is the same as its value count only if it is complete.

Specified by:
getValueCount in interface IRdFloatMatrix<RowType,ColumnType>
Returns:
the count of values.

hashCode

public int hashCode()
Overrides:
hashCode in class Object

isComplete

public boolean isComplete()
Description copied from interface: IRdFloatMatrix

A matrix is complete iff it contains a value for every possible position (row, column) where row and column are rows and columns existing in this matrix (i.e. corresponding to at least one value). An empty matrix is complete.

Specified by:
isComplete in interface IRdFloatMatrix<RowType,ColumnType>
Returns:
true iff this matrix is complete.

isEmpty

public boolean isEmpty()
Description copied from interface: IRdFloatMatrix
Checks whether this matrix contains no value.

Specified by:
isEmpty in interface IRdFloatMatrix<RowType,ColumnType>
Returns:
true iff the matrix contains no value.

populateFrom

public boolean populateFrom(IRdFloatMatrix<RowType,ColumnType> matrix)
Copies all values from the given matrix in this matrix, provided they are valid (i.e. they all meet the constraint bound to this object). If they are not all valid, this object is unchanged after this method returns.

Parameters:
matrix - the matrix to copy the values from. Not null.
Returns:
true iff all values have been copied from the matrix, or equivalently, true iff this matrix has changed.

put

public boolean put(RowType row,
                   ColumnType column,
                   double value)
Puts the given value at the position specified by the given row and column, provided that it is valid (according to the ConstrainedMatrix.FuzzyValidator bound to this object). If it is not, the value is not stored at the given position and any object which was at that position (if any) remains there.

Parameters:
row - not null.
column - not null.
value - the value to store.
Returns:
true iff the given value is valid, or, equivalently, true iff the given value has been stored in this matrix.

isValid

public boolean isValid(double value)

remove

public Double remove(RowType row,
                     ColumnType column)
Removes the value in this matrix at the position specified by the given row and column. If there was no value at that position, this method has no effect.

Parameters:
row - not null.
column - not null.
Returns:
the value which was previously at the given position. Returns null iff there was no value at that position (and, hence, nothing was removed).

toString

public String toString()
Overrides:
toString in class Object

setValidator

public void setValidator(ConstrainedMatrix.ValidatorType validator)
Changes the validator this object uses to validate values put into it. All values already contained in this object must be valid according to the new validator.

Parameters:
validator - not null.

isCompatible

public boolean isCompatible(ConstrainedMatrix.ValidatorType validator)
Checks whether the existing values in this matrix are compatible (i.e., satisfy) the given validator. Note that this method is efficient iff the given validator is watched (see ConstrainedMatrix(ValidatorType, Set)).

Parameters:
validator - not null.
Returns:
true iff the values in this matrix are valid considering the given validator.


Copyright © 2011. All Rights Reserved.