001package ball.game.card;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: Deck.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-10-29-java-enums-as-predicates/src/main/resources/javadoc/src-html/ball/game/card/Deck.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.util.ArrayList;
024import java.util.MissingResourceException;
025import java.util.ResourceBundle;
026import java.util.regex.Pattern;
027import org.apache.commons.lang3.StringUtils;
028
029/**
030 * {@link Card} deck.
031 *
032 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
033 * @version $Revision: 5431 $
034 */
035public abstract class Deck extends ArrayList<Card> implements Cloneable {
036    private static final long serialVersionUID = -1376087186450102030L;
037
038    /**
039     * Sole constructor.
040     */
041    protected Deck() {
042        super(Card.Suit.values().length * Card.Rank.values().length);
043
044        try {
045            ResourceBundle bundle =
046                ResourceBundle.getBundle(getClass().getName());
047
048            for (String key : bundle.keySet()) {
049                String value = bundle.getString(key);
050
051                if (! StringUtils.isEmpty(value)) {
052                    for (String suit : key.split(Pattern.quote(","))) {
053                        for (String rank : value.split(Pattern.quote(","))) {
054                            add(new Card(Card.Suit.parse(suit),
055                                         Card.Rank.parse(rank)));
056                        }
057                    }
058                } else {
059                    add(Card.parse(key));
060                }
061            }
062        } catch (Exception exception) {
063            throw new ExceptionInInitializerError(exception);
064        }
065    }
066
067    @Override
068    public Card[] toArray() { return toArray(new Card[] { }); }
069
070    @Override
071    public Deck clone() { return (Deck) super.clone(); }
072}