/*
 *  Copyright (c) 2001,2020 Alex Leigh, All Rights Reserved
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH The copyright
 *  notice above does not evidence any actual or intended publication of such
 *  source code.
 *
 *  Use, disclosure, copying, distribution, or transmission of this code without
 *  express permission is prohibited.
 */

package leigh.eo.sort;

import leigh.eo.EO;
import leigh.mecha.lang.MultiComparator;
import leigh.mecha.sort.FieldSort;
import leigh.mecha.sort.SortPolicy;

import java.util.Comparator;
import java.util.Locale;

/**
 * This implementation of {@link SortPolicy} provides additional functionality for building
 * {@link java.util.Comparator} stacks for performing the sort stated in the policy.
 *
 * @author Alex Leigh
 * @since 15.1
 */
public class EOSortPolicy extends SortPolicy<EO> {
    public EOSortPolicy(Locale locale) {
        super(locale);
    }

    /**
     * Build a comparator based on the current sort policy.
     *
     * @return The new comparator.
     */
    public Comparator<EO> buildComparator() {
        MultiComparator<EO> mc = new MultiComparator<>();
        for (FieldSort field : getFields()) {
            EOAlphaNumericComparator eoc = new EOAlphaNumericComparator(field.getKey(),
                    field.getCardinality(), getLocale());
            mc.add(eoc);
        }
        return mc;
    }
}