Package groovy.transform
Annotation Type RecordOptions
Class annotation used to assist in the creation of record-like classes.
- Since:
- 4.0.0
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanIftrue, this adds a methodcomponents()to the record which returns its components as a typed tupleTuple0,Tuple1...booleanIftrue, this adds a methodcopyWithwhich takes a Map of new property values and returns a new instance of the record class with these values set.booleanIftrue, this adds a methodgetAt(int)which given an integer n, returns the n'th component in the record.Mode to use when creating record type classes.booleanIftrue, this adds a methodsize()to the record which returns the number of components.booleanIftrue, this adds a methodtoList()to the record which returns the record's components as a list.booleanIftrue, this adds a methodtoMap()to the record.
-
Element Details
-
mode
RecordTypeMode modeMode to use when creating record type classes.- Default:
- AUTO
-
getAt
boolean getAtIftrue, this adds a methodgetAt(int)which given an integer n, returns the n'th component in the record. Example:import static groovy.test.GroovyAssert.shouldFail record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p[0] == 100 assert p[1] == 200 assert p[2] == 'green' shouldFail(IllegalArgumentException) { p[-1] } // getAt also enables destructuring def (x, y, c) = p assert x == 100 assert y == 200 assert c == 'green'If a methodgetAt(int)already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
toList
boolean toListIftrue, this adds a methodtoList()to the record which returns the record's components as a list. Example:record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p.toList() == [100, 200, 'green']If a methodtoList()already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
toMap
boolean toMapIftrue, this adds a methodtoMap()to the record. Example:record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p.toMap() == [x:100, y:200, color:'green']If a methodtoMap()already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
size
boolean sizeIftrue, this adds a methodsize()to the record which returns the number of components. Example:record Point(int x, int y, String color) {} def p = new Point(100, 200, 'green') assert p.size() == 3If a methodsize()already exists in the class, then this setting is ignored, and no additional method is generated.- Default:
- true
-
copyWith
boolean copyWithIftrue, this adds a methodcopyWithwhich takes a Map of new property values and returns a new instance of the record class with these values set. Example:
Unknown keys in the map are ignored, and if the values would not change the object, then the original object is returned. If a method called@groovy.transform.RecordType(copyWith = true) class Person { String first, last } def tim = new Person('tim', 'yates') def alice = tim.copyWith(first:'alice') assert tim.toString() == 'Person[first=tim, last=yates]' assert alice.toString() == 'Person[first=alice, last=yates]'copyWiththat takes a single parameter already exists in the class, then this setting is ignored, and no method is generated.- Default:
- false
-
components
boolean componentsIftrue, this adds a methodcomponents()to the record which returns its components as a typed tupleTuple0,Tuple1... Example:import groovy.transform.*
The signature of the components method for this example is:@RecordOptions(components=true)record Point(int x, int y, String color) {} def (x, y, c) = new Point(100, 200, 'green').components() assert x == 100 assert y.intdiv(2) == 100 assert c.toUpperCase() == 'GREEN'Tuple3<Integer, Integer, String> components()
This is suitable for destructuring inTypeCheckedscenarios. If a methodcomponents()already exists in the class, then this setting is ignored, and no additional method is generated. It is a compile-time error if there are more components in the record than the largest TupleN class in the Groovy codebase.- Default:
- false
-