[go: up one dir, main page]

Menu

[r12]: / database / DB.java  Maximize  Restore  History

Download this file

115 lines (114 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package database;
import cell.*;
/**
This class represents a table. Its contents are automatically loaded from disk.
When a part of the table is modified, it is immediately written back to disk.
The table does not have fixed data types, but can return both floating point values and character strings.
*/
public final class DB
{
CellBase c ;
/**
This loads a file from disk, and into memory, where it can be accessed by you.
@param tableName
Please consult {@link database.Tables} for more information.
@throws IllegalArgumentException If your table's name is unacceptable.
@throws IOException Never. If there is such an exception, the program just calls System.exit() pronto.
*/
public DB(String tableName)
{
c = new CellFinal(tableName);
}
/**
Retrieve a count of added records.
@return The number of populated rows in this table.
*/
public int length()
{
return c.max()+1;
}
/**
Apply a new value to this particular cell.
<p>
Then write this value to disk, before returning.
@param x Column >= 0
@param y Row >= 0
@param value The new value, in floating point format.
*/
public void setFloat(int x, int y, float value)
{
setString(x, y, ""+value);
}
/**
Modify the value of a particular cell.
<p>
The data is then writtn to disk, straight away.
@param x The column in question. You can set 0, and then 60, whatever.
@param y Row offset.
@param value The new value you want it to be, expressed as a character string.
*/
public void setString(int x, int y, String value)
{
c.so((byte)x, (short)y, value);
}
/**
Return the contents of this cell, as a float.
@param x Column.
@param y row >= 0
@return The last value set to this cell.
If the contents are not a float, or if ese coordinates are out of range, zero is returned.
*/
public float getFloat(int x, int y)
{
return c.af((byte)x, (short)y);
}
/**
Get the data for a cell.
@param x Column offset >= 0
@param y Row offset.
@returns The data in this cell.
If the coordinates are out of range, "" is returned.
*/
public String getString(int x, int y)
{
return ""+c.go((byte)x, (short)y);
}
/**
Delete a row, with one caveat:
<p>
The row is not deleted on disk, only in memory.
So you would have to delete this row every time you load from file.
This means that you can only use this method within the constructor of your class.
@param y The offset of the row. Zero-based.
@throws Exception If you try to delete a non-existent row.
*/
public void delete(int y)
{
c.ramDelete((short)y);
}
/**
Appends a new row to the bottom of this table.
<p>
It is written to disk immediately.
@param values An array of either Float or String objects, although probably anything will work.
These will be the contents of the new row, on disk, for example:
values[0] == getFloat(0, length()-1)
*/
public void add(Object[] values)
{
int y = length();
for (int x = 0; x < values.length; x++)
setString(x, y, values[x]+"");
}
/**
Test case.
*/
public static void main(String[] dummy)
{
String name = "TST";
DB db = new DB(name);
float val = (float)Math.random();
db.setFloat(0, 0, val);
assert (new DB(name).getFloat(0, 0) == val);
}
}