001/*
002 * Copyright 2017-2022 Product Mog LLC, 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.lokalized;
018
019import org.jspecify.annotations.NonNull;
020
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.Map;
024import java.util.stream.Collectors;
025
026import static java.lang.String.format;
027import static java.util.Objects.requireNonNull;
028
029/**
030 * High-level categories of language forms used to drive placeholder agreement.
031 * <p>
032 * These values correspond to the JSON {@code form} names used by selector-based placeholder translations, for example
033 * {@code GENDER} or {@code CASE}.
034 *
035 * @author <a href="https://revetkn.com">Mark Allen</a>
036 */
037public enum LanguageFormType {
038  /**
039   * Cardinality/plural form.
040   */
041  CARDINALITY(Cardinality.class),
042  /**
043   * Ordinality/ordinal-number form.
044   */
045  ORDINALITY(Ordinality.class),
046  /**
047   * Grammatical gender form.
048   */
049  GENDER(Gender.class),
050  /**
051   * Grammatical case form.
052   */
053  CASE(GrammaticalCase.class),
054  /**
055   * Definiteness form.
056   */
057  DEFINITENESS(Definiteness.class),
058  /**
059   * Classifier or counter-word form.
060   */
061  CLASSIFIER(Classifier.class),
062  /**
063   * Speech-level or formality form.
064   */
065  FORMALITY(Formality.class),
066  /**
067   * Clusivity form.
068   */
069  CLUSIVITY(Clusivity.class),
070  /**
071   * Animacy form.
072   */
073  ANIMACY(Animacy.class),
074  /**
075   * Phonetic form.
076   */
077  PHONETIC(Phonetic.class);
078
079  @NonNull
080  private static final Map<@NonNull String, @NonNull LanguageFormType> LANGUAGE_FORM_TYPES_BY_NAME;
081  @NonNull
082  private final Class<? extends LanguageForm> languageFormClass;
083
084  static {
085    LANGUAGE_FORM_TYPES_BY_NAME = Collections.unmodifiableMap(Arrays.stream(LanguageFormType.values())
086        .collect(Collectors.toMap(languageFormType -> languageFormType.name(), languageFormType -> languageFormType)));
087  }
088
089  LanguageFormType(@NonNull Class<? extends LanguageForm> languageFormClass) {
090    requireNonNull(languageFormClass);
091    this.languageFormClass = languageFormClass;
092  }
093
094  /**
095   * Gets the concrete {@link LanguageForm} class represented by this type.
096   *
097   * @return the concrete {@link LanguageForm} class represented by this type, not null
098   */
099  @NonNull
100  public Class<? extends LanguageForm> getLanguageFormClass() {
101    return languageFormClass;
102  }
103
104  /**
105   * Gets the mapping of selector-form names to selector-form values.
106   *
107   * @return the mapping of selector-form names to selector-form values, not null
108   */
109  @NonNull
110  static Map<@NonNull String, @NonNull LanguageFormType> getLanguageFormTypesByName() {
111    return LANGUAGE_FORM_TYPES_BY_NAME;
112  }
113
114  /**
115   * Determines the selector-form type for a concrete language-form value.
116   *
117   * @param languageForm the language-form value for which to determine a type, not null
118   * @return the selector-form type for the supplied language-form value, not null
119   * @throws IllegalArgumentException if the language form is not recognized
120   */
121  @NonNull
122  public static LanguageFormType forLanguageForm(@NonNull LanguageForm languageForm) {
123    requireNonNull(languageForm);
124
125    for (LanguageFormType languageFormType : LanguageFormType.values()) {
126      if (languageFormType.getLanguageFormClass().isInstance(languageForm))
127        return languageFormType;
128    }
129
130    throw new IllegalArgumentException(format("Encountered unrecognized language form %s", languageForm));
131  }
132}