All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.xlsx.CellRange Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.xlsx;


import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 Container for a simple spreadsheet cell range (ex: 'A1:D10').

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class CellRange implements Comparable
{
   private CellRef mBeginCell;
   private CellRef mEndCell;

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public CellRange(CellRef inBegin, CellRef inEnd)
   {
      mBeginCell = inBegin;
      mEndCell   = inEnd;
   }

   //---------------------------------------------------------------------------
   public CellRange(String inValue)
   {
      parse(inValue);
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public CellRef getBeginCell()
   {
      return mBeginCell;
   }

   //---------------------------------------------------------------------------
   public CellRef getEndCell()
   {
      return mEndCell;
   }

   //---------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return mBeginCell + (mBeginCell.equals(mEndCell) ? "" : ":" + mEndCell);
   }

   //---------------------------------------------------------------------------
   @Override
   public boolean equals(Object inObj2)
   {
      return (inObj2 != null
            && inObj2 instanceof CellRange
            && 0 == compareTo(inObj2));
   }

   //--------------------------------------------------------------------------
   @Override
   public int hashCode()
   {
      int hashCode = 0;

      if (getBeginCell() != null)
      {
         hashCode = getBeginCell().hashCode();
      }

      if (getEndCell() != null)
      {
         hashCode += 31 * getEndCell().hashCode();
      }

      return hashCode;
   }

   //---------------------------------------------------------------------------
   public int compareTo(Object inObj2)
   {
      int result = 1;

      if (inObj2 instanceof CellRange)
      {
         CellRange cellRange2 = (CellRange) inObj2;

         result = CompareUtil.compare(getBeginCell(), cellRange2.getBeginCell());

         if (0 == result)
         {
            result = CompareUtil.compare(getEndCell(), cellRange2.getEndCell());
         }
      }

      return result;
   }

   //###########################################################################
   // PRIVATE METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   private void parse(String inValue)
   {
      if (StringUtil.isSet(inValue))
      {
         try
         {
            int colonIdx = inValue.indexOf(":");
            mBeginCell = new CellRef(inValue.substring(0, colonIdx));
            mEndCell   = new CellRef(inValue.substring(colonIdx + 1));
         }
         catch (Exception e)
         {
            throw new RuntimeException("Problem parsing cell range value " + StringUtil.singleQuote(inValue) + "!", e);
         }
      }
      else
      {
         throw new RuntimeException("No value was specified for the cell range!");
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy