Skip to content

Instantly share code, notes, and snippets.

@delphyne
Created April 23, 2015 17:26
Show Gist options
  • Save delphyne/f71b3b56a68a0ef73e92 to your computer and use it in GitHub Desktop.
Save delphyne/f71b3b56a68a0ef73e92 to your computer and use it in GitHub Desktop.
IntelliJ Generate Setters for Guava Optionals
#set ($paramName = $helper.getParamName($field, $project))
#set ($isOptional = $field.typeQualifiedName.startsWith('com.google.common.base.Optional<'))
#if ($isOptional)
#set ($paramType = $field.typeQualifiedName.replaceAll(".*?<(.*)>$", "$1"))
#else
#set ($paramType = $field.typeQualifiedName)
#end
public ##
#if($field.modifierStatic)
static void ##
#else
$classname ##
#end
set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($paramType $paramName) {
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = ##
#if ($isOptional)
Optional.fromNullable( ##
#end
$paramName ##
#if ($isOptional)
) ##
#end
;
#if(!$field.modifierStatic)
return this;
#end
}
@delphyne
Copy link
Author

Given the following class:

class Foo {
    String bar;
    Optional<String> baz;
}

Generates the following setters:

public Foo setBar(String bar) {
    this.bar = bar;
    return this;
}

public Foo setBaz(String baz) {
    this.baz = Optional.fromNullable(baz);
    return this;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment