From 34835fa8b7ad24cf8bb80cda71594c9a1eb785de Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
build()
, the produced collection will be immutable.build()
works fine and does not corrupt any of the collections already generated.build()
does not modify any already generated objects, and, if build()
is later called again,
+ build()
does not modify any already generated objects, and, if build()
is later called again, another collection with all the elements added since the creation of the builder is generated.
- @Singular
can only be applied to collection types for which lombok has a recipe to produce the singular methods. Currently, the supported types are:
+ @Singular
can only be applied to collection types known to lombok. Currently, the supported types are:
java.util
:Iterable
, Collection
, and List
(backed by a compacted unmodifiable ArrayList
in the general case).com.google.common.collect
:ImmutableCollection
and ImmutableList
(backed by the builder feature of ImmutableList
).ImmutableSet
and ImmutableSortedSet
(backed by the builder feature of those types).ImmutableMap
, ImmutableBiMap
, and ImmutableSortedMap (backed by the builder feature of those types).ImmutableMap
, ImmutableBiMap
, and ImmutableSortedMap
(backed by the builder feature of those types).
- If your identifiers are written in common english, lombok assumes that any collection with @Singular
on it is an english plural and will attempt to automatically
- singularize it. If this is possible, the add-one method will use this name. For example, if your collection is called statuses
, then the add-one method will automatically
- be called status
. If lombok cannot singularize your identifier, or it is ambiguous, lombok will generate an error and force you to explicitly specify the singular name.
- To do this, just pass the singular name as string, like so: @Singular("axis") List<Line> axes;
.
-
- When using the java.util
interfaces, lombok always uses ArrayList
to store items added to the builder, because this is more efficient than adding them to a map or
- set immediately, as lombok needs to compact and potentially duplicate the result.
+ If your identifiers are written in common english, lombok assumes that the name of any collection with @Singular
on it is an english plural and will attempt to automatically
+ singularize that name. If this is possible, the add-one method will use this name. For example, if your collection is called statuses
, then the add-one method will automatically
+ be called status
. You can also specify the singular form of your identifier explictly by passing the singular form as argument to the annotation like so: @Singular("axis") List<Line> axes;
.
+ If lombok cannot singularize your identifier, or it is ambiguous, lombok will generate an error and force you to explicitly specify the singular name.
The snippet below does not show what lombok generates for a @Singular
field/parameter because it is rather complicated.
You can view a snippet here.
@@ -132,9 +128,9 @@
lombok.builder.flagUsage
= [warning
| error
] (default: not set)@Builder
as a warning or error if configured.lombok.singular.useGuava
= [true
| false
] (default: false)true
, lombok will use guava's ImmutableX
builders and types to implement java.util
collection interfaces, instead of creating
- implementations based on Collections.unmodifiableX
. You must ensure that guava is actually available on the classpath and buildpath if you use this setting.
- Guava is used automatically if your field/parameter has one of the guava ImmutableX
types.
+ true
, lombok will use guava's ImmutableXxx
builders and types to implement java.util
collection interfaces, instead of creating
+ implementations based on Collections.unmodifiableXxx
. You must ensure that guava is actually available on the classpath and buildpath if you use this setting.
+ Guava is used automatically if your field/parameter has one of the guava ImmutableXxx
types.
lombok.singular.auto
= [true
| false
] (default: true)true
(which is the default), lombok automatically tries to singularize your identifier name by assuming that it is a common english plural.
If false
, you must always explicitly specify the singular name, and lombok will generate an error if you don't (useful if you write your code in a language other than english).
@@ -146,9 +142,11 @@
@Singular support for java.util.NavigableMap/Set
only works if you are compiling with JDK1.8 or higher.
You cannot manually provide some or all parts of a @Singular
node; the code lombok generates is too complex for this. If you want to
- manually control (part of) the builder nodes associated with some field or parameter, don't use @Singular and add everything you need manually.
+ manually control (part of) the builder code associated with some field or parameter, don't use @Singular
and add everything you need manually.
The sorted collections (java.util: SortedSet
, NavigableSet
, SortedMap
, NavigableMap
and guava: ImmutableSortedSet
, ImmutableSortedMap
) require that the type argument of the collection has natural order (implements java.util.Comparable
). There is no way to pass an explicit Comparator
to use in the builder.
+
+ An ArrayList
is used to store added elements as call methods of a @Singular
marked field, if the target collection is from the java.util
package, even if the collection is a set or map. Because lombok ensures that generated collections are compacted, a new backing instance of a set or map must be constructed anyway, and storing the data as an ArrayList
during the build process is more efficient that storing it as a map or set. This behaviour is not externally visible, an an implementation detail of the current implementation of the java.util
recipes for @Singular @Builder
.