001package ball.game.scrabble;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: WordList.html 5431 2020-02-12 19:03:17Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2019-07-08-fivethirtyeight-best-scrabble-string/src/main/resources/javadoc/src-html/ball/game/scrabble/WordList.html $
007 * %%
008 * Copyright (C) 2010 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.beans.ConstructorProperties;
024import java.util.Locale;
025import java.util.ResourceBundle;
026import java.util.TreeSet;
027
028import static java.util.Comparator.comparing;
029import static java.util.Objects.requireNonNull;
030import static org.apache.commons.lang3.StringUtils.isNotBlank;
031
032/**
033 * Abstract Word List base class.
034 *
035 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
036 * @version $Revision: 5431 $
037 */
038public abstract class WordList extends TreeSet<CharSequence> {
039    private static final long serialVersionUID = 9123060041133450168L;
040
041    /** @serial */ private final Locale locale;
042
043    /**
044     * Sole constructor.
045     *
046     * @param   locale          The {@link Locale}.
047     */
048    @ConstructorProperties({ "locale" })
049    protected WordList(Locale locale) {
050        super(comparing(CharSequence::toString, String.CASE_INSENSITIVE_ORDER));
051
052        this.locale = requireNonNull(locale);
053
054        ResourceBundle bundle =
055            ResourceBundle.getBundle(getClass().getName(), getLocale());
056
057        bundle.keySet()
058            .stream()
059            .map(t -> t.split("#", 2)[0])
060            .map(t -> t.split(" ", 2)[0])
061            .filter(t -> isNotBlank(t))
062            .map(t -> t.trim().toUpperCase())
063            .forEach(t -> this.add(t));
064    }
065
066    /**
067     * Method to get the {@link Locale} for this {@link WordList}.
068     *
069     * @return  The {@link Locale} for this {@link WordList}.
070     */
071    public Locale getLocale() { return locale; }
072
073    @Override
074    public CharSequence[] toArray() { return toArray(new CharSequence[] { }); }
075}