3.2.1: 2012-12-13

net.sf.basedb.util.parser
Class DefaultItemFunction<T extends Nameable>

java.lang.Object
  extended by net.sf.basedb.util.parser.DefaultItemFunction<T>
All Implemented Interfaces:
JepFunction, org.nfunk.jep.function.PostfixMathCommandI

public class DefaultItemFunction<T extends Nameable>
extends Object
implements JepFunction

JEP function that can be used to return the name of a default item in a project. The constructor need to specify the project and the type of item to look for. Use 'default()' or 'default(col("header"))' in the JEP expression to use this function. The first variant will always return the default item if one exists for the project. The second variant will return the default item only if the 'col("header")' part is null or an empty string.

Before this function is used the setCurrentSubtype(ItemSubtype) can be called to limit the search to items with a specific subtype. If there is more than one matching item in a project, it is undefined which one is returned, but multiple uses of this function will return the same item.

Since:
3.1
Author:
Nicklas
Last modified
$Date: 2011-12-06 09:17:37 +0100 (Tue, 06 Dec 2011) $

Field Summary
private  DbControl dc
           
private  Map<Object,T> itemCache
           
private  Item itemType
           
private  int numParameters
           
private  Project project
           
private  ItemSubtype subtype
           
 
Constructor Summary
DefaultItemFunction(DbControl dc, Project project, Item itemType)
          Create a new function instance.
 
Method Summary
 boolean checkNumberOfParameters(int n)
           
 T findDefaultItem()
          Find a default item.
 String getFunctionName()
          Get the name of this function.
 int getNumberOfParameters()
           
 void run(Stack stack)
           
 void setCurNumberOfParameters(int n)
           
 void setCurrentSubtype(ItemSubtype subtype)
          Set the current subtype to use when looking for default items.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dc

private final DbControl dc

project

private final Project project

itemType

private final Item itemType

subtype

private ItemSubtype subtype

numParameters

private int numParameters

itemCache

private Map<Object,T extends Nameable> itemCache
Constructor Detail

DefaultItemFunction

public DefaultItemFunction(DbControl dc,
                           Project project,
                           Item itemType)
Create a new function instance.

Parameters:
dc - The DbControl to use when loading items from the database
project - The project (null is allowed)
itemType - The type of item to look for
Method Detail

getFunctionName

public String getFunctionName()
Description copied from interface: JepFunction
Get the name of this function. It is used when registering the function with a JEP parser.

Specified by:
getFunctionName in interface JepFunction
Returns:
The string "default"
See Also:
JEP.addFunction(String, PostfixMathCommandI)

getNumberOfParameters

public int getNumberOfParameters()
Specified by:
getNumberOfParameters in interface org.nfunk.jep.function.PostfixMathCommandI

setCurNumberOfParameters

public void setCurNumberOfParameters(int n)
Specified by:
setCurNumberOfParameters in interface org.nfunk.jep.function.PostfixMathCommandI

checkNumberOfParameters

public boolean checkNumberOfParameters(int n)
Specified by:
checkNumberOfParameters in interface org.nfunk.jep.function.PostfixMathCommandI

run

public void run(Stack stack)
         throws ParseException
Specified by:
run in interface org.nfunk.jep.function.PostfixMathCommandI
Throws:
ParseException

findDefaultItem

public T findDefaultItem()
Find a default item. If a subtype has been set with setCurrentSubtype(ItemSubtype) the Project.findDefaultItems(DbControl, ItemSubtype, boolean) method is used in non-strict mode, otherwise the Project.findDefaultItems(DbControl, Item, boolean) is used in strict mode.

Returns:
A matching default item or null if none could be found

setCurrentSubtype

public void setCurrentSubtype(ItemSubtype subtype)
Set the current subtype to use when looking for default items.


3.2.1: 2012-12-13