Sweet.apex

  • Docs
  • Tutorials
Docs Menu
  • Sweet.apex Core
    • Transpilation
    • Grammar
    • Command
    • Config
  • Features
    • Action
    • Apex Doc
    • Array Creation
    • Aspect
    • Cast
    • Default Value
    • Enum
    • File
    • Function
    • Identity
    • Injection
    • Lambda
    • Log
    • Mod
    • Not Null
    • Operator
    • Optional
    • Reflection
    • Rethrow
    • Switch
    • Template String
    • Template
    • Script
    • Tagged String
    • Annotation
    • Nullable
    • Var
    • Val
    • Map Access
    • Constructor
    • Transaction
    • Destructure
    • Import Static
    • Pipeline
    • Varargs
    • Patch
    • Import As
  • Plugin Development
    • Feature
    • Test Case

Identity Guide

Identity

Feature Overview

This feature generates equals and hashCode methods for the class.

Prerequisite

None

Sweet Apex Example

@identity
public class IdentityDemo {
    private String name;
    private Integer id;
    private Boolean active;
}
 
1
@identity
2
public class IdentityDemo {
3
    private String name;
4
    private Integer id;
5
    private Boolean active;
6
}

Transpiled Apex

public class IdentityDemo {
    private String name;
    private Integer id;
    private Boolean active;

    public Boolean equals(Object other) {
        if(other instanceof IdentityDemo) {
            IdentityDemo target = (IdentityDemo)other;
            return this.name == target.name && this.id == target.id && this.active == target.active;
        }
        return false;
    }

    public Integer hashCode() {
        Map<String, Object> data = new Map<String, Object>();
        data.put('name', this.name);
        data.put('id', this.id);
        data.put('active', this.active);
        return Sweet.generateHashCode(data);
    }
}
​x
 
1
public class IdentityDemo {
2
    private String name;
3
    private Integer id;
4
    private Boolean active;
5
​
6
    public Boolean equals(Object other) {
7
        if(other instanceof IdentityDemo) {
8
            IdentityDemo target = (IdentityDemo)other;
9
            return this.name == target.name && this.id == target.id && this.active == target.active;
10
        }
11
        return false;
12
    }
13
​
14
    public Integer hashCode() {
15
        Map<String, Object> data = new Map<String, Object>();
16
        data.put('name', this.name);
17
        data.put('id', this.id);
18
        data.put('active', this.active);
19
        return Sweet.generateHashCode(data);
20
    }
21
}

Usage

@identity only includes non-static fields.

Some variations are:

ExampleDescription
@identityGenerate all non-static fields
@identity(&#123; 'name', 'id' &#125;)Generate the given fields
@identity(fields=&#123; 'name', 'id' &#125;)Generate the given fields

Contribute on Github! Edit this section.

  • Feature Overview3 sec read
  • Prerequisite1 sec read
  • Sweet Apex Example5 sec read
  • Transpiled Apex26 sec read
  • Usage10 sec read
    Copy