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

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

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


import com.hfg.exception.InvalidValueException;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

//------------------------------------------------------------------------------
/**
 Represents a cell location in a spreadsheet (ex: 'B11').

 @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 CellRef implements Comparable
{
   private Integer mColIndex; // 1-based column index
   private Integer mRowIndex; // 1-based row index
   private String  mRefString;

   private static final Pattern CELL_REF_PATTERN = Pattern.compile("([A-Z]{1,3})(\\d+)");

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

   //---------------------------------------------------------------------------
   public CellRef()
   {

   }

   //---------------------------------------------------------------------------
   public CellRef(String inValue)
   {
      if (StringUtil.isSet(inValue))
      {
         mRefString = inValue.toUpperCase().trim();
         Matcher m = CELL_REF_PATTERN.matcher(mRefString);
         if (! m.matches())
         {
            throw new InvalidValueException("The cell reference " + StringUtil.singleQuote(inValue) + " is not in a valid format!");
         }

         mColIndex = 0;
         int colPosition = m.group(1).length();
         for (char colChar : m.group(1).toCharArray())
         {
            mColIndex += (int) (Math.pow(26, colPosition - 1)) * ((int) colChar - 64);
            colPosition--;
         }

         mRowIndex = Integer.parseInt(m.group(2));
      }
   }

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

   //---------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return mRefString;
   }

   //---------------------------------------------------------------------------
   /**
    Specifies the column index as a 1-based number.
    */
   public CellRef setColIndex(int inValue)
   {
      mColIndex = inValue;
      calcRefString();
      return this;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the column index as a 1-based number.
    */
   public Integer getColIndex()
   {
      return mColIndex;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the column as a String.
    */
   public String getCol()
   {
      return mColIndex != null ? colIndexToString(mColIndex) : null;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the next column as a String.
    */
   public String nextCol()
   {
      return mColIndex != null ? colIndexToString(mColIndex + 1) : null;
   }

   //---------------------------------------------------------------------------
   /**
    Specifies the row index as a 1-based number.
    */
   public CellRef setRowIndex(int inValue)
   {
      mRowIndex = inValue;
      calcRefString();
      return this;
   }

   //---------------------------------------------------------------------------
   /**
    Returns the row index as a 1-based number.
    */
   public Integer getRowIndex()
   {
      return mRowIndex;
   }

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

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

      if (getColIndex() != null)
      {
         hashCode = getColIndex();
      }

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

      return hashCode;
   }

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

      if (inObj2 instanceof CellRef)
      {
         CellRef cellRef2 = (CellRef) inObj2;

         result = CompareUtil.compare(getColIndex(), cellRef2.getColIndex());

         if (0 == result)
         {
            result = CompareUtil.compare(getRowIndex(), cellRef2.getRowIndex());
         }
      }

      return result;
   }

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

   //---------------------------------------------------------------------------
   private void calcRefString()
   {
      mRefString = null;
      if (mColIndex != null
          && mRowIndex != null)
      {
         mRefString = String.format("%s%d", colIndexToString(mColIndex), mRowIndex);
      }
   }

   //---------------------------------------------------------------------------
   // Recursively turns a column index into an A-Z string reference.
   private static String colIndexToString(int inColIndex)
   {
      int baseValue = (int) 'A';
      int zeroBasedColIndex = inColIndex - 1;

      String refString = "";

      if (inColIndex > 26)
      {
         refString = colIndexToString(zeroBasedColIndex / 26) ;
      }

      return refString + (char) (baseValue + (zeroBasedColIndex % 26) );
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy