| 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.model; |
| 22 | |
| 23 | import java.util.HashSet; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * A region of cells in the grid. Essentially it's a set of coordinates, plus |
| 28 | * some other metadata. |
| 29 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
| 30 | */ |
| 31 | public class Region extends HashSet<Coordinate> { |
| 32 | /** |
| 33 | * Holds value of property name. |
| 34 | */ |
| 35 | private String name; |
| 36 | |
| 37 | /** Creates a new instance of Region */ |
| 38 | public Region() { |
| 39 | } |
| 40 | |
| 41 | /** Creates a new instance of Region */ |
| 42 | public Region(String name) { |
| 43 | this.name = name; |
| 44 | } |
| 45 | |
| 46 | /** Creates a new instance of Region */ |
| 47 | public Region(Set<Coordinate> coords) { |
| 48 | this.addAll(coords); |
| 49 | } |
| 50 | |
| 51 | /** Creates a new instance of Region */ |
| 52 | public Region(String name, Set<Coordinate> coords) { |
| 53 | this.name = name; |
| 54 | this.addAll(coords); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Getter for property name. |
| 59 | * @return Value of property name. |
| 60 | */ |
| 61 | public String getName() { |
| 62 | return this.name; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Setter for property name. |
| 67 | * @param name New value of property name. |
| 68 | */ |
| 69 | public void setName(String name) { |
| 70 | this.name = name; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Calculates the union of this region and another. |
| 75 | * @param region The other region. |
| 76 | * @return A new region containing the union. |
| 77 | */ |
| 78 | public Region union(Region region) { |
| 79 | Region union = new Region(this); |
| 80 | union.addAll(region); |
| 81 | return union; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Calculates the intersection of this region and another. |
| 86 | * @param region The other region. |
| 87 | * @return A new region containing the union. |
| 88 | */ |
| 89 | public Region intersection(Region region) { |
| 90 | Region intersection = new Region(this); |
| 91 | intersection.retainAll(region); |
| 92 | return intersection; |
| 93 | } |
| 94 | |
| 95 | public String toString() { |
| 96 | String retValue; |
| 97 | |
| 98 | if (this.name != null) { |
| 99 | retValue = this.name; |
| 100 | } else { |
| 101 | retValue = super.toString(); |
| 102 | } |
| 103 | |
| 104 | return retValue; |
| 105 | } |
| 106 | |
| 107 | } |