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;
}

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 data = new Map();
        data.put('name', this.name);
        data.put('id', this.id);
        data.put('active', this.active);
        return Sweet.generateHashCode(data);
    }
}

Usage

@identity only includes non-static fields.

Some variations are:

ExampleDescription
@identityGenerate all non-static fields
@identity({ 'name', 'id' })Generate the given fields
@identity(fields={ 'name', 'id' })Generate the given fields

Contribute on Github! Edit this section.