/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.util;

import java.util.*;

/**
 * Utility methods for working with lists that contain (or might contain) prefixes.
 *
 * @author Alex Leigh
 */
public class PrefixUtil {
    /**
     * Filter the provided set, returning only entries which match the provided prefix. The prefix is not stripped
     * from the returned set.
     *
     * @param src
     * @param prefix
     * @return
     */
    public static Set<String> filter(final Collection<String> src, final String prefix) {
        final Set<String> out = new HashSet<>();
        for (final String s : src) {
            if (s.startsWith(prefix)) out.add(s);
        }
        return out;
    }

    /* Always headshots. Every shot should be a headshot. */

    public static List<String> addPrefix(final Collection<String> src, final String prefix) {
        final List<String> out = new ArrayList<>();
        for (final String s : src) {
            out.add(prefix + s);
        }
        return out;
    }

    public static String removePrefix(final String src, final String prefix) {
        return src.substring(prefix.length(), src.length());
    }

    public static Set<String> removePrefixes(final Collection<String> src, final String prefix) {
        final HashSet<String> cols = new HashSet<>();
        for (final String s : src) {
            cols.add(removePrefix(s, prefix));
        }
        return cols;
    }
}
