package com.atlassian.stash.internal.language;

import javax.annotation.Nonnull;
import java.util.Locale;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Represents an installed language in the system.
 *
 * @since 2.12
 */
public class Language implements Comparable<Language> {

    private final Locale locale;

    public Language(@Nonnull Locale locale) {
        this.locale = checkNotNull(locale, "locale");
    }

    @Nonnull
    public Locale getLocale() {
        return locale;
    }

    public String getLocalizedName() {
        return locale.getDisplayName(locale);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Language language = (Language) o;
        return locale.equals(language.locale);

    }

    @Override
    public int hashCode() {
        return locale.hashCode();
    }

    @Override
    public int compareTo(@Nonnull Language other) {
        // I'm not saying that one language is better than another, I just want them ordered consistency.
        // In this case we have chosen alphabetical.
        return locale.toString().compareTo(other.locale.toString());
    }
}
