com.sas.util
Class PrefixPredicate

com.sas.util.PrefixPredicate
All Implemented Interfaces:
com.sas.util.PredicateInterface, StringPredicateInterface, java.io.Serializable

public class PrefixPredicate
implements com.sas.util.PredicateInterface, StringPredicateInterface, java.io.Serializable

PrefixPredicate is a predicate which tests if a string matches a prefix. Useful for collecting items from a collection which begin with a prefix string.

You can use this in situations where you wish to have incremental feedback/matching from alist.

For example, if you have a collection c, then

     import com.sas.collection.PredicateEnumeration;
     ...
     Enumeration e = new PredicateEnumeration(c.getItems(),
                                            new PrefixPredicate("SAS"));
 
each e.nextElement() will return an item o from the collection c such that o.toString() begins with "SAS".

Note, you can use a com.sas.util.transforms.EnumerationTransform if the items in the collection/enumeration must be transformed to String data (for prefix comparison) in a non-trivial manner. For example, if you wish to search an Enumeration of Employee records for one whose last name (e.getLastName()) starts with "B", you could use an EnumerationTransform that extracts the last name:

     Enumeration employees = db.getEmployees();
     Enumeration lastNames = new EnumerationTransform(employees,
                 new TransformInterface() {
                     public Object transform(Object obj) {
                         return ((Employee) obj).getLastName(); }
                 });
     Enumeration e = new PredicateEnumeration(lastNames,
                                              new PrefixPredicate("B"));
 
This is type of transformation is necessary if you wish to compare the prefix to some other string than the object.toString()

See Also:
PredicateEnumeration, Enumerable.getItems(), EnumerationTransform, Serialized Form

Constructor Summary
PrefixPredicate(java.lang.String prefix)
          Construct a PrefixPredicate that tests for strings matching the given prefix.
 
Method Summary
 int findItemByPrefix(java.util.Enumeration enumer, int startIndex)
          Return the index of the item starting with the prefix string.
 int findItemByPrefix(IndexedGetInterface collection, int startIndex)
          Return the index of the item starting with the prefix string.
 java.lang.String getPrefix()
          Return the value of the prefix string
 boolean predicate(java.lang.Object object)
          the predicate test: does the object start with the stored prefix string?
 boolean predicate(java.lang.String string)
          The predicate test: does the object start with the stored prefix string?
 void setPrefix(java.lang.String newPrefix)
          Set the prefix string
 

Constructor Detail

PrefixPredicate

public PrefixPredicate(java.lang.String prefix)
Construct a PrefixPredicate that tests for strings matching the given prefix.

Parameters:
prefix - a prefix string. Must be non-null.
Method Detail

predicate

public boolean predicate(java.lang.Object object)
the predicate test: does the object start with the stored prefix string?

Specified by:
predicate in interface com.sas.util.PredicateInterface
Returns:
true if the object (it's toString method, actually) starts with the stored prefix string.

predicate

public boolean predicate(java.lang.String string)
The predicate test: does the object start with the stored prefix string?

Specified by:
predicate in interface StringPredicateInterface
Parameters:
string - The String which should be tested
Returns:
true if the string starts with the stored prefix string.

setPrefix

public void setPrefix(java.lang.String newPrefix)
Set the prefix string

Parameters:
newPrefix - the new value for the prefix string

getPrefix

public java.lang.String getPrefix()
Return the value of the prefix string

Returns:
the prefix string.

findItemByPrefix

public int findItemByPrefix(java.util.Enumeration enumer,
                            int startIndex)
Return the index of the item starting with the prefix string.

Parameters:
enumer - the enumeration in which to find the item with the prefix string in.
startIndex - the index to start searching the enumeratio from.
Returns:
the index of the item starting with the prefix string or -1 if not found.

findItemByPrefix

public int findItemByPrefix(IndexedGetInterface collection,
                            int startIndex)
Return the index of the item starting with the prefix string.

Parameters:
collection - the collection to find the item with the prefix string in.
startIndex - the index to start searching the collection from.
Returns:
the index of the item starting with the prefix string or -1 if not found.



Copyright © 2009 SAS Institute Inc. All Rights Reserved.