1 2 /* ==================================================================== 3 * The Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2002 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Apache" and "Apache Software Foundation" and 28 * "Apache POI" must not be used to endorse or promote products 29 * derived from this software without prior written permission. For 30 * written permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * "Apache POI", nor may "Apache" appear in their name, without 34 * prior written permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation. For more 52 * information on the Apache Software Foundation, please see 53 * <http://www.apache.org/>. 54 */ 55 56 package org.apache.poi.hssf.util; 57 58 import org.apache.poi.hssf.record.MergeCellsRecord.MergedRegion; 59 60 /** 61 * Represents a from/to row/col square. This is a object primitive 62 * that can be used to represent row,col - row,col just as one would use String 63 * to represent a string of characters. Its really only useful for HSSF though. 64 * 65 * @author Andrew C. Oliver acoliver at apache dot org 66 */ 67 68 public class Region 69 implements Comparable 70 { 71 private int rowFrom; 72 private short colFrom; 73 private int rowTo; 74 private short colTo; 75 76 /** 77 * Creates a new instance of Region (0,0 - 0,0) 78 */ 79 80 public Region() 81 { 82 } 83 84 public Region(int rowFrom, short colFrom, int rowTo, short colTo) 85 { 86 this.rowFrom = rowFrom; 87 this.rowTo = rowTo; 88 this.colFrom = colFrom; 89 this.colTo = colTo; 90 } 91 92 /** 93 * special constructor (I know this is bad but it is so wrong that its right 94 * okay) that makes a region from a mergedcells's region subrecord. 95 */ 96 97 public Region(MergedRegion region) 98 { 99 this(region.row_from, region.col_from, region.row_to, region.col_to); 100 } 101 102 /** 103 * get the upper left hand corner column number 104 * 105 * @return column number for the upper left hand corner 106 */ 107 108 public short getColumnFrom() 109 { 110 return colFrom; 111 } 112 113 /** 114 * get the upper left hand corner row number 115 * 116 * @return row number for the upper left hand corner 117 */ 118 119 public int getRowFrom() 120 { 121 return rowFrom; 122 } 123 124 /** 125 * get the lower right hand corner column number 126 * 127 * @return column number for the lower right hand corner 128 */ 129 130 public short getColumnTo() 131 { 132 return colTo; 133 } 134 135 /** 136 * get the lower right hand corner row number 137 * 138 * @return row number for the lower right hand corner 139 */ 140 141 public int getRowTo() 142 { 143 return rowTo; 144 } 145 146 /** 147 * set the upper left hand corner column number 148 * 149 * @param colFrom column number for the upper left hand corner 150 */ 151 152 public void setColumnFrom(short colFrom) 153 { 154 this.colFrom = colFrom; 155 } 156 157 /** 158 * set the upper left hand corner row number 159 * 160 * @param rowFrom row number for the upper left hand corner 161 */ 162 163 public void setRowFrom(int rowFrom) 164 { 165 this.rowFrom = rowFrom; 166 } 167 168 /** 169 * set the lower right hand corner column number 170 * 171 * @param colTo column number for the lower right hand corner 172 */ 173 174 public void setColumnTo(short colTo) 175 { 176 this.colTo = colTo; 177 } 178 179 /** 180 * get the lower right hand corner row number 181 * 182 * @param rowTo row number for the lower right hand corner 183 */ 184 185 public void setRowTo(int rowTo) 186 { 187 this.rowTo = rowTo; 188 } 189 190 /** 191 * Answers: "is the row/column inside this range?" 192 * 193 * @returns boolean - true if the cell is in the range and false if it is not 194 */ 195 196 public boolean contains(int row, short col) 197 { 198 if ((this.rowFrom <= row) && (this.rowTo >= row) 199 && (this.colFrom <= col) && (this.colTo >= col)) 200 { 201 202 // System.out.println("Region ("+rowFrom+","+colFrom+","+rowTo+","+ 203 // colTo+") does contain "+row+","+col); 204 return true; 205 } 206 return false; 207 } 208 209 public boolean equals(Region r) 210 { 211 return (compareTo(r) == 0); 212 } 213 214 /** 215 * Compares that the given region is the same less than or greater than this 216 * region. If any regional coordiant passed in is less than this regions 217 * coordinants then a positive integer is returned. Otherwise a negative 218 * integer is returned. 219 * 220 * @param r region 221 * @see #compareTo(Object) 222 */ 223 224 public int compareTo(Region r) 225 { 226 if ((this.getRowFrom() == r.getRowFrom()) 227 && (this.getColumnFrom() == r.getColumnFrom()) 228 && (this.getRowTo() == r.getRowTo()) 229 && (this.getColumnTo() == r.getColumnTo())) 230 { 231 return 0; 232 } 233 if ((this.getRowFrom() < r.getRowFrom()) 234 || (this.getColumnFrom() < r.getColumnFrom()) 235 || (this.getRowTo() < r.getRowTo()) 236 || (this.getColumnTo() < r.getColumnTo())) 237 { 238 return 1; 239 } 240 return -1; 241 } 242 243 public int compareTo(Object o) 244 { 245 return compareTo(( Region ) o); 246 } 247 248 /** 249 * @returns the area contained by this region (number of cells) 250 */ 251 252 public int getArea() 253 { 254 return ((1 + (getRowTo() - getRowFrom())) 255 * (1 + (getColumnTo() - getColumnFrom()))); 256 } 257 } 258