package lc.mecha.sort;

import java.util.*;

/**
 * This class implements a locale-specific sorting policy for tabular (or similarly structured) data. Implementing
 * versions of this class will provide factory support for instancing matching comparators.
 *
 * @author Alex Leigh
 * @since 15.1
 */
public abstract class SortPolicy<T> {
    private final Locale locale;
    private final List<FieldSort> fields = new ArrayList<>();

    public SortPolicy(Locale locale) {
        this.locale = locale;
    }

    public void addField(FieldSort field) {
        fields.add(field);
    }

    /**
     * Return the fields associated with this sort policy. If multiple fields are present in the policy they are to
     * be evaluated in the order returned here.
     *
     * @return The list of fields sort policies.
     */
    public List<FieldSort> getFields() {
        return fields;
    }

    /**
     * Return the {@link Locale} associated with this sort policy. This Locale should be used when evaluating
     * strings.
     *
     * @return The {@link Locale}.
     */
    public Locale getLocale() {
        return locale;
    }

    /**
     * Reset the sort policy so that it does not contain any fields.
     */
    public void clear() {
        fields.clear();
    }

    public abstract Comparator<T> buildComparator();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SortPolicy that = (SortPolicy) o;
        return Objects.equals(locale, that.locale) &&
                Objects.equals(fields, that.fields);
    }

    @Override
    public int hashCode() {
        return Objects.hash(locale, fields);
    }

    @Override
    public String toString() {
        return "SortPolicy{" +
                "locale=" + locale +
                ", fields=" + fields +
                '}';
    }
}