| 1 | /* |
| 2 | * Copyright (c) 2005 The PseudoQ Project. |
| 3 | * |
| 4 | * This file is part of PseudoQ. |
| 5 | * |
| 6 | * PseudoQ is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Lesser General Public License as published by |
| 8 | * the Free Software Foundation; either version 2.1 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * PseudoQ is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public License |
| 17 | * along with PseudoQ; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | package net.sourceforge.pseudoq.solver; |
| 22 | |
| 23 | import net.sourceforge.pseudoq.model.Coordinate; |
| 24 | import net.sourceforge.pseudoq.model.Region; |
| 25 | |
| 26 | /** |
| 27 | * Counts the number of times a particular digit appears in a region. |
| 28 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 29 | */ |
| 30 | public class RegionValueIndicatorCounter implements Counter { |
| 31 | private int count = 0; |
| 32 | private Region region; |
| 33 | private int value = 0; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new instance of RegionValueIndicatorCounter. |
| 37 | * @param region The region to monitor. |
| 38 | * @param value The value to look for. |
| 39 | */ |
| 40 | public RegionValueIndicatorCounter(Region region, int value) { |
| 41 | if (region == null) { |
| 42 | throw new IllegalArgumentException("Can't create counter for null region"); |
| 43 | } |
| 44 | |
| 45 | this.region = region; |
| 46 | this.value = value; |
| 47 | } |
| 48 | |
| 49 | public void increment() { |
| 50 | count++; |
| 51 | } |
| 52 | |
| 53 | public void reset() { |
| 54 | count = 0; |
| 55 | } |
| 56 | |
| 57 | public void place(Coordinate coord, int value) { |
| 58 | if (coord == null) { |
| 59 | throw new IllegalArgumentException("Can't place a value with null coordinate"); |
| 60 | } |
| 61 | assert (region != null); |
| 62 | |
| 63 | if (region.contains(coord) && this.value == value) { |
| 64 | increment(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Setter for property count. |
| 70 | * @param count New value of property count. |
| 71 | */ |
| 72 | public void setCount(int count) { |
| 73 | this.count = count; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Getter for property count. |
| 78 | * @return Value of property count. |
| 79 | */ |
| 80 | public int getCount() { |
| 81 | return count; |
| 82 | } |
| 83 | |
| 84 | } |