Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http-client-java, unify branded/unbranded ExpandableEnum #5334

Conversation

XiaofeiCao
Copy link
Member

@XiaofeiCao XiaofeiCao commented Dec 11, 2024

Issue

This PR

  • Apply current branded ExpandableEnum interface implementation to unbranded
  • fromString will be unified as fromValue, and will throw if parameter is null(previously will return null)

Test

Tested with openai, no compilation issue found:

package com.openai;

import io.clientcore.core.annotation.Metadata;
import io.clientcore.core.util.ExpandableEnum;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * Defines values for OlympicRecordModel.
 */
public final class OlympicRecordModel implements ExpandableEnum<Double> {
    private static final Map<Double, OlympicRecordModel> VALUES = new ConcurrentHashMap<>();

    private static final Function<Double, OlympicRecordModel> NEW_INSTANCE = OlympicRecordModel::new;

    /**
     * Static value 9.58 for OlympicRecordModel.
     */
    @Metadata(generated = true)
    public static final OlympicRecordModel OLYMPIC_100_METERS = fromValue(9.58);

    /**
     * Static value 19.3 for OlympicRecordModel.
     */
    @Metadata(generated = true)
    public static final OlympicRecordModel OLYMPIC_200_METERS = fromValue(19.3);

    private final Double value;

    private OlympicRecordModel(Double value) {
        this.value = value;
    }

    /**
     * Creates or finds a OlympicRecordModel.
     * 
     * @param value a value to look for.
     * @return the corresponding OlympicRecordModel.
     * @throws IllegalArgumentException if value is null.
     */
    @Metadata(generated = true)
    public static OlympicRecordModel fromValue(Double value) {
        if (value == null) {
            throw new IllegalArgumentException("'value' cannot be null.");
        }
        return VALUES.computeIfAbsent(value, NEW_INSTANCE);
    }

    /**
     * Gets known OlympicRecordModel values.
     * 
     * @return Known OlympicRecordModel values.
     */
    @Metadata(generated = true)
    public static Collection<OlympicRecordModel> values() {
        return new ArrayList<>(VALUES.values());
    }

    /**
     * Gets the value of the OlympicRecordModel instance.
     * 
     * @return the value of the OlympicRecordModel instance.
     */
    @Metadata(generated = true)
    @Override
    public Double getValue() {
        return this.value;
    }

    @Metadata(generated = true)
    @Override
    public String toString() {
        return Objects.toString(this.value);
    }

    @Metadata(generated = true)
    @Override
    public boolean equals(Object obj) {
        return this == obj;
    }

    @Metadata(generated = true)
    @Override
    public int hashCode() {
        return Objects.hashCode(this.value);
    }
}

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:java Issue for the Java client emitter: @typespec/http-client-java label Dec 11, 2024
@azure-sdk
Copy link
Collaborator

No changes needing a change description found.

@XiaofeiCao XiaofeiCao marked this pull request as ready for review December 11, 2024 09:56
@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs

@XiaofeiCao XiaofeiCao added this pull request to the merge queue Dec 12, 2024
Merged via the queue into microsoft:main with commit df5a885 Dec 12, 2024
25 checks passed
@XiaofeiCao XiaofeiCao deleted the java_fix_unbranded_non_string_expandable_enum branch December 12, 2024 09:57
archerzz pushed a commit to archerzz/typespec that referenced this pull request Dec 17, 2024
)

### Issue
- Last item of Azure/autorest.java#2841
- Previous unbranded implementation has two major issues
  - No `equals()` override
  - Non-string implementation can't compile

### This PR
- Apply current branded ExpandableEnum interface implementation to
unbranded
- `fromString` will be unified as `fromValue`, and will throw if
parameter is null(previously will return null)

### Test
Tested with openai, no compilation issue found:
```java// Code generated by Microsoft (R) TypeSpec Code Generator.

package com.openai;

import io.clientcore.core.annotation.Metadata;
import io.clientcore.core.util.ExpandableEnum;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * Defines values for OlympicRecordModel.
 */
public final class OlympicRecordModel implements ExpandableEnum<Double> {
    private static final Map<Double, OlympicRecordModel> VALUES = new ConcurrentHashMap<>();

    private static final Function<Double, OlympicRecordModel> NEW_INSTANCE = OlympicRecordModel::new;

    /**
     * Static value 9.58 for OlympicRecordModel.
     */
    @metadata(generated = true)
    public static final OlympicRecordModel OLYMPIC_100_METERS = fromValue(9.58);

    /**
     * Static value 19.3 for OlympicRecordModel.
     */
    @metadata(generated = true)
    public static final OlympicRecordModel OLYMPIC_200_METERS = fromValue(19.3);

    private final Double value;

    private OlympicRecordModel(Double value) {
        this.value = value;
    }

    /**
     * Creates or finds a OlympicRecordModel.
     * 
     * @param value a value to look for.
     * @return the corresponding OlympicRecordModel.
     * @throws IllegalArgumentException if value is null.
     */
    @metadata(generated = true)
    public static OlympicRecordModel fromValue(Double value) {
        if (value == null) {
            throw new IllegalArgumentException("'value' cannot be null.");
        }
        return VALUES.computeIfAbsent(value, NEW_INSTANCE);
    }

    /**
     * Gets known OlympicRecordModel values.
     * 
     * @return Known OlympicRecordModel values.
     */
    @metadata(generated = true)
    public static Collection<OlympicRecordModel> values() {
        return new ArrayList<>(VALUES.values());
    }

    /**
     * Gets the value of the OlympicRecordModel instance.
     * 
     * @return the value of the OlympicRecordModel instance.
     */
    @metadata(generated = true)
    @OverRide
    public Double getValue() {
        return this.value;
    }

    @metadata(generated = true)
    @OverRide
    public String toString() {
        return Objects.toString(this.value);
    }

    @metadata(generated = true)
    @OverRide
    public boolean equals(Object obj) {
        return this == obj;
    }

    @metadata(generated = true)
    @OverRide
    public int hashCode() {
        return Objects.hashCode(this.value);
    }
}

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:java Issue for the Java client emitter: @typespec/http-client-java
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants