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

com.hfg.sql.table.DatabaseRowWithLongPrimaryKey Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.sql.table;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hfg.xml.XMLTag;

//------------------------------------------------------------------------------
/**
 Database row object which has Long as a primary key.
 
@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 abstract class DatabaseRowWithLongPrimaryKey extends DatabaseRow { // Cached value private Long mRowId; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public DatabaseRowWithLongPrimaryKey(DatabaseTable inTable) { super(inTable); } //--------------------------------------------------------------------------- public DatabaseRowWithLongPrimaryKey(DatabaseTable inTable, ResultSet inResultSet) { super(inTable, inResultSet); } //--------------------------------------------------------------------------- public DatabaseRowWithLongPrimaryKey(XMLTag inXMLTag) { super(inXMLTag); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public Long getId() { if (null == mRowId) { mRowId = (Long) getField(getIdCol()).getValue(); } return mRowId; } //--------------------------------------------------------------------------- @SuppressWarnings("unchecked") public void clearId() { getField(getIdCol()).setValue(null); mRowId = null; } //--------------------------------------------------------------------------- /** Searches a List of DatabaseRowWithLongPrimaryKey objects sorted in ascending order to find the matching row with the specified id. Slower than a HashMap approach but more memory efficient. More performant when used with ArrayLists than with LinkedLists. * @param inList the sorted List to be searched * @param inTargetRowId the row id to be searched for * @return the index of the row with the matching rowId or -1 if it is not present in the list */ public static int interpolationSearch(List inList, Long inTargetRowId) { int listSize = inList.size(); int leftIdx = 0; int rightIdx = inList.size() - 1; DatabaseRowWithLongPrimaryKey leftRow = inList.get(leftIdx); DatabaseRowWithLongPrimaryKey rightRow = inList.get(rightIdx); while (leftIdx < rightIdx) { // Guess where to check next int nextIdx = leftIdx + (int) ((inTargetRowId - leftRow.getId()) * (rightIdx - leftIdx) / (rightRow.getId() - leftRow.getId())); if (nextIdx < 0) { nextIdx = 0; } else if (nextIdx >= listSize) { nextIdx = listSize - 1; } long comparison = inTargetRowId - inList.get(nextIdx).getId(); if (0 == comparison) { return nextIdx; } else if (comparison < 0) { if (nextIdx <= 0 || rightIdx <= nextIdx - 1) { break; // Or we'll end up in an infinite loop } rightIdx = nextIdx - 1; rightRow = inList.get(rightIdx); } else { if (nextIdx == listSize - 1 || leftIdx >= nextIdx + 1) { break; // Or we'll end up in an infinite loop } leftIdx = nextIdx + 1; leftRow = inList.get(leftIdx); } } return (inTargetRowId.equals(leftRow.getId()) ? leftIdx : -1); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy