Wrap
Purpose¶
Since 6.10.9
Provides utility methods for manipulating Array, String, Date, File and Numbers.
Note
A Wrapper is a class that wraps a value and provides utility methods for manipulating it.
Methods¶
Binding name: p6.wrap
of (String)¶
Provides a StringWrapper based on a String.
Syntax
StringWrapper p6.wrap.of(String source)
Example
final w = p6.wrap.of transactionInfo.sender
final String formattedDate = p6.wrap.of transactionInfo.creationDate asDate 'yyyy-MM-dd' format 'yyyy-MM-dd'T'HH:mm'
final String formattedDate = p6.wrap
.of transactionInfo.keyValue.find{it.Key.text() == 'Total Amount'}.Value.text()
asNumber() format'###,###,##0.00'
final String extension = p6.wrap
.of '/opt/path/to' asFile() extension()
of (Date)¶
Provides a DateWrapper based on a Date.
Syntax
DateWrapper p6.wrap.of(Date source)
Example
final w = p6.wrap.of new Date()
of (Number)¶
Provides a NumberWrapper based on a Number.
Syntax
NumberWrapper p6.wrap.of(Number source)
Example
final w = p6.wrap.of transactionInfo.attachments.size()
of (Path)¶
Provides a FileWrapper based on a Path.
Syntax
FileWrapper p6.wrap.of(Path source)
Example
final w = p6.wrap.of Paths.get('/path/to')
of (File)¶
Provides a FileWrapper based on a File.
Syntax
FileWrapper p6.wrap.of(File source)
Example
final w = p6.wrap.of p6.uri.fileFromUrl('/path/to.ext')
of (Array)¶
Provides a ArrayWrapper based on a Object array.
Syntax
ArrayWrapper p6.wrap.of(Object[] source)
Example
final w = p6.wrap.of ['fee', 'foo']
Wrappers¶
StringWrapper¶
Provides utility methods for manipulating a String. On the following methods, wrap is a StringWrapper.
toString¶
Retrieves the String representation of the wrapped value.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of 'example' toString()
final String s = p6.wrap.of 'example'
get¶
Retrieves the wrapped String
Syntax
String wrap.get()
Example
final String value = p6.wrap.of 'example' get()
assert value == 'example'
asNumber¶
Converts the string to a number.
Syntax
NumberWrapper wrap.asNumber()
Example
final value = p6.wrap.of '123' asNumber() get()
assert value == 123
asDate¶
Converts the string to a date using the default format.
Default: Date format = yyyy-MM-dd'T'HH:mm:ss.SSSZ
Syntax
DateWrapper wrap.asDate()
Example
final String value = p6.wrap.of '2023-10-01T12:34:56.789+0100' asDate() get()
assert value == 'Sun Oct 01 11:34:56 UTC 2023'
asDate (with format)¶
Converts the string to a date using the specified format.
Syntax
DateWrapper wrap.asDate(String format)
Example
final String value = p6.wrap.of '01-10-2023' asDate 'dd-MM-yyyy' get()
assert value == 'Sun Oct 01 00:00:00 UTC 2023'
asFile¶
Converts the string to a file.
Note
The file path will be resolved using the file or p6file protocol. If the protocol is not present, the path will be resolved as a local file.
Syntax
FileWrapper wrap.asFile()
Example
final String value = p6.wrap.of 'file:///opt/path/to' asFile() get()
assert value == '/opt/path/to'
final String value = p6.wrap.of 'p6file:///${P6_DATA}/path/to' asFile() get()
assert value == '/opt/p6core.data/path/to'
final String value = p6.wrap.of '/opt/path/to' asFile() get()
assert value == '/opt/path/to'
append¶
Appends the specified value to the string.
Syntax
StringWrapper wrap.append(String value)
Example
final String value = p6.wrap.of 'example' append '123'
assert value == 'example123'
prepend¶
Prepends the specified value to the string.
Syntax
StringWrapper wrap.prepend(String value)
Example
final String value = p6.wrap.of 'example' prepend '123'
assert value == '123example'
abbreviate¶
Abbreviates the string to a maximum width of maxWidth.
Syntax
StringWrapper wrap.abbreviate(int maxWidth).with(String ellipsis)
StringWrapper wrap.abbreviate(int maxWidth).get()
# Similar as ellipsis equals '...'
Example
final String value = p6.wrap.of 'example' abbreviate 5 with '...'
assert value == 'ex...'
final String value = p6.wrap.of 'example' abbreviate 5 with '.'
assert value == 'exam.'
final String value = p6.wrap.of 'example' abbreviate 5 get()
assert value == 'ex...'
capitalize¶
Capitalizes the first character of the string.
Syntax
StringWrapper wrap.capitalize()
Example
final String value = p6.wrap.of 'example' capitalize()
assert value == 'Example'
uncapitalize¶
Uncapitalizes the first character of the string.
Syntax
StringWrapper wrap.uncapitalize()
Example
final String value = p6.wrap.of 'Example' uncapitalize()
assert value == 'example'
trim¶
Trims whitespace from both ends of the string.
Syntax
StringWrapper wrap.trim()
Example
final String value = p6.wrap.of ' example ' trim()
assert value == 'example'
upper¶
Converts the string to uppercase.
Syntax
StringWrapper wrap.upper()
Example
final String value = p6.wrap.of 'example' upper()
assert value == 'EXAMPLE'
lower¶
Converts the string to lowercase.
Syntax
StringWrapper wrap.lower()
Example
final String value = p6.wrap.of 'EXAMPLE' lower()
assert value == 'example'
escape¶
Escape the string.
Syntax
StringWrapper wrap.escape()
Example
final w = p6.wrap.of 'example' escape()
left¶
Gets the leftmost length characters of the string.
Syntax
StringWrapper wrap.left(int length)
Example
final String value = p6.wrap.of 'example' left 3
assert value == 'exa'
right¶
Gets the rightmost length characters of the string.
Syntax
StringWrapper wrap.right(int length)
Example
final String value = p6.wrap.of 'example' right 3
assert value == 'ple'
leftPad¶
Pads, the specified pad string, to the left of the string length amount of times.
Syntax
StringWrapper wrap.leftPad(int length).with(String pad)
StringWrapper wrap.leftPad(int length).get()
# Similar as pad equals ' '
Example
final String value = p6.wrap.of 'example' leftPad 10 with ' '
assert value == ' example'
final String value = p6.wrap.of 'example' leftPad 10 with '.'
assert value == '...example'
final String value = p6.wrap.of 'example' leftPad 10 get()
assert value == ' example'
rightPad¶
Pads, the specified pad string, to the right of the string length amount of times.
Syntax
StringWrapper wrap.rightPad(int length).with(String pad)
StringWrapper wrap.rightPad(int length).get()
# Similar as pad equals ' '
Example
final String value = p6.wrap.of 'example' rightPad 10 with ' '
assert value == 'example '
final String value = p6.wrap.of 'example' rightPad 10 with '.'
assert value == 'example...'
final String value = p6.wrap.of 'example' rightPad 10 get()
assert value == 'example '
replace¶
Replaces all occurrences of target with replacement in the string.
Syntax
StringWrapper wrap.replace(String target).with(String replacement)
Example
final String value = p6.wrap.of 'example' replace 'e' with 'a'
assert value == 'axampla'
replaceRe¶
Replaces all occurrences of the regular expression target with replacement in the string.
Syntax
StringWrapper wrap.replaceRe(String regex).with(String replacement)
Example
final String value = p6.wrap.of 'example' replaceRe 'e.' with 'a'
assert value == 'aample'
stripStart¶
Removes the leading characters that match the specified sequence.
Syntax
StringWrapper wrap.stripStart(String seq)
Example
final String value = p6.wrap.of 'example' stripStart 'ex'
assert value == 'ample'
isBlank¶
Checks if the string is blank.
Syntax
boolean wrap.isBlank()
Example
final value = p6.wrap.of ' ' isBlank()
assert value == true
isNotBlank¶
Checks if the string is not blank.
Syntax
boolean wrap.isNotBlank()
Example
final value = p6.wrap.of ' ' isNotBlank()
assert value == false
isEmpty¶
Checks if the string is empty.
Syntax
boolean wrap.isEmpty()
Example
final value = p6.wrap.of '' isEmpty()
assert value == true
#### isNotEmpty Checks if the string is not empty.
Syntax
boolean wrap.isNotEmpty()
Example
final value = p6.wrap.of '' isNotEmpty()
assert value == false
isNumeric¶
Checks if the string is numeric.
Syntax
boolean wrap.isNumeric()
Example
final value = p6.wrap.of '123' isNumeric()
assert value == true
final value = p6.wrap.of 'abc' isNumeric()
assert value == false
countMatches¶
Counts how many times the specified sequence seq is contained.
Syntax
NumberWrapper wrap.countMatches(String seq)
Example
final value = p6.wrap.of 'example' countMatches 'e' get()
assert value == 2
contains¶
Checks if the string contains the specified sequence seq.
Syntax
boolean wrap.contains(String seq)
Example
final value = p6.wrap.of 'example' contains 'amp'
assert value == true
final value = p6.wrap.of 'example' contains 'fee'
assert value == false
startsWith¶
Checks if the string starts with the specified sequence seq.
Syntax
boolean wrap.startsWith(String seq)
Example
final value = p6.wrap.of 'example' startsWith 'ex'
assert value == true
final value = p6.wrap.of 'example' startsWith 'le'
assert value == false
endsWith¶
Checks if the string ends with the specified sequence seq.
Syntax
boolean wrap.endsWith(String seq)
Example
final value = p6.wrap.of 'example' endsWith 'le'
assert value == true
final value = p6.wrap.of 'example' endsWith 'ex'
assert value == false
split¶
Splits the string into an array of StringWrapper using the specified separator.
Syntax
ArrayWrapper wrap.split(String seq)
Example
final value = p6.wrap.of 'fee,foo,bar' split ',' size() get()
assert value == 3
DateWrapper¶
Provides utility methods for manipulating a Date. On the following methods, wrap is a DateWrapper.
Default: Date format = yyyy-MM-dd'T'HH:mm:ss.SSSZ
Tip
Convert the date to a string, use the toString method or define the variable as a String
toString¶
Retrieves the String representation of the wrapped value using the optional timezone and date format.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of new Date() toString()
final String s = p6.wrap.of new Date()
get¶
Retrieves the wrapped Date
Syntax
Date wrap.get()
Example
final d = new Date()
final value = p6.wrap.of d get()
assert value == d
timestamp¶
Returns the timestamp of the date.
Syntax
NumberWrapper wrap.timestamp()
Example
final d = new Date(1674537600000)
final value = p6.wrap.of d timestamp() get()
assert value == 1674537600000
setLastOfMonth¶
Sets the date to the last day of the month.
Syntax
NumberWrapper wrap.setLastOfMonth()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d setLastOfMonth() getDay() get()
assert value == 31
setFirstOfMonth¶
Sets the date to the first day of the month.
Syntax
NumberWrapper wrap.setFirstOfMonth()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d setFirstOfMonth() getDay() get()
assert value == 1
add¶
Adds the specified modifier to the date.
Syntax
DateWrapper wrap.add(String modifier)
Note
The modifier string should follow the pattern [+-]\d+[smhdMy], where:
+o-indicates addition or substraction\d+is the number of units to add or substractsfor seconds,mfor minutes,hfor hours,dfor days,Mfor months, andyfor years.
Tip
You can use multiple modifiers in the same string. (i.e. +1d-1h)
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d add '+1d' getDay() get()
assert value == 25
getYear¶
Get the year of the date.
Syntax
NumberWrapper wrap.getYear()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d getYear() get()
assert value == 2025
getMonth¶
Get the month of the date. (zero indexed value)
Syntax
NumberWrapper wrap.getMonth()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d getMonth() get()
assert value == 0
getDay¶
Get the day of the month of the date.
Syntax
NumberWrapper wrap.getDay()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d getDay() get()
assert value == 24
getHours¶
Get the hours of the date.
Syntax
NumberWrapper wrap.getHours()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d getHours() get()
assert value == 12
getMinutes¶
Get the minutes of the date.
Syntax
NumberWrapper wrap.getMinutes()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d getMinutes() get()
assert value == 35
getSeconds¶
Get the seconds of the date.
Syntax
NumberWrapper wrap.getSeconds()
Example
final d = new Date(1737722104000)
final value = p6.wrap.of d getSeconds() get()
assert value == 4
format¶
Formats the date using the specified format.
Syntax
DateWrapper wrap.format(String format)
Example
final d = new Date(1737722104000)
final String value = p6.wrap.of d format 'yyyy-MM-dd'
assert value == '2025-01-24'
timezone¶
Set the timezone of the date.
Syntax
DateWrapper wrap.tmezone(String timezone)
Example
final d = new Date(1737722104000)
final String value = p6.wrap.of d format 'yyyy-MM-dd hh:mm:ss' timezone 'GMT+1'
assert value == '2025-01-24 01:35:04'
NumberWrapper¶
Provides utility methods for manipulating a Number. On the following methods, wrap is a NumberWrapper.
Tip
- to convert the number to a string, use the
toStringmethod or define the variable as a String
toString¶
Retrieves the String representation of the wrapped value using the optional number format.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of 123456.78 toString()
final String s = p6.wrap.of 123456.78
get¶
Retrieves the wrapped Number
Syntax
Number wrap.get()
Example
final value = p6.wrap.of 123456.78 get()
assert value == 123456.78
format¶
Formats the number using the specified format.
Syntax
NumberWrapper wrap.format(String format)
Example
final String value = p6.wrap.of 123456.78 format '#,##0.000'
assert value == "123,456.780"
round¶
Rounds the number to the specified number of decimal places.
Syntax
NumberWrapper wrap.round(int precision)
Example
final value = p6.wrap.of 123456.78 round 1 get()
assert value == 123456.8
calc¶
Perform a mathematics operation on the number.
Syntax
NumberWrapper wrap.calc(String operator, Number value)
Note
The operator string should be one of the following:
+indicates addition-indicates substraction*indicates multiplication/indicates division
Tip
Multiplications and divisions can generate numbers with decimal. You can use the round method to round the result.
Example
final String value = p6.wrap.of 123.45 calc('*', 67.89) round 2
assert value == "8381.02"
calc (from String)¶
Performs the same action as calc but with a string value. The string is converted to a BigDecimal before the operation.
Syntax
NumberWrapper wrap.calc(String operator, String value)
Example
final String value = p6.wrap.of 123.45 calc('*', '67.89') round 2
assert value == "8381.02"
FileWrapper¶
Provides utility methods for manipulating a File. On the following methods, wrap is a FileWrapper.
Tip
- to convert the File to a string, use the
toStringmethod or define the variable as a String
toString¶
Retrieves the String representation of the wrapped value using the optional number format.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of p6.uri.fileFromUrl('p6file:///path/to.ext') toString()
final String s = p6.wrap.of p6.uri.fileFromUrl('p6file:///path/to.ext')
get¶
Retrieves the wrapped File
Syntax
File wrap.get()
Example
final value = p6.wrap.of '/path/to.ext' asFile() get()
extension¶
Retrieves the extension of the file.
Syntax
StringWrapper wrap.extension()
Example
final String value = p6.wrap.of '/path/to.ext' asFile() extension()
assert value == 'ext'
name¶
Retrieves the name of the file.
Syntax
StringWrapper wrap.name()
Example
final String value = p6.wrap.of '/path/to.ext' asFile() name()
assert value == 'to.ext'
baseName¶
Retrieves the base name of the file.
Syntax
StringWrapper wrap.baseName()
Example
final String value = p6.wrap.of '/path/to.ext' asFile() baseName()
assert value == 'to'
ArrayWrapper¶
Provides utility methods for manipulating an Array. On the following methods, wrap is an ArrayWrapper.
Tip
- to convert the Array to a string, use the
toStringmethod or define the variable as a String
toString¶
Retrieves the String representation of the wrapped value joined on the default separator ,.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of ['fee', 'foo'] toString()
final String s = p6.wrap.of ['fee', 'foo']
get¶
Retrieves the wrapped Array
Syntax
Object[] wrap.get()
Example
final a = ['fee', 'foo']
final value = p6.wrap.of a get()
assert value == a
get(index)¶
Retrieves the element at the specified index as a Wrapper.
Warning
The index is zero-based.
Syntax
BaseWrapper wrap.get()
Example
final String value = p6.wrap.of ['fee', 'foo'] get 0
assert value == 'fee'
first¶
Retrieves the first element as a Wrapper.
Syntax
BaseWrapper wrap.first()
Example
final String value = p6.wrap.of ['fee', 'foo'] first()
assert value == 'fee'
last¶
Retrieves the last element as a Wrapper.
Syntax
BaseWrapper wrap.last()
Example
final String value = p6.wrap.of ['fee', 'foo'] last()
assert value == 'foo'
size¶
Returns the size of the array.
Syntax
NumberWrapper wrap.size()
Example
final value = p6.wrap.of ['fee', 'foo'] size()
assert value == 2
contains¶
Checks if the array contains one of the specified values.
Syntax
boolean wrap.contains(Object v1 [, Object v2, ...])
Example
final value = p6.wrap.of ['fee', 'foo'] contains 'fee'
assert value == true
final value = p6.wrap.of ['fee', 'foo'] contains ('fee', 'bar')
assert value == true
final value = p6.wrap.of ['fee', 'foo'] contains 'bar'
assert value == false
join¶
Joins the array elements using the specified separator.
Syntax
StringWrapper wrap.join(String seq)
Example
final String value = p6.wrap.of ['fee', 'foo'] join '|'
assert value == 'fee|foo'