com.fdsapi.arrays
Class ArrayFilter

java.lang.Object
  |
  +--com.fdsapi.arrays.ArrayFilter

public class ArrayFilter
extends java.lang.Object

This class does for arrays what a SQL select statement for a database table. For example in a select statement you can display or not display columns by including them in the select column list. Example: select col1,col1,col2,col5. The addDisplayCol(...) methods in the ArrayFilter class are analagous. In the SQL select statement you can also limit/filter rows by using where conditions. For example: select col1,col1,col2,col5 from table where col1='A' and (col2<=100 or col5 like 'john%'). The ArrayFilter has methods that can build up such conditionals. The addConditional(...) methods build each individual conditional (i.e. col1='A'), addAnd()/addOr() methods are called in between calls to addConditional() to string them together, and parenthesis are controlled with addLeftParen(), and addRightParen() (opening/left and closing/right parens respectively).

The original ArrayFilter class was designed by Ed Desrosiers. Steve Souza refactored this class, and added design patterns (Primarily the gang of 4's Composite and Decorator patterns.)

To see sample code click on view code below. The classes main method has plenty of examples.



View Code

Authors: Steve Souza and Ed Desrosiers


Constructor Summary
ArrayFilter()
          Creates a new instance of ArrayFilter
ArrayFilter(ArrayConverter arrayConverter)
          Creates a new instance of ArrayFilter
ArrayFilter(java.lang.String[] header)
          Creates a new instance filter and allows the columns to be referenced by the label in both display columns and where clauses.
ArrayFilter(java.lang.String[] header, ArrayConverter arrayConverter)
           
 
Method Summary
 void addAnd()
          Used to indicate that two conditionals are true only if both of them are true.
 void addConditional(Conditional conditional)
          This method can be used if you create your own Condtional and would like it to be called to see if the row should be retained.
 void addConditional(int col, java.lang.String type, java.lang.Object comparisonValue)
          Add a Conditional to be called against a given column.
 void addConditional(java.lang.String colName, java.lang.String type, java.lang.Object comparisonValue)
          Add a conditional against the named column.
 void addDisplayCol(int columnNumber)
          Select a column number to be displayed.
 void addDisplayCol(java.lang.String columnName)
          Display the named column.
 void addDisplayFunction(java.lang.String functionName)
           
 void addLeftParen()
          Group logicals together with parens.
 void addNot()
          Negate the current paren level/composite.
 void addOr()
          Used to indicate that two conditionals are true if one or both of them are true.
 void addRightParen()
          Group logicals together with parens.
 java.lang.Object[][] convert(java.lang.Object[][] data)
          This method executes the query against the data and converts the data with the ArrayConverter.
 java.lang.Object[][] filter(java.lang.Object[][] fullArray)
          Return the array filtered on both rows and columns.
 ArrayConverter getArrayConverter()
          Get the underlying ArrayConverter that backs the ArrayFilter object
 int getColNumFromName(java.lang.String columnName)
          Returns the column number when passed a column name that matches the header array passed into the Constructor
static void main(java.lang.String[] args)
          Test and Sample code for the ArrayFilter.
 void setArrayConverter(ArrayConverter arrayConverter)
          Set the underlying ArrayConverter that backs the ArrayFilter object
 java.lang.String toString()
          Create a string representation of the ArrayFilter object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArrayFilter

public ArrayFilter()
Creates a new instance of ArrayFilter


ArrayFilter

public ArrayFilter(ArrayConverter arrayConverter)
Creates a new instance of ArrayFilter


ArrayFilter

public ArrayFilter(java.lang.String[] header)

Creates a new instance filter and allows the columns to be referenced by the label in both display columns and where clauses. Arrays don't have the concept of named columns and this array gives them this capability. For example in the following example after calling this constructor col0 can be referred to as fname, and col1 can be referred to as lname. The names are not case sensitive

Sample Call:

  String[] header={"fname","lname"};
  ArrayFilter f=new ArrayFilter(header); 
  f.addDisplayCol("fname");
  f.addDisplayCol("lname");
  f.addConditional("LNAme","=","souza");
  Object[][] data=f.filter(names);
 


ArrayFilter

public ArrayFilter(java.lang.String[] header,
                   ArrayConverter arrayConverter)
Method Detail

filter

public java.lang.Object[][] filter(java.lang.Object[][] fullArray)

Return the array filtered on both rows and columns. The effect of this method and the ArrayFilter in general is to return a subset of an array much like a select statement does for a database table. For example if a table has 4 columns and 100 rows, then a select statement may only bring back 3 of the columns and half of the rows based on conditions in the where clause. Note the array passed in is unchanged and a the method returns the filtered array. Note also if a null array is passed in it will be returned and no error will occur. If there are no matches on the array it will return an empty array (i.e. it will be a valid array with no rows or columns).

For examples of using this method click the view code href above


convert

public java.lang.Object[][] convert(java.lang.Object[][] data)

This method executes the query against the data and converts the data with the ArrayConverter. This method first calls execute(...).:

Sample Code:

 ArrayFilter filter=new ArrayFilter();
 filter.addDisplayCol(5);
 filter.convert(data); 
 


getArrayConverter

public ArrayConverter getArrayConverter()
Get the underlying ArrayConverter that backs the ArrayFilter object


setArrayConverter

public void setArrayConverter(ArrayConverter arrayConverter)
Set the underlying ArrayConverter that backs the ArrayFilter object


addConditional

public void addConditional(int col,
                           java.lang.String type,
                           java.lang.Object comparisonValue)

Add a Conditional to be called against a given column. Note ==, and = can take Objects as the comparisonValue, but any of the <,<=,>,>= must take a Comparator as an argument. Another option is to use the alternative signature that adds a Conditional directly.

Arguments: 1) int col - must be >=0 and it represents the index of the array that you would like to have the conditional act against. 2) String type - is the type of Conditional. Currently valid values are: =,==,<,<= >,>=,<>,!=,like,not like,in,not in which work as you would expect. See the ConditionalFactory for further info. 3) Object comparisonValue - is the value that will be compared against the array column.

The Conditional will be added at the current paren level


addConditional

public void addConditional(java.lang.String colName,
                           java.lang.String type,
                           java.lang.Object comparisonValue)

Add a conditional against the named column. This column must match one that was passed in with the contructor that takes a String[] header. If not a RuntimeException will be thrown. The Conditional will be added at the current paren level.


addConditional

public void addConditional(Conditional conditional)

This method can be used if you create your own Condtional and would like it to be called to see if the row should be retained. The Conditional will be added at the current paren level.


addNot

public void addNot()
Negate the current paren level/composite. This means that if the current paren level returns true it will now return false and vice versa.


addAnd

public void addAnd()
Used to indicate that two conditionals are true only if both of them are true. (i.e. col1=1 and col2=50)


addOr

public void addOr()
Used to indicate that two conditionals are true if one or both of them are true. (i.e. col1=1 or col2=50)


addLeftParen

public void addLeftParen()
Group logicals together with parens. This method represents a left/open paren in the grouping: col1=2 && (col2==50 || col2=100). This method is comparable to the left paren after && in the example. If this method is called addRightParen() must also be called.


addRightParen

public void addRightParen()
Group logicals together with parens. This method represents a right/closed paren in the grouping: col1=2 && (col2==50 || col2=100). This method is comparable to the right paren before the first col2 in the example. If this method is called addLeftParen() has to be called first.


addDisplayCol

public void addDisplayCol(int columnNumber)
Select a column number to be displayed. Analagous to a column that is listed in a select statement. A RuntimeException is thrown if the column number is less than 0. The column number is indexed starting at 0 (as arrays are) and can't be greater than the the number of columns in the array minus one. The order that this method is called is the order that the columns will be displayed in. Columns can also be displayed more than once. Technically the columns are returned not displayed.


addDisplayCol

public void addDisplayCol(java.lang.String columnName)
Display the named column. This column must match a column name passed into the constructor. The columns in the resulting array from filter(array) will appear in the order that the addDisplayCol(...) methods are called in. Columns can also be displayed more than once. Technically columns are returned not displayed.


addDisplayFunction

public void addDisplayFunction(java.lang.String functionName)

getColNumFromName

public int getColNumFromName(java.lang.String columnName)
Returns the column number when passed a column name that matches the header array passed into the Constructor


toString

public java.lang.String toString()
Create a string representation of the ArrayFilter object. This is useful for debugging. An example is of a String that could be returned is: select col1,col2,lname from array where (col1=50000 && (col2=25 || lname='souza))

Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)
Test and Sample code for the ArrayFilter. View this classes source code for examples on how to use the ArrayFilter.