001package ball.game.sudoku;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: Cell.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/Cell.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 */
023
024/**
025 * Sudoku {@link Cell}.
026 *
027 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
028 * @version $Revision: 5285 $
029 */
030public class Cell extends Digits {
031    private static final long serialVersionUID = -2029175165124035385L;
032
033    /**
034     * {@link #UNKNOWN} = {@value #UNKNOWN}
035     */
036    public static final String UNKNOWN = ".";
037
038    /**
039     * Sole constructor.  Construct with all possible digits.
040     */
041    public Cell() {
042        super();
043
044        addAll(ALL);
045    }
046
047    /**
048     * Method to determine if {@link.this} {@link Cell} is solved.
049     *
050     * @return  {@code true} if the {@link Cell} is solved; {@code false}
051     *          otherwise.
052     */
053    public boolean isSolved() { return (size() == 1); }
054
055    /**
056     * Method to get {@link.this} {@link Cell}'s solution.
057     *
058     * @return  The solution if the {@link Cell} is solved; {@code null}
059     *          otherwise.
060     */
061    public Integer solution() { return isSolved() ? first() : null; }
062
063    /**
064     * @return  The minimum value of the {@link Cell}.
065     *
066     * @see #first()
067     */
068    public Integer min() { return first(); }
069
070    /**
071     * @return  The maximum value of the {@link Cell}.
072     *
073     * @see #last()
074     */
075    public Integer max() { return last(); }
076
077    /**
078     * Method to determine if {@link.this} {@link Cell} is in the specified
079     * {@link Iterable} with {@code ==}.
080     *
081     * @param   iterable        The {@link Iterable} of {@link Object}s to
082     *                          test.
083     *
084     * @return  {@code true} if the {@link Cell} is in the {@link Iterable};
085     *          {@code false} otherwise.
086     */
087    public boolean isIn(Iterable<?> iterable) {
088        boolean isContained = false;
089
090        for (Object object : iterable) {
091            isContained |= (this == object);
092
093            if (isContained) {
094                break;
095            }
096        }
097
098        return isContained;
099    }
100
101    @Override
102    public String toString() {
103        return isSolved() ? String.valueOf(first()) : toString(this);
104    }
105
106    private String toString(Cell cell) {
107        StringBuilder buffer = new StringBuilder();
108
109        if (cell.size() == ALL.size()) {
110            buffer.append(".");
111        } else {
112            buffer.append("[");
113
114            for (int digit : cell) {
115                buffer.append(digit);
116            }
117
118            buffer.append("]");
119        }
120
121        return buffer.toString();
122    }
123}