How to Simplify Calls with Enum Parameter Inversion in Java and TypeScript

This article explains the concept of enum parameter inversion, shows how to encapsulate enums and add formatting methods in Java and TypeScript, and demonstrates how the technique reduces the number of arguments needed when calling formatting functions.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
How to Simplify Calls with Enum Parameter Inversion in Java and TypeScript

1. Introduction

Enum parameter inversion means adding methods to an enum to reduce the number of arguments passed at the call site.

Example

long timestamp = System.currentTimeMillis();
// before
DateTimeUtil.format(timestamp, DateTimeFormatter.FULL_DATE);
// after
DateTimeFormatter.FULL_DATE.format(timestamp);

The example shows how the call becomes shorter.

还有其他好处吗? 好像没有了...

We will not discuss pros or cons, only the implementation.

2. Java implementation

In Java, enums are special classes; each enum constant can be treated as a static instance.

2.1 Encapsulate enum class

@Getter
@AllArgsConstructor
public enum DateTimeFormatter {
    /** 年月日 */
    FULL_DATE("yyyy-MM-dd"),
    /** 时分秒 */
    FULL_TIME("HH:mm:ss"),
    /** 年月日时分秒 */
    FULL_DATETIME("yyyy-MM-dd HH:mm:ss");
    private final String value;
}

After declaring the enum we can add methods.

2.2 Add methods

@Getter
@AllArgsConstructor
public enum DateTimeFormatter {
    // omitted enum constants
    ;
    private final String value;
    /** Use this pattern to format a millisecond timestamp */
    @NotNull
    public final String format(long milliSecond) {
        return DateTimeUtil.format(milliSecond, this);
    }
    /** Format the current time */
    @NotNull
    public final String formatCurrent() {
        return format(System.currentTimeMillis());
    }
}

2.3 Call example

After encapsulation we can use the two methods to achieve enum parameter inversion:

QQ_1744356483132.png
QQ_1744356483132.png

3. TypeScript implementation

TypeScript enums do not support the same encapsulation, so we use a class that extends a base enum helper.

3.1 Encapsulate enum class

export class DateTimeFormatter extends AirEnum {
    static readonly FULL_DATETIME = new DateTimeFormatter('yyyy-MM-dd HH:mm:ss', '年-月-日 时:分:秒');
    static readonly FULL_DATE = new DateTimeFormatter('yyyy-MM-dd', '年-月-日');
    static readonly FULL_TIME = new DateTimeFormatter('HH:mm:ss', '时-分-秒');
}

3.2 Add methods

export class DateTimeFormatter extends AirEnum<string> {
    // static enum items omitted
    /** Format millisecond timestamp */
    format(timestamp: number) {
        return AirDateTime.formatFromMilliSecond(timestamp);
    }
    /** Format current time */
    formatCurrent() {
        return this.format(Date.now().valueOf());
    }
}

3.3 Call example

QQ_1744357214249.png
QQ_1744357214249.png

4. Summary

The article demonstrates how to encapsulate enums in Java and TypeScript to achieve enum parameter inversion, reducing the number of arguments required at call sites.

JavaTypeScriptcode refactoringEnumparameter inversion
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.