IndexType.java

/*
 * Copyright (C) 2012-2025 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */

package org.ctan.site.services.search.base;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import lombok.Getter;

/**
 * This enumeration lists the various locations for which indices can be
 * created. It provides information and functionality for them.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public enum IndexType {

    /**
     * The constant <code>AUTHORS</code> contains the index type for authors.
     */
    AUTHORS("authors", 'A'),
    /**
     * The constant <code>CONTENT</code> contains the content pages.
     */
    CONTENT("content", 'C'),
    /**
     * The constant <code>FILE</code> contains the index type for the file
     * names.
     */
    FILES("files", 'F'),
    /**
     * The constant <code>GUESTBOOK</code> contains the guest book entries.
     */
    GUESTBOOK("guestbook", 'G'),
    /**
     * The constant <code>LICENSES</code> contains the index type for licenses.
     */
    LICENSES("licenses", 'L'),
    /**
     * The constant <code>PKG</code> contains the index type for package
     * descriptions.
     */
    PKG("pkg", 'P'),

    /**
     * The constant <code>SITE</code> contains the index type for the static
     * pages.
     */
    SITE("site", 'S'),

    /**
     * The field <code>MIRRORS</code> contains the index type for mirrors.
     */
    MIRRORS("mirrors", 'M'),

    /**
     * The constant <code>TOPICS</code> contains the index type for topics.
     */
    TOPICS("topics", 'T'),

    /**
     * The constant <code>USERS</code> contains the site users.
     */
    USERS("users", 'U');

    /**
     * The field <code>DECODE</code> contains the mapping from single letter to
     * the index types.
     */
    private static final Map<Character, IndexType> DECODE = new HashMap<>();

    static {
        for (var it : values()) {
            DECODE.put(it.letter, it);
        }
    }

    /**
     * The method <code>decode</code> provides means to translate string as a
     * list of characters to a set of corresponding index types.
     *
     * @param s the string of single letter indicators
     * @return the set of index types
     */
    public static Set<IndexType> decode(String s) {

        var result = new HashSet<IndexType>();
        for (var c : s.toCharArray()) {
            var it = DECODE.get(c);
            if (it == null) {
                throw new IllegalArgumentException(
                    "unknown IndexType " + c);
            }
            result.add(it);
        }
        return result;
    }

    /**
     * The field <code>key</code> contains the key.
     */
    @Getter
    private String key;

    /**
     * The field <code>letter</code> contains the single letter representation.
     */
    @Getter
    private char letter;

    /**
     * This is the constructor for <code>IndexType</code>.
     *
     * @param key the key
     */
    IndexType(String key, char letter) {

        this.key = key;
        this.letter = letter;
    }

}