001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/rtree/MemoryPageFile.java $
002 //----------------------------------------
003 //RTree implementation.
004 //Copyright (C) 2002-2004 Wolfgang Baer - WBaer@gmx.de
005 //
006 //This library is free software; you can redistribute it and/or
007 //modify it under the terms of the GNU Lesser General Public
008 //License as published by the Free Software Foundation; either
009 //version 2.1 of the License, or (at your option) any later version.
010 //
011 //This library is distributed in the hope that it will be useful,
012 //but WITHOUT ANY WARRANTY; without even the implied warranty of
013 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 //Lesser General Public License for more details.
015 //
016 //You should have received a copy of the GNU Lesser General Public
017 //License along with this library; if not, write to the Free Software
018 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019 //----------------------------------------
020
021 package org.deegree.io.rtree;
022
023 import java.io.Serializable;
024 import java.util.Hashtable;
025
026 /**
027 * <p>
028 * A memory based implementation of a PageFile.<br>
029 * Implemented as a Hashtable with keys representing the page file numbers of the saved nodes.
030 * </p>
031 *
032 * @author Wolfgang Baer - WBaer@gmx.de
033 */
034 class MemoryPageFile extends PageFile implements Serializable {
035
036 private Hashtable<Integer, Node> file = new Hashtable<Integer, Node>( 500 );
037
038 /**
039 * Constructor
040 */
041 protected MemoryPageFile() {
042 super();
043 file.clear();
044 }
045
046 /**
047 * @see PageFile#readNode(int)
048 */
049 protected Node readNode( int pageFile )
050 throws PageFileException {
051 return file.get( new Integer( pageFile ) );
052 }
053
054 /**
055 * @see PageFile#writeNode(Node)
056 */
057 protected int writeNode( Node node )
058 throws PageFileException {
059
060 int i = 0;
061 if ( node.getPageNumber() < 0 ) {
062 while ( true ) {
063 if ( !file.containsKey( new Integer( i ) ) ) {
064 break;
065 }
066 i++;
067 }
068 node.setPageNumber( i );
069 } else
070 i = node.getPageNumber();
071
072 file.put( new Integer( i ), node );
073
074 return i;
075 }
076
077 /**
078 * @see PageFile#deleteNode(int)
079 */
080 protected Node deleteNode( int pageNumber ) {
081 return file.remove( new Integer( pageNumber ) );
082 }
083
084 /**
085 * @see PageFile#close()
086 */
087 protected void close()
088 throws PageFileException {
089 // nothing to do
090 }
091 }