FODBIndexTable.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.database.fastobjectdb.db.index;
00030 
00031 import java.io.*;
00032 
00033 import org.openmobileis.database.fastobjectdb.db.index.node.Node;
00034 
00035 
00040 public class FODBIndexTable implements Externalizable {
00041   static final long serialVersionUID = 5521257935120563452L;
00042 
00043   private static int DEF_TAB_SIZE = 20;
00044         
00045         private int incTabSize;
00046         private int currentSize;
00047         
00048         private long[] indexsPos;
00049         
00050         public FODBIndexTable() {
00051                 incTabSize = DEF_TAB_SIZE;                              // default value;
00052                 constructorHelper();
00053         }
00054         
00055         public FODBIndexTable(int tSize) {
00056                 incTabSize = tSize;
00057                 constructorHelper();
00058         }
00059         
00060         protected void constructorHelper() {
00061                 currentSize = 0;
00062                 indexsPos = new long[incTabSize];
00063                 
00064                 for (int i = 0; i < incTabSize; i++) {
00065                         indexsPos[i] = Node.NO_NODE;
00066                 }
00067         }
00068         
00069         public int size() {
00070                 return currentSize;
00071         }
00072         
00073         public int allocatedSize() {
00074                 return indexsPos.length;
00075         }
00076         
00077         public synchronized boolean add(long pos) {
00078                 int tabSize = indexsPos.length;
00079                 
00080                 if (currentSize < tabSize) {
00081                         indexsPos[currentSize] = pos;
00082                         currentSize++;
00083                         return false;
00084                 }
00085                 else { // currentSize == tabSize
00086                         long[] newIndexsPos = new long[tabSize + incTabSize];
00087                         
00088                         System.arraycopy(indexsPos, 0, newIndexsPos, 0, indexsPos.length);
00089                         
00090                         newIndexsPos[currentSize] = pos;
00091                         
00092                         for (int i = currentSize+1; i<newIndexsPos.length; i++)  {
00093                                 newIndexsPos[i] = Node.NO_NODE;
00094                         }
00095                         
00096                         indexsPos = newIndexsPos;
00097                         currentSize++;
00098                         return true;
00099                 }
00100                 
00101         }
00102         
00103         public long getPosByPlace(int place) {
00104                 if (place >= currentSize || place < 0) {
00105                         return Node.NO_NODE;
00106                 }
00107                 return indexsPos[place];
00108         }
00109         
00110         public String toString() {
00111                 String result = "       FODBIndexTable[" + incTabSize + ", " + currentSize + "]\n";
00112                 
00113                 result = result.concat("         [");
00114                 for (int i = 0; i < currentSize; i++) {
00115                         result = result.concat("(" + indexsPos[i] + ")");
00116                 }
00117                 result = result.concat("]\n");
00118                 
00119                 return result;
00120         }
00121         
00122         public void writeExternal(ObjectOutput out) throws IOException {
00123                 out.writeInt(incTabSize);
00124                 out.writeInt(currentSize);
00125                 out.writeInt(indexsPos.length);
00126                 for (int i = 0; i < indexsPos.length; i++) {
00127                         out.writeLong(indexsPos[i]);
00128                 }
00129         }
00130         
00131         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
00132                 incTabSize = in.readInt();
00133                 currentSize = in.readInt();
00134                 int realTabSize = in.readInt();
00135                 
00136                 indexsPos = new long[realTabSize];
00137                 
00138                 for (int i = 0; i < realTabSize; i++) {
00139                         indexsPos[i] = in.readLong();
00140                 }
00141         }
00142 
00143 }
00144                 

Generated on Mon Dec 4 11:03:26 2006 for OpenMobileIS by  doxygen 1.5.1-p1