IDEA set get 模板

IDEA set get 模板

Scroll Down

优雅的生成get/set方法

针对set/get集合,数组做出优化. 避免了null值.

支持所有的基础类型和包装类型以及数组
支持List,Set,Map以及它们的子类.
支持Date,Timestamp


public class SystemMenuDTO {

    private List<String> list;
    private Set<String> set;
    private TreeSet<String> treeSet;
    private Map<String, String> map;
    private TreeMap<String, String> treeMap;
    private LinkedList<String> linkedList;
    private ArrayList<String> arrayList;
    private Date date;
    private Timestamp timestamp;
    private int[] array;
    private Object[] array2;


    public List<String> getList() {
        return Objects.isNull(list) ? new ArrayList<>() : new ArrayList<>(list);
    }

    public void setList(List<String> list) {
        this.list = Objects.isNull(list) ? new ArrayList<>() : new ArrayList<>(list);
    }

    public Set<String> getSet() {
        return Objects.isNull(set) ? new HashSet<>() : new HashSet<>(set);
    }

    public void setSet(Set<String> set) {
        this.set = Objects.isNull(set) ? new HashSet<>() : new HashSet<>(set);
    }

    public TreeSet<String> getTreeSet() {
        return Objects.isNull(treeSet) ? new TreeSet<>() : new TreeSet<>(treeSet);
    }

    public void setTreeSet(TreeSet<String> treeSet) {
        this.treeSet = Objects.isNull(treeSet) ? new TreeSet<>() : new TreeSet<>(treeSet);
    }

    public Map<String, String> getMap() {
        return Objects.isNull(map) ? new HashMap<>() : new HashMap<>(map);
    }

    public void setMap(Map<String, String> map) {
        this.map = Objects.isNull(map) ? new HashMap<>() : new HashMap<>(map);
    }

    public TreeMap<String, String> getTreeMap() {
        return Objects.isNull(treeMap) ? new TreeMap<>() : new TreeMap<>(treeMap);
    }

    public void setTreeMap(TreeMap<String, String> treeMap) {
        this.treeMap = Objects.isNull(treeMap) ? new TreeMap<>() : new TreeMap<>(treeMap);
    }

    public LinkedList<String> getLinkedList() {
        return Objects.isNull(linkedList) ? new LinkedList<>() : new LinkedList<>(linkedList);
    }

    public void setLinkedList(LinkedList<String> linkedList) {
        this.linkedList = Objects.isNull(linkedList) ? new LinkedList<>() : new LinkedList<>(linkedList);
    }

    public ArrayList<String> getArrayList() {
        return Objects.isNull(arrayList) ? new ArrayList<>() : new ArrayList<>(arrayList);
    }

    public void setArrayList(ArrayList<String> arrayList) {
        this.arrayList = Objects.isNull(arrayList) ? new ArrayList<>() : new ArrayList<>(arrayList);
    }

    public Date getDate() {
        return Objects.isNull(date) ? null : new Date(date.getTime());
    }

    public void setDate(Date date) {
        this.date = Objects.isNull(date) ? null : new Date(date.getTime());
    }

    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

    public int[] getArray() {
        return Objects.isNull(array) ? new int[0] : array.clone();
    }

    public void setArray(int[] array) {
        this.array = Objects.isNull(array) ? new int[0] : array.clone();
    }

    public Object[] getArray2() {
        return Objects.isNull(array2) ? new Object[0] : array2.clone();
    }

    public void setArray2(Object[] array2) {
        this.array2 = Objects.isNull(array2) ? new Object[0] : array2.clone();
    }
}



配置方法:

image.png

image.png

image.png

Clone Set

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {
#if ($field.name == $paramName)
    #if (!$field.modifierStatic)
    this.##
    #else
        $classname.##
    #end
#end
#if($field.isList())
    $field.name = java.util.Objects.isNull($paramName) ? #if ($field.getType().startsWith("java.util.List"))new java.util.ArrayList<>() #else new $field.type.replaceAll("<\S+>", "<>")() #end : #if ($field.getType().startsWith("java.util.List"))new java.util.ArrayList<>($paramName);#else new $field.type.replaceAll("<\S+>", "<>")($paramName);#end
#elseif($field.isSet())
    $field.name = java.util.Objects.isNull($paramName) ? #if ($field.getType().startsWith("java.util.Set"))new java.util.HashSet<>() #else new $field.type.replaceAll("<\S+>", "<>")() #end : #if ($field.getType().startsWith("java.util.Set"))new java.util.HashSet<>($paramName); #else new $field.type.replaceAll("<\S+>", "<>")($paramName);#end
#elseif($field.isMap())
    $field.name = java.util.Objects.isNull($paramName) ? #if ($field.getType().startsWith("java.util.Map"))new java.util.HashMap<>() #else new $field.type.replaceAll("<\S+>", "<>")() #end : #if ($field.getType().startsWith("java.util.Map"))new java.util.HashMap<>($paramName); #else new $field.type.replaceAll("<\S+>", "<>")($paramName);#end
#elseif($field.isArray())
    $field.name = java.util.Objects.isNull($paramName) ? new $field.getType().replace("[]","[0]") : ${paramName}.clone();
#elseif($field.isDate())
    $field.name = java.util.Objects.isNull($paramName) ? null : new ${field.type}(${field.name}.getTime());
#else
    $field.name = $paramName;
#end
}

Clone Get

#if($field.modifierStatic)
static ##
#end
$field.type ##
#if($field.recordComponent)
    ${field.name}##
#else
    #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
    #if ($field.boolean && $field.primitive)
    is${name}####
    #else
    get${name}####
    #end
#end
() {
#if($field.isList())
return java.util.Objects.isNull($field.name) ? #if ($field.getType().startsWith("java.util.List"))new java.util.ArrayList<>()#else new $field.type.replaceAll("<\S+>", "<>")()#end : #if ($field.getType().startsWith("java.util.List"))new java.util.ArrayList<>($field.name);#else new $field.type.replaceAll("<\S+>", "<>")($field.name);#end
#elseif($field.isSet())
return java.util.Objects.isNull($field.name) ? #if ($field.getType().startsWith("java.util.Set"))new java.util.HashSet<>()#else new $field.type.replaceAll("<\S+>", "<>")()#end : #if ($field.getType().startsWith("java.util.Set"))new java.util.HashSet<>($field.name);#else new $field.type.replaceAll("<\S+>", "<>")($field.name);#end
#elseif($field.isMap())
return java.util.Objects.isNull($field.name) ? #if ($field.getType().startsWith("java.util.Map"))new java.util.HashMap<>()#else new $field.type.replaceAll("<\S+>", "<>")()#end : #if ($field.getType().startsWith("java.util.Map"))new java.util.HashMap<>($field.name);#else new $field.type.replaceAll("<\S+>", "<>")($field.name);#end
#elseif($field.isArray())
return java.util.Objects.isNull($field.name) ? new $field.getType().replace("[]","[0]") : ${field.name}.clone();
#elseif($field.isDate())
return java.util.Objects.isNull($field.name) ? null : new ${field.type}(${field.name}.getTime());
#else
return $field.name;
#end
}