All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.auties.whatsapp.model.mobile.CountryLocale Maven / Gradle / Ivy

package it.auties.whatsapp.model.mobile;

import it.auties.protobuf.annotation.ProtobufProperty;
import it.auties.protobuf.model.ProtobufMessage;
import it.auties.protobuf.model.ProtobufType;

import java.util.Objects;
import java.util.Optional;

public record CountryLocale(
        @ProtobufProperty(index = 1, type = ProtobufType.STRING)
        String languageValue,
        @ProtobufProperty(index = 2, type = ProtobufType.STRING)
        String languageCode,
        @ProtobufProperty(index = 3, type = ProtobufType.STRING)
        String separator
) implements ProtobufMessage {
    public static CountryLocale of(String encoded) {
        return of(encoded, "-")
                .or(() -> of(encoded, "_"))
                .orElseThrow(() -> new IllegalArgumentException("Cannot decode locale: " + encoded));
    }

    public static Optional of(String encoded, String separator) {
        Objects.requireNonNull(encoded, "Expected non-null encoded locale");
        var split = encoded.split(String.valueOf(separator));
        return split.length == 2 ? Optional.of(new CountryLocale(split[0], split[1], separator)) : Optional.empty();
    }

    @Override
    public String toString() {
        return languageValue + separator + languageCode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy