Skip to content

Instantly share code, notes, and snippets.

@kapresoft
Created May 1, 2023 23:50
Show Gist options
  • Save kapresoft/f124b2cf0acbc29dba4d22cac32b6511 to your computer and use it in GitHub Desktop.
Save kapresoft/f124b2cf0acbc29dba4d22cac32b6511 to your computer and use it in GitHub Desktop.
Lombok Singular
package lombok;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation can be used on a field of a builder class to let lombok generate an add method for adding a single
* element to the collection behind the field.
* <p>
* This annotation is honored by {@code @Builder}.
*
* @see Singulars
*/
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.SOURCE)
public @interface Singular {
/**
* If the singular parameter name is not something that can be inferred from the variable name, specify it here.
*
* @return Name of the singular method.
*/
String value() default "";
/**
* If this and the {@code value} parameter are both absent, lombok will attempt to guess the name of the singular
* method by removing common collection prefixes (e.g. {@code set}, {@code map}) from the name of the variable that
* the annotation is put on.
*
* @return Name of the variable the annotation is put on.
*/
String[] excludes() default {};
/**
* If true, and the field is not initialized with an instance, lombok will generate a new instance for the field.
*
* @return Whether to generate a new instance if none is found.
*/
boolean force() default false;
/**
* If this value is not empty, the generated method will be annotated with this value.
*
* @return The annotation to place on the generated method.
*/
String onMethod() default "";
/**
* Set to true (default is false) to generate an 'addFooIfAbsent' method instead of an 'addFoo' method. This can
* improve readability of code that adds elements to a collection where it matters that the element isn't already in
* the collection.
*
* @return Whether to generate an 'addFooIfAbsent' method.
*/
boolean addIfAbsent() default false;
/**
* When using this annotation on a field of type {@code java.util.Map<K,V>}, you can use this attribute to specify the
* name of the parameter for the key of the map. This is useful if the name of the field does not contain enough
* information to infer the name of the key parameter.
*
* @return Name of the key parameter.
*/
String key() default "";
/**
* When using this annotation on a field of type {@code java.util.Map<K,V>}, you can use this attribute to specify the
* name of the parameter for both the key and value of the map. This is useful if the name of the field does not
* contain enough information to infer the names of the key and value parameters.
*
* @return Name of the key and value parameters.
*/
String entry() default "";
/**
* If present, lombok will add the given {@code @Deprecated} annotation to the generated method.
*
* @return The {@code @Deprecated} annotation to add to the generated method.
*/
DeprecatedDeprecation deprecation() default @DeprecatedDeprecation;
/**
* If present, lombok will make the generated method deprecated and adds the given message to the @Deprecated
* annotation.
*
* @return The message for the @Deprecated annotation.
*/
String de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment