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

Enum Guide

Enum

Feature Overview

This feature generates a full-fledged enum class.

Prerequisite

None

Sweet Apex Example

public enum EnumDemo {
    One('1'),
    Two('2'),
    Three('3');

    private String id;

    private EnumDemo(String id) {
        this.id = id;
    }

    public String getId() {
        return this.id;
    }
}

Transpiled Apex

public class EnumDemo extends Sweet.BaseEnum {
    public static final EnumDemo One = (EnumDemo)new EnumDemo('1').setName('One').setOrdinal(0);
    public static final EnumDemo Two = (EnumDemo)new EnumDemo('2').setName('Two').setOrdinal(1);
    public static final EnumDemo Three = (EnumDemo)new EnumDemo('3').setName('Three').setOrdinal(2);

    private static final Map instances = new Map{ 'One' => One, 'Two' => Two, 'Three' => Three };

    public static List values() {
        return instances.values();
    }

    public static EnumDemo valueOf(String name) {
        return instances.get(name);
    }

    private String id;
    private EnumDemo(String id) {
        this.id = id;
    }
    public String getId() {
        return this.id;
    }
}

Usage

Generated enums are all subclasses of Sweet.BaseEnum, and they share the same API.

Method NameDescription
toString()Get the name of the enum
ordinal()Get the ordinal of the enum
static BaseEnum valueOf(String)Get the enum by name
static List values()Get all enum values

Contribute on Github! Edit this section.