001package ball.game.sudoku; 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.Collection; 022import java.util.Collections; 023import java.util.List; 024import java.util.SortedSet; 025import java.util.TreeSet; 026import java.util.stream.IntStream; 027import lombok.NoArgsConstructor; 028 029import static java.util.stream.Collectors.toCollection; 030 031/** 032 * Sudoku {@link Digits}. 033 * 034 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 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 042 static { 043 var all = IntStream.rangeClosed(1, 9).boxed().collect(toCollection(TreeSet::new)); 044 045 ALL = Collections.unmodifiableSortedSet(all); 046 } 047 048 protected static final int SUM = ALL.stream().mapToInt(Integer::intValue).sum(); 049 050 /** 051 * See {@link #addAll(Collection)}. 052 * 053 * @param digits The digits to add. 054 * 055 * @return {@code true} if {@link.this} {@link Digits} changes; 056 * {@code false} otherwise. 057 */ 058 public boolean addAll(Integer... digits) { 059 return Collections.addAll(this, digits); 060 } 061 062 /** 063 * See {@link #removeAll(Collection)}. 064 * 065 * @param digits The digits to remove. 066 * 067 * @return {@code true} if {@link.this} {@link Digits} changes; 068 * {@code false} otherwise. 069 */ 070 public boolean removeAll(Integer... digits) { 071 return removeAll(List.of(digits)); 072 } 073 074 /** 075 * See {@link #retainAll(Collection)}. 076 * 077 * @param digits The digits to retain. 078 * 079 * @return {@code true} if {@link.this} {@link Digits} changes; 080 * {@code false} otherwise. 081 */ 082 public boolean retainAll(Integer... digits) { 083 return retainAll(List.of(digits)); 084 } 085 086 @Override 087 public boolean add(Integer digit) { 088 if (! ALL.contains(digit)) { 089 throw new IllegalArgumentException(String.valueOf(digit)); 090 } 091 092 return super.add(digit); 093 } 094 095 @Override 096 public boolean addAll(Collection<? extends Integer> collection) { 097 boolean modified = super.addAll(collection); 098 099 if (retainAll(ALL)) { 100 throw new IllegalArgumentException(String.valueOf(collection)); 101 } 102 103 return modified; 104 } 105}