001package ball.game.scrabble;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: Rack.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/scrabble/Rack.java $
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 ball.util.stream.Combinations;
024import java.util.LinkedList;
025import java.util.List;
026import java.util.stream.Stream;
027
028import static java.util.Objects.requireNonNull;
029
030/**
031 * Scrabble {@link Rack}.
032 *
033 * {@bean.info}
034 *
035 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
036 * @version $Revision: 5285 $
037 */
038public class Rack extends LinkedList<Tile> implements Cloneable {
039    private static final long serialVersionUID = -888967675416820573L;
040
041    private static final int CAPACITY = 7;
042
043    /**
044     * Sole constructor.
045     */
046    public Rack() { super(); }
047
048    /**
049     * Method to determine if the {@link Rack} has any remaining capacity
050     * (should draw).
051     *
052     * @return  {@code true} if the {@link Rack} contains less than its
053     *          maximum capacity; {@code false} otherwise.
054     */
055    public boolean hasCapacity() { return (size() < CAPACITY); }
056
057    /**
058     * Method to draw {@link Tile}s from a {@link Bag}.
059     *
060     * @param   bag             The {@link Bag} from which to draw.
061     *
062     * @return  The {@link List} of {@link Tile}s drawn.
063     */
064    public List<Tile> draw(Bag bag) {
065        int from = size();
066
067        synchronized (bag) {
068            while (hasCapacity() && (! bag.isEmpty())) {
069                add(bag.draw());
070            }
071        }
072
073        return new LinkedList<Tile>(subList(from, size()));
074    }
075
076    /**
077     * Method to get the {@link Stream} of all possible combinations.
078     *
079     * @return  The {@link Stream} of combinations (each a {@link List}
080     *          of {@link Tile}s).
081     *
082     * @see Combinations
083     */
084    public Stream<List<Tile>> combinations() {
085        return Combinations.of(size(), 1, null, this);
086    }
087
088    @Override
089    public boolean add(Tile tile) {
090        if (! hasCapacity()) {
091            throw new IllegalStateException();
092        }
093
094        requireNonNull(tile);
095
096        return super.add(tile);
097    }
098
099    @Override
100    public Tile[] toArray() { return toArray(new Tile[] { }); }
101
102    @Override
103    public Rack clone() { return (Rack) super.clone(); }
104}