com.hfg.sql.table.DatabaseRowWithIntPrimaryKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.sql.table;
import java.sql.ResultSet;
import java.util.List;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Database row object which has an Integer as the 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 DatabaseRowWithIntPrimaryKey extends DatabaseRow
{
// Cached value
private Integer mRowId;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public DatabaseRowWithIntPrimaryKey(DatabaseTable extends DatabaseRow> inTable)
{
super(inTable);
}
//---------------------------------------------------------------------------
public DatabaseRowWithIntPrimaryKey(DatabaseTable extends DatabaseRow> inTable, ResultSet inResultSet)
{
super(inTable, inResultSet);
}
//---------------------------------------------------------------------------
public DatabaseRowWithIntPrimaryKey(XMLTag inXMLTag)
{
super(inXMLTag);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public Integer getId()
{
if (null == mRowId)
{
mRowId = (Integer) 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 extends DatabaseRowWithIntPrimaryKey> inList,
Integer inTargetRowId)
{
int listSize = inList.size();
int leftIdx = 0;
int rightIdx = inList.size() - 1;
DatabaseRowWithIntPrimaryKey leftRow = inList.get(leftIdx);
DatabaseRowWithIntPrimaryKey rightRow = inList.get(rightIdx);
while (leftIdx < rightIdx)
{
// Guess where to check next
int nextIdx = leftIdx + ((inTargetRowId - leftRow.getId()) * (rightIdx - leftIdx) /
(rightRow.getId() - leftRow.getId()));
if (nextIdx < 0)
{
nextIdx = 0;
}
else if (nextIdx >= listSize)
{
nextIdx = listSize - 1;
}
int comparison = inTargetRowId - inList.get(nextIdx).getId();
if (0 == comparison)
{
return nextIdx;
}
else if (comparison < 0)
{
if (nextIdx <= 0
|| rightIdx == nextIdx - 1) // Or we'll end up in an infinite loop
{
break;
}
rightIdx = nextIdx - 1;
rightRow = inList.get(rightIdx);
}
else
{
if (nextIdx == listSize - 1
|| leftIdx == nextIdx + 1) // Or we'll end up in an infinite loop
{
break;
}
leftIdx = nextIdx + 1;
leftRow = inList.get(leftIdx);
}
}
return (inTargetRowId.equals(leftRow.getId()) ? leftIdx : -1);
}
}