/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.codec.language.bm;

import java.io.InputStream;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Set;

/**
 * <p>
 * Language codes.
 * </p>
 * <p>
 * Language codes are typically loaded from resource files. These are UTF-8 encoded text files. They are systematically named following the
 * pattern:
 * </p>
 * <blockquote>org/apache/commons/codec/language/bm/${{@link NameType#getName()} languages.txt</blockquote>
 * <p>
 * The format of these resources is the following:
 * </p>
 * <ul>
 * <li><b>Language:</b> a single string containing no whitespace</li>
 * <li><b>End-of-line comments:</b> Any occurance of '//' will cause all text following on that line to be discarded as a comment.</li>
 * <li><b>Multi-line comments:</b> Any line starting with '/*' will start multi-line commenting mode. This will skip all content until a
 * line ending in '*' and '/' is found.</li>
 * <li><b>Blank lines:</b> All blank lines will be skipped.</li>
 * </ul>
 * <p>
 * Ported from language.php
 * </p>
 * 
 * @author Apache Software Foundation
 * @since 2.0
 */
public class Languages {

    public static final String ANY = "any";

    private static final Map<NameType, Languages> LANGUAGES = new EnumMap<NameType, Languages>(NameType.class);

    static {
        for (NameType s : NameType.values()) {
            LANGUAGES.put(s, instance(langResourceName(s)));
        }
    }

    public static Languages instance(NameType nameType) {
        return LANGUAGES.get(nameType);
    }

    public static Languages instance(String languagesResourceName) {
        // read languages list
        Set<String> ls = new HashSet<String>();
        InputStream langIS = Languages.class.getClassLoader().getResourceAsStream(languagesResourceName);

        if (langIS == null) {
            throw new IllegalArgumentException("Unable to resolve required resource: " + languagesResourceName);
        }

        Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING);
        boolean inExtendedComment = false;
        while (lsScanner.hasNextLine()) {
            String line = lsScanner.nextLine().trim();
            if (inExtendedComment) {
                if (line.endsWith(ResourceConstants.EXT_CMT_END)) {
                    inExtendedComment = false;
                } else {
                    // skip
                }
            } else {
                if (line.startsWith(ResourceConstants.EXT_CMT_START)) {
                    inExtendedComment = true;
                } else if (line.length() > 0) {
                    ls.add(line);
                } else {
                    // skip blank lines
                }
            }
        }

        return new Languages(Collections.unmodifiableSet(ls));
    }

    private static String langResourceName(NameType nameType) {
        return String.format("org/apache/commons/codec/language/bm/%s_languages.txt", nameType.getName());
    }

    private final Set<String> languages;

    private Languages(Set<String> languages) {
        this.languages = languages;
    }

    public Set<String> getLanguages() {
        return this.languages;
    }

    /**
     * A set of languages.
     */
    public static abstract class LanguageSet {
        public abstract LanguageSet restrictTo(LanguageSet other);

        public static LanguageSet from(Set<String> langs) {
            if (langs.isEmpty()) {
                return NO_LANGUAGES;
            } else {
                return new SomeLanguages(langs);
            }
        }

        public abstract boolean contains(String language);

        public abstract boolean isSingleton();

        public abstract String getAny();

        public abstract boolean isEmpty();
    }

    /**
     * No languages at all.
     */
    public static LanguageSet NO_LANGUAGES = new LanguageSet() {
        @Override
        public LanguageSet restrictTo(LanguageSet other) {
            return this;
        }

        @Override
        public boolean contains(String language) {
            return false;
        }

        @Override
        public boolean isSingleton() {
            return false;
        }

        @Override
        public String getAny() {
            throw new NoSuchElementException("Can't fetch any language from the empty language set.");
        }

        @Override
        public boolean isEmpty() {
            return true;
        }
    };

    /**
     * Any/all languages.
     */
    public static LanguageSet ANY_LANGUAGE = new LanguageSet() {
        @Override
        public LanguageSet restrictTo(LanguageSet other) {
            return other;
        }

        @Override
        public boolean contains(String language) {
            return true;
        }

        @Override
        public boolean isSingleton() {
            return false;
        }

        @Override
        public String getAny() {
            throw new NoSuchElementException("Can't fetch any language from the any language set.");
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
    };

    /**
     * Some languages, explicitly enumerated.
     */
    public static class SomeLanguages extends LanguageSet {
        private final Set<String> languages;

        private SomeLanguages(Set<String> languages) {
            this.languages = Collections.unmodifiableSet(languages);
        }

        public Set<String> getLanguages() {
            return this.languages;
        }

        @Override
        public LanguageSet restrictTo(LanguageSet other) {
            if (other == NO_LANGUAGES) {
                return other;
            } else if (other == ANY_LANGUAGE) {
                return this;
            } else {
                SomeLanguages sl = (SomeLanguages) other;
                Set<String> ls = new HashSet<String>(this.languages);
                ls.retainAll(sl.languages);
                return from(ls);
            }
        }

        @Override
        public boolean contains(String language) {
            return this.languages.contains(language);
        }

        @Override
        public boolean isSingleton() {
            return this.languages.size() == 1;
        }

        @Override
        public String getAny() {
            return this.languages.iterator().next();
        }

        @Override
        public boolean isEmpty() {
            return this.languages.isEmpty();
        }
    }
}

@@ -4720,32 +4720,38 @@
   public static 
+final 
 LanguageSet NO_L
@@ -5407,32 +5407,38 @@
   public static 
+final 
 LanguageSet ANY_

Make static fields final.