001package ball.game.card;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * %%
006 * Copyright (C) 2010 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.ArrayList;
022import java.util.MissingResourceException;
023import java.util.ResourceBundle;
024import java.util.regex.Pattern;
025import org.apache.commons.lang3.StringUtils;
026
027/**
028 * {@link Card} deck.
029 *
030 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
031 */
032public abstract class Deck extends ArrayList<Card> implements Cloneable {
033    private static final long serialVersionUID = -1376087186450102030L;
034
035    /**
036     * Sole constructor.
037     */
038    protected Deck() {
039        super(Card.Suit.values().length * Card.Rank.values().length);
040
041        try {
042            var bundle = ResourceBundle.getBundle(getClass().getName());
043
044            for (var key : bundle.keySet()) {
045                var value = bundle.getString(key);
046
047                if (! StringUtils.isEmpty(value)) {
048                    for (var suit : key.split(Pattern.quote(","))) {
049                        for (var rank : value.split(Pattern.quote(","))) {
050                            add(new Card(Card.Suit.parse(suit), Card.Rank.parse(rank)));
051                        }
052                    }
053                } else {
054                    add(Card.parse(key));
055                }
056            }
057        } catch (Exception exception) {
058            throw new ExceptionInInitializerError(exception);
059        }
060    }
061
062    @Override
063    public Card[] toArray() { return toArray(new Card[] { }); }
064
065    @Override
066    public Deck clone() { return (Deck) super.clone(); }
067}