001package ball.game.sudoku;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: Digits.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/sudoku/Digits.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 java.util.Arrays;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.SortedSet;
027import java.util.TreeSet;
028import lombok.NoArgsConstructor;
029
030/**
031 * Sudoku {@link Digits}.
032 *
033 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
034 * @version $Revision: 5285 $
035 */
036@NoArgsConstructor
037public class Digits extends TreeSet<Integer> {
038    private static final long serialVersionUID = 3540373718233836695L;
039
040    protected static final SortedSet<Integer> ALL;
041    protected static final int SUM;
042
043    static {
044        TreeSet<Integer> digits = new TreeSet<>();
045        int sum = 0;
046
047        for (int i = 1; i <= 9; i += 1) {
048            digits.add(i);
049            sum += i;
050        }
051
052        ALL = Collections.unmodifiableSortedSet(digits);
053        SUM = sum;
054    }
055
056    /**
057     * See {@link #addAll(Collection)}.
058     *
059     * @param   digits          The digits to add.
060     *
061     * @return  {@code true} if {@link.this} {@link Digits} changes;
062     *          {@code false} otherwise.
063     */
064    public boolean addAll(Integer... digits) {
065        return Collections.addAll(this, digits);
066    }
067
068    /**
069     * See {@link #removeAll(Collection)}.
070     *
071     * @param   digits          The digits to remove.
072     *
073     * @return  {@code true} if {@link.this} {@link Digits} changes;
074     *          {@code false} otherwise.
075     */
076    public boolean removeAll(Integer... digits) {
077        return removeAll(Arrays.asList(digits));
078    }
079
080    /**
081     * See {@link #retainAll(Collection)}.
082     *
083     * @param   digits          The digits to retain.
084     *
085     * @return  {@code true} if {@link.this} {@link Digits} changes;
086     *          {@code false} otherwise.
087     */
088    public boolean retainAll(Integer... digits) {
089        return retainAll(Arrays.asList(digits));
090    }
091
092    @Override
093    public boolean add(Integer digit) {
094        if (! ALL.contains(digit)) {
095            throw new IllegalArgumentException(String.valueOf(digit));
096        }
097
098        return super.add(digit);
099    }
100
101    @Override
102    public boolean addAll(Collection<? extends Integer> collection) {
103        boolean modified = super.addAll(collection);
104
105        if (retainAll(ALL)) {
106            throw new IllegalArgumentException(String.valueOf(collection));
107        }
108
109        return modified;
110    }
111}