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

org.solovyev.android.list.LabeledEnum Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
package org.solovyev.android.list;

import android.content.Context;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.Labeled;

import java.util.ArrayList;
import java.util.List;

/**
 * User: serso
 * Date: 7/31/12
 * Time: 11:36 PM
 */
public class LabeledEnum {

    @NotNull
    private E enumConstant;

    @NotNull
    private String label;

    private LabeledEnum() {
    }

    @NotNull
    public static  LabeledEnum newInstance(@NotNull E enumConstant, @NotNull String label) {
        final LabeledEnum result = new LabeledEnum();

        result.enumConstant = enumConstant;
        result.label = label;

        return result;
    }

    @NotNull
    public static  LabeledEnum newInstance(@NotNull E enumConstant,
                                                                        @NotNull Context context) {
        return LabeledEnum.newInstance(enumConstant, context.getString(enumConstant.getCaptionResId()));
    }

    @NotNull
    public static  List> toLabeledEnums(@NotNull Class enumClass,
                                                                                 @NotNull Context context) {
        final E[] enumConstants = enumClass.getEnumConstants();

        final List> result = new ArrayList>(enumConstants.length);
        for (E enumConstant : enumConstants) {
            result.add(LabeledEnum.newInstance(enumConstant, context));
        }

        return result;
    }

    @NotNull
    public E getEnumConstant() {
        return enumConstant;
    }

    @NotNull
    public String getLabel() {
        return label;
    }

    @Override
    public String toString() {
        return label;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LabeledEnum)) return false;

        LabeledEnum that = (LabeledEnum) o;

        if (!enumConstant.equals(that.enumConstant)) return false;

        return true;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy