001package com.hfg.xml.msofficexml.xlsx; 002 003 004import com.hfg.util.CompareUtil; 005import com.hfg.util.StringUtil; 006 007//------------------------------------------------------------------------------ 008/** 009 Container for a simple spreadsheet cell range (ex: 'A1:D10'). 010 011 @author J. Alex Taylor, hairyfatguy.com 012 */ 013//------------------------------------------------------------------------------ 014// com.hfg XML/HTML Coding Library 015// 016// This library is free software; you can redistribute it and/or 017// modify it under the terms of the GNU Lesser General Public 018// License as published by the Free Software Foundation; either 019// version 2.1 of the License, or (at your option) any later version. 020// 021// This library is distributed in the hope that it will be useful, 022// but WITHOUT ANY WARRANTY; without even the implied warranty of 023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024// Lesser General Public License for more details. 025// 026// You should have received a copy of the GNU Lesser General Public 027// License along with this library; if not, write to the Free Software 028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029// 030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 031// jataylor@hairyfatguy.com 032//------------------------------------------------------------------------------ 033 034public class CellRange implements Comparable 035{ 036 private CellRef mBeginCell; 037 private CellRef mEndCell; 038 039 //########################################################################### 040 // CONSTRUCTORS 041 //########################################################################### 042 043 //--------------------------------------------------------------------------- 044 public CellRange(CellRef inBegin, CellRef inEnd) 045 { 046 mBeginCell = inBegin; 047 mEndCell = inEnd; 048 } 049 050 //--------------------------------------------------------------------------- 051 public CellRange(String inValue) 052 { 053 parse(inValue); 054 } 055 056 //########################################################################### 057 // PUBLIC METHODS 058 //########################################################################### 059 060 //--------------------------------------------------------------------------- 061 public CellRef getBeginCell() 062 { 063 return mBeginCell; 064 } 065 066 //--------------------------------------------------------------------------- 067 public CellRef getEndCell() 068 { 069 return mEndCell; 070 } 071 072 //--------------------------------------------------------------------------- 073 @Override 074 public String toString() 075 { 076 return mBeginCell + (mBeginCell.equals(mEndCell) ? "" : ":" + mEndCell); 077 } 078 079 //--------------------------------------------------------------------------- 080 @Override 081 public boolean equals(Object inObj2) 082 { 083 return (inObj2 != null 084 && inObj2 instanceof CellRange 085 && 0 == compareTo(inObj2)); 086 } 087 088 //-------------------------------------------------------------------------- 089 @Override 090 public int hashCode() 091 { 092 int hashCode = 0; 093 094 if (getBeginCell() != null) 095 { 096 hashCode = getBeginCell().hashCode(); 097 } 098 099 if (getEndCell() != null) 100 { 101 hashCode += 31 * getEndCell().hashCode(); 102 } 103 104 return hashCode; 105 } 106 107 //--------------------------------------------------------------------------- 108 public int compareTo(Object inObj2) 109 { 110 int result = 1; 111 112 if (inObj2 instanceof CellRange) 113 { 114 CellRange cellRange2 = (CellRange) inObj2; 115 116 result = CompareUtil.compare(getBeginCell(), cellRange2.getBeginCell()); 117 118 if (0 == result) 119 { 120 result = CompareUtil.compare(getEndCell(), cellRange2.getEndCell()); 121 } 122 } 123 124 return result; 125 } 126 127 //########################################################################### 128 // PRIVATE METHODS 129 //########################################################################### 130 131 //--------------------------------------------------------------------------- 132 private void parse(String inValue) 133 { 134 if (StringUtil.isSet(inValue)) 135 { 136 try 137 { 138 int colonIdx = inValue.indexOf(":"); 139 mBeginCell = new CellRef(inValue.substring(0, colonIdx)); 140 mEndCell = new CellRef(inValue.substring(colonIdx + 1)); 141 } 142 catch (Exception e) 143 { 144 throw new RuntimeException("Problem parsing cell range value " + StringUtil.singleQuote(inValue) + "!", e); 145 } 146 } 147 else 148 { 149 throw new RuntimeException("No value was specified for the cell range!"); 150 } 151 } 152}