package basics.collections.index;

import java.io.IOException;

import basics.filesystem.serializer.Serializer;
import basics.utl.SUtl;

/**
 * @author ks
 * Base class for data stored in DTable.
 * DTable contains the contents indexed in the btree.
 */
public class Indexable
{
private static final String TOSTRING = "[%/%]";
public transient int        _index   = -1;
public String               _key     = null;

public Indexable(String key)
{
   this._key = key;
}

public Indexable create()
{
   return new Indexable(null);
}

/**
 * Read a row and update the index. 
 * @param serializer
 * @param index
 * @return
 */
public boolean load(Serializer serializer)
{
   try
   {
      _key = serializer.readString();
      return true;
   }
   catch(IOException e)
   {
      return false;
   }
}

public boolean save(Serializer serializer)
{
   try
   {
      serializer.write(_key);
      return true;
   }
   catch(IOException e)
   {
      return false;
   }
}

public String toString()
{
   return SUtl.sprint(TOSTRING, _index, _key);
}
}
