Skip to content

Deem Java Expression

This transform allows you to enter User Defined Java Expressions as a basis for the calculation of new values.

This step have the same functionality as the standard “User Defined Java Expressions” transform, but have support for some extra “tools”.

Deem Java Expression

If you have a Java expression like:

C=A+B

Then you can simply enter the right side of the expression in the dialog:

A+B

The values are exposed to the expressions as the Java objects they are:

Data typeJava Class
Stringjava.lang.String
Integerjava.lang.Long
Numberjava.lang.Double
Datejava.util.Date
BigNumberBigDecimal
Binarybyte[]
OptionDescription
New FieldThe new field in the data stream. If you want to overwrite an existing field, you need to define the field here and in the “Replace value” option
Java ExpressionThe Java Expression, see examples below
Value TypeType
LengthLength
PrecisionPrecision
Replace valueSelect this identical to the “New field” name when you want to replace the value
A+B

firstname and name and put a space in between:

firstname+" "+name

or if you really care about performance, this might be faster:

new StringBuffer(firstname).append(" ").append(name).toString()

Set the first character of each word in upper-case

Section titled “Set the first character of each word in upper-case”

john doe —> John Doe

org.pentaho.di.core.Const.initCap(name)
System.getProperty("os.name")

When a<c then return true else return false:

a<c?true:false

This can be more complicated:

a<c?(a==1?1:2):3

even with OR and AND and other operators and functions.

If you use a constant, you may need to define the right type in some expressions otherwise it could throw:

Incompatible expression types “int” and “java.lang.Long”

To solve this, use:

test == null ? new Long(0) : test

In this case, it checks if test is null and replaces with zero. If it is not null, it will return test.

Cut a string from end and test for null and minimal length

Section titled “Cut a string from end and test for null and minimal length”

Imagine you have input strings with:

  • Orlando FL
  • New York NY

and you want to separate the state and city, you could use the following expressions:

For state (get the last 2 characters):

location != null && location.length()>2 ? location.substring(location.length()-2, location.length()) : null

For city (get the beginning without the last 2 characters and trim):

location != null && location.length()>2 ? location.substring(0, location.length()-2).trim() : location

Functionality of a LIKE operator (contains string) and replacing values

Section titled “Functionality of a LIKE operator (contains string) and replacing values”

The following example returns 1 when abc is within the source string, otherwise 2. It returns also 2 when the source string is null. The return values could be of value type Integer.

samplestr !=null && samplestr.indexOf("abc")>-1 ? 1 : 2

The Deem Java Expression transform provides access to a comprehensive set of utility methods through ZTools. These methods are organized into categories for easier reference.

Modifier and TypeMethod and Description
Stringabbreviate(String str, int maxWidth) - Abbreviates a String using ellipses
Stringabbreviate(String str, int offset, int maxWidth) - Abbreviates a String using ellipses
StringabbreviateMiddle(String str, String middle, int length) - Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String
Stringcapitalize(String str) - Capitalizes a String changing the first letter to title case
StringcapitalizeAllWords(String str) - Capitalizes all the whitespace separated words in a String
Stringcenter(String str, int size) - Centers a String in a larger String of size using the space character
Stringcenter(String str, int size, char padChar) - Centers a String in a larger String of size
Stringcenter(String str, int size, String padStr) - Centers a String in a larger String of size
Stringchomp(String str) - Removes one newline from end of a String if it’s there, otherwise leave it alone
Stringchomp(String str, String separator) - Removes separator from the end of str if it’s there, otherwise leave it alone
Stringchop(String str) - Remove the last character from a String
booleancontains(String str, char searchChar) - Checks if String contains a search character, handling null
booleancontains(String str, String searchStr) - Checks if String contains a search String, handling null
booleancontainsAny(String str, char[] searchChars) - Checks if the String contains any character in the given set of characters
booleancontainsAny(String str, String searchChars) - Checks if the String contains any character in the given set of characters
booleancontainsIgnoreCase(String str, String searchStr) - Checks if String contains a search String irrespective of case, handling null
booleancontainsNone(String str, char[] invalidChars) - Checks that the String does not contain certain characters
booleancontainsNone(String str, String invalidChars) - Checks that the String does not contain certain characters
booleancontainsOnly(String str, char[] valid) - Checks if the String contains only certain characters
booleancontainsOnly(String str, String validChars) - Checks if the String contains only certain characters
intcountMatches(String str, String sub) - Counts how many times the substring appears in the larger String
StringdefaultIfBlank(String str, String defaultStr) - Returns either the passed in String, or if the String is empty or null, the value of defaultStr
StringdefaultIfEmpty(String str, String defaultStr) - Returns either the passed in String, or if the String is empty or null, the value of defaultStr
StringdefaultString(String str) - Returns either the passed in String, or if the String is null, an empty String ("")
StringdefaultString(String str, String defaultStr) - Returns either the passed in String, or if the String is null, the value of defaultStr
StringdeleteWhitespace(String str) - Deletes all whitespaces from a String as defined by Character.isWhitespace(char)
Stringdifference(String str1, String str2) - Compares two Strings, and returns the portion where they differ
booleanendsWith(String str, String suffix) - Check if a String ends with a specified suffix
booleanendsWithIgnoreCase(String str, String suffix) - Case insensitive check if a String ends with a specified suffix
booleanequals(String str1, String str2) - Compares two Strings, returning true if they are equal
booleanequalsIgnoreCase(String str1, String str2) - Compares two Strings, returning true if they are equal ignoring the case
StringgetCommonPrefix(String[] strs) - Compares all Strings in an array and returns the initial sequence of characters that is common to all of them
intindexOf(String str, String searchStr) - Finds the first index within a String, handling null
intindexOf(String str, String searchStr, int startPos) - Finds the first index within a String, handling null
intindexOfAny(String str, String[] searchStrs) - Find the first index of any of a set of potential substrings
intindexOfAnyBut(String str, char[] searchChars) - Search a String to find the first index of any character not in the given set of characters
intindexOfAnyBut(String str, String searchChars) - Search a String to find the first index of any character not in the given set of characters
intindexOfDifference(String[] strs) - Compares all Strings in an array and returns the index at which the Strings begin to differ
intindexOfDifference(String str1, String str2) - Compares two Strings, and returns the index at which the Strings begin to differ
booleanisAllLowerCase(String str) - Checks if the String contains only lowercase characters
booleanisAllUpperCase(String str) - Checks if the String contains only uppercase characters
booleanisAlpha(String str) - Checks if the String contains only unicode letters
booleanisAlphanumeric(String str) - Checks if the String contains only unicode letters or digits
booleanisAlphanumericSpace(String str) - Checks if the String contains only unicode letters, digits or space
booleanisAlphaSpace(String str) - Checks if the String contains only unicode letters and space
booleanisAsciiPrintable(String str) - Checks if the string contains only ASCII printable characters
booleanisBetween(String lowerBound, String upperBound, String value)
booleanisBlank(String str) - Checks if a String is whitespace, empty ("") or null
booleanisEmpty(String str) - Checks if a String is empty ("") or null
booleanisNotBlank(String str) - Checks if a String is not empty (""), not null and not whitespace only
booleanisNotEmpty(String str) - Checks if a String is not empty ("") and not null
booleanisNumeric(String str) - Checks if the String contains only unicode digits
booleanisNumericSpace(String str) - Checks if the String contains only unicode digits or space
booleanisWhitespace(String str) - Checks if the String contains only whitespace
intlastIndexOf(String str, String searchStr) - Finds the last index within a String, handling null
intlastIndexOf(String str, String searchStr, int startPos) - Finds the first index within a String, handling null
intlastIndexOfIgnoreCase(String str, String searchStr) - Case in-sensitive find of the last index within a String
intlastIndexOfIgnoreCase(String str, String searchStr, int startPos) - Case in-sensitive find of the last index within a String from the specified position
intlastOrdinalIndexOf(String str, String searchStr, int ordinal) - Finds the n-th last index within a String, handling null
Stringleft(String str, int len) - Gets the leftmost len characters of a String
StringleftPad(String str, int size) - Left pad a String with spaces
StringleftPad(String str, int size, char padChar) - Left pad a String with a specified character
StringleftPad(String str, int size, String padStr) - Left pad a String with a specified String
intlength(String str) - Gets a String’s length or 0 if the String is null
StringlowerCase(String str) - Converts a String to lower case as per String.toLowerCase()
StringlowerCase(String str, Locale locale) - Converts a String to lower case as per String.toLowerCase(Locale)
Stringmid(String str, int pos, int len) - Gets len characters from the middle of a String
intordinalIndexOf(String str, String searchStr, int ordinal) - Finds the n-th index within a String, handling null
Stringoverlay(String str, String overlay, int start, int end) - Overlays part of a String with another String
Stringremove(String str, char remove) - Removes all occurrences of a character from within the source string
Stringremove(String str, String remove) - Removes all occurrences of a substring from within the source string
StringremoveEnd(String str, String remove) - Removes a substring only if it is at the end of a source string, otherwise returns the source string
StringremoveEndIgnoreCase(String str, String remove) - Case insensitive removal of a substring if it is at the end of a source string, otherwise returns the source string
StringremoveStart(String str, String remove) - Removes a substring only if it is at the begining of a source string, otherwise returns the source string
StringremoveStartIgnoreCase(String str, String remove) - Case insensitive removal of a substring if it is at the begining of a source string, otherwise returns the source string
Stringrepeat(String str, int repeat) - Repeat a String repeat times to form a new String
Stringrepeat(String str, String separator, int repeat) - Repeat a String repeat times to form a new String, with a String separator injected each time
Stringreplace(String text, String searchString, String replacement) - Replaces all occurrences of a String within another String
Stringreplace(String text, String searchString, String replacement, int max) - Replaces a String with another String inside a larger String, for the first max values of the search String
StringreplaceOnce(String text, String searchString, String replacement) - Replaces a String with another String inside a larger String, once
Stringreverse(String str) - Reverses a String as per StringBuffer.reverse()
Stringright(String str, int len) - Gets the rightmost len characters of a String
StringrightPad(String str, int size) - Right pad a String with spaces
StringrightPad(String str, int size, char padChar) - Right pad a String with a specified character
StringrightPad(String str, int size, String padStr) - Right pad a String with a specified String
booleanstartsWith(String str, String prefix) - Check if a String starts with a specified prefix
booleanstartsWithAny(String string, String[] searchStrings) - Check if a String starts with any of an array of specified strings
booleanstartsWithIgnoreCase(String str, String prefix) - Case insensitive check if a String starts with a specified prefix
Stringstrip(String str) - Strips whitespace from the start and end of a String
Stringstrip(String str, String stripChars) - Strips any of a set of characters from the start and end of a String
StringstripEnd(String str, String stripChars) - Strips any of a set of characters from the end of a String
StringstripStart(String str, String stripChars) - Strips any of a set of characters from the start of a String
StringstripToEmpty(String str) - Strips whitespace from the start and end of a String returning an empty String if null input
StringstripToNull(String str) - Strips whitespace from the start and end of a String returning null if the String is empty ("") after the strip
Stringsubstring(String str, int start) - Gets a substring from the specified String avoiding exceptions
Stringsubstring(String str, int start, int end) - Gets a substring from the specified String avoiding exceptions
StringsubstringAfter(String str, String separator) - Gets the substring after the first occurrence of a separator
StringsubstringAfterLast(String str, String separator) - Gets the substring after the last occurrence of a separator
StringsubstringBefore(String str, String separator) - Gets the substring before the first occurrence of a separator
StringsubstringBeforeLast(String str, String separator) - Gets the substring before the last occurrence of a separator
StringsubstringBetween(String str, String tag) - Gets the String that is nested in between two instances of the same String
StringsubstringBetween(String str, String open, String close) - Gets the String that is nested in between two Strings
StringswapCase(String str) - Swaps the case of a String changing upper and title case to lower case, and lower case to upper case
Stringtrim(String str) - Removes control characters (char <= 32) from both ends of this String, handling null by returning null
StringtrimToEmpty(String str) - Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is null
StringtrimToNull(String str) - Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null
Stringuncapitalize(String str) - Uncapitalizes a String changing the first letter to title case
StringupperCase(String str) - Converts a String to upper case as per String.toUpperCase()
StringupperCase(String str, Locale locale) - Converts a String to upper case as per String.toUpperCase(Locale)
String[]split(String str) - Splits the provided text into an array, using whitespace as the separator
String[]split(String str, char separatorChar) - Splits the provided text into an array, separator specified
String[]split(String str, String separatorChars) - Splits the provided text into an array, separators specified
String[]split(String str, String separatorChars, int max) - Splits the provided text into an array with a maximum length, separators specified
String[]splitPreserveAllTokens(String str) - Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators
String[]splitPreserveAllTokens(String str, char separatorChar) - Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators
String[]splitPreserveAllTokens(String str, String separatorChars) - Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators
String[]splitPreserveAllTokens(String str, String separatorChars, int max) - Splits the provided text into an array with a maximum length, separators specified, preserving all tokens, including empty tokens created by adjacent separators
intgetLevenshteinDistance(String s, String t) - Find the Levenshtein distance between two Strings
DoubletoDouble(Object value)
IntegertoInteger(Object value)
StringtoString(Object value)
Modifier and TypeMethod and Description
DateaddDays(Date date, int amount) - Adds a number of days to a date returning a new object
DateaddHours(Date date, int amount) - Adds a number of hours to a date returning a new object
DateaddMilliseconds(Date date, int amount) - Adds a number of milliseconds to a date returning a new object
DateaddMinutes(Date date, int amount) - Adds a number of minutes to a date returning a new object
DateaddMonths(Date date, int amount) - Adds a number of months to a date returning a new object
DateaddSeconds(Date date, int amount) - Adds a number of seconds to a date returning a new object
DateaddWeeks(Date date, int amount) - Adds a number of weeks to a date returning a new object
DateaddYears(Date date, int amount) - Adds a number of years to a date returning a new object
Calendarceiling(Calendar date, int field) - Ceil this date, leaving the field specified as the most significant field
Dateceiling(Date date, int field) - Ceil this date, leaving the field specified as the most significant field
Dateceiling(Object date, int field) - Ceil this date, leaving the field specified as the most significant field
DategetDate() - Returns today as a Date object
IntegergetDayOfMonth()
IntegergetDayOfMonth(Date date)
IntegergetDayOfWeek()
IntegergetDayOfWeek(Date date)
IntegergetHourOfDay(Date date)
IntegergetMonth()
IntegergetMonth(Date date)
IntegergetMonthNo()
IntegergetMonthNo(Date date)
IntegergetWeek()
IntegergetWeek(Date date)
IntegergetWeek52()
IntegergetWeek52(Date date)
IntegergetWeekNo()
IntegergetWeekNo(Date date)
IntegergetWeekNo52()
IntegergetWeekNo52(Date date)
IntegergetYear()
IntegergetYear(Date date)
IntegergetYMD8() - Returns today in YMD8 integer format
Datenow() - Returns today as a Date object
IntegernowYMD8() - Returns today in YMD8 Integer format
DateparseDate(String str, String[] parsePatterns) - Parses a string representing a date by trying a variety of different parsers
DateparseDateStrictly(String str, String[] parsePatterns) - Parses a string representing a date by trying a variety of different parsers
Calendarround(Calendar date, int field) - Round this date, leaving the field specified as the most significant field
Dateround(Date date, int field) - Round this date, leaving the field specified as the most significant field
Dateround(Object date, int field) - Round this date, leaving the field specified as the most significant field
DatesetDate(int m3Date) - Set date based upon YMD8 formatted int
DatesetDate(Integer m3Date) - Set date based upon YMD8 formatted int
DatesetDate(int m3Date, int m3Time) - Set date based upon YMD8 formatted date and HHMMSS formatted time
DatesetDate(Long m3Date) - Set date based upon YMD8 formatted int
DatesetDate(Long m3Date, Long m3Time) - Set date based upon YMD8 formatted date and HHMMSS formatted time
DatesetDays(Date date, int amount) - Sets the day of month field to a date returning a new object
DatesetHours(Date date, int amount) - Sets the hours field to a date returning a new object
DatesetMilliseconds(Date date, int amount) - Sets the milliseconds field to a date returning a new object
DatesetMinutes(Date date, int amount) - Sets the minute field to a date returning a new object
DatesetMonths(Date date, int amount) - Sets the months field to a date returning a new object
DatesetSeconds(Date date, int amount) - Sets the seconds field to a date returning a new object
DatesetTime(int m3Time)
DatesetTime(Long m3Time) - Set time based upon HHMMSS formatted time
DatesetYears(Date date, int amount) - Sets the years field to a date returning a new object
StringformatDate(Date date, String format)
StringtoHH_HH(Date date) - Convert a date to the HH-HH format
StringtoHHMM(Date date) - Convert a date to the HH:MM format (Hour:Minutes)
IntegertoYMD8(Date date) - Returns today in YMD8 integer format
Calendartruncate(Calendar date, int field) - Truncate this date, leaving the field specified as the most significant field
Datetruncate(Date date, int field) - Truncate this date, leaving the field specified as the most significant field
Datetruncate(Object date, int field) - Truncate this date, leaving the field specified as the most significant field
booleanisSameDay(Calendar cal1, Calendar cal2) - Checks if two calendar objects are on the same day ignoring time
booleanisSameDay(Date date1, Date date2) - Checks if two date objects are on the same day ignoring time
booleanisSameInstant(Calendar cal1, Calendar cal2) - Checks if two calendar objects represent the same instant in time
booleanisSameInstant(Date date1, Date date2) - Checks if two date objects represent the same instant in time
booleanisSameLocalTime(Calendar cal1, Calendar cal2) - Checks if two calendar objects represent the same local time