|
Unofficial "CLDC 1.0 + MIDP 1.0" API Reference. (日本語版) |
|||||||||
| 前のパッケージ 次のパッケージ | フレームあり フレームなし | |||||||||
参照先:
説明
| インタフェースの概要 | |
|---|---|
| RecordComparator | このインタフェースは、どれがそれらに一致するか、それらの相対的なソートの序列位置を確かめるために2つのレコードを(実装が定義した方法にて)比較するコンパレータを定義します。 |
| RecordEnumeration | レコード・ストアの中のレコードを双方向に移動可能な RecordEnumeration を定義するインタフェースです。 |
| RecordFilter | そのレコードが、アプリケーションが定義した判定基準にマッチしているかどうかをチェックするフィルタを定義するインタフェースです。 |
| RecordListener | レコード・ストアにレコードが追加/変更/削除された際のイベントを受け取るリスナーを定義するインタフェースです。 |
| クラスの概要 | |
|---|---|
| RecordStore | レコード・ストアを表すクラスです。 |
| 例外の概要 | |
|---|---|
| InvalidRecordIDException | レコード ID が無効なため、オペレーションを完了することができなかった場合に throw される例外です。 |
| RecordStoreException | レコード・ストアの操作において一般的な例外が発生したことを示す例外クラスです。 |
| RecordStoreFullException | システムストレージが足りないため、レコード・ストアに対する操作を完了することができなかったことを示す例外クラスです。 |
| RecordStoreNotFoundException | 対象となるレコード・ストアを見つけることができなかったため、操作を完了できなかったことを示す例外クラスです。 |
| RecordStoreNotOpenException | クローズされたレコード・ストアに対して操作を行おうとしたことを示す例外クラスです。 |
Mobile Information Devie Profile は、永続的にデータを格納し、それをその後検索するためのメカニズムを MIDlet に提供します。 この永続的な記録メカニズムはシンプルなレコード志向のデータベースに習って設計されており、レコード・マネジメント・システムと呼ばれます。
例:
これは、ゲームに置いてハイスコアを格納し、検索するためにレコード・マネジメント・システムを使用する例です。
この例では、ハイスコアを別々のレコードに蓄積し、RecordEnumeration を使用してソートを行います。
import javax.microedition.rms.*;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
/**
* ゲームのスコアを蓄積し表示するクラスです。
*/
public class RMSGameScores
implements RecordFilter, RecordComparator
{
/*
* ゲームのスコアを蓄積するために使用するレコード・ストアです。
*/
private RecordStore recordStore = null;
/*
* プレイヤーの名前でフィルタリングする際に使用します。
*/
public static String playerNameFilter = null;
/*
* RecordFilter インタフェースの一部です。
*/
public boolean matches(byte[] candidate)
throws IllegalArgumentException
{
// もしフィルターが設定されていなければ一致する項目はありません。
if (this.playerNameFilter == null) {
return false;
}
ByteArrayInputStream bais = new ByteArrayInputStream(candidate);
DataInputStream inputStream = new DataInputStream(bais);
String name = null;
try {
int score = inputStream.readInt();
name = inputStream.readUTF();
}
catch (EOFException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
catch (IOException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
return (this.playerNameFilter.equals(name));
}
/*
* RecordComparator インタフェースの一部です。
*/
public int compare(byte[] rec1, byte[] rec2)
{
// レコードからスコアを取り出すための DataInputStream を構築します。
ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
DataInputStream inputStream1 = new DataInputStream(bais1);
ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
DataInputStream inputStream2 = new DataInputStream(bais2);
int score1 = 0;
int score2 = 0;
try {
// スコアを取り出します。
score1 = inputStream1.readInt();
score2 = inputStream2.readInt();
}
catch (EOFException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
catch (IOException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
// スコアをソートします。
if (score1 < score2) {
return RecordComparator.PRECEDES;
}
else if (score1 > score2) {
return RecordComparator.FOLLOWS;
}
else {
return RecordComparator.EQUIVALENT;
}
}
/**
* コンストラクタは必要に応じてレコード・ストアを作成あるいはオープンします。
*/
public RMSGameScores()
{
//
// 新しいレコード・ストアをこの例のために作成します。
//
try {
recordStore = RecordStore.openRecordStore("scores", true);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
}
/**
* 新しいレコード・ストアをストレージに追加します。
*
* @param score 保存するスコア。
* @param playerName このスコアを出したプレイヤーの名前。
*/
public void addScore(int score, String playerName)
{
//
// 各スコアはスコア、プレイヤーの名前の順番に個別のレコードに格納します。
//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try {
// スコアを byte 配列にして格納します。
outputStream.writeInt(score);
// 次にプレイヤーの名前を格納します。
outputStream.writeUTF(playerName);
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
// byte 配列を取り出します。
byte[] b = baos.toByteArray();
// これをレコード・ストアに追加します。
try {
recordStore.addRecord(b, 0, b.length);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
}
/**
* printScores メソッドのためのヘルパーメソッドです。
*/
private void printScoresHelper(RecordEnumeration re)
{
try {
while(re.hasNextElement()) {
int id = re.nextRecordIndex();
ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id));
DataInputStream inputStream = new DataInputStream(bais);
try {
int score = inputStream.readInt();
String playerName = inputStream.readUTF();
System.out.println(playerName + " = " + score);
}
catch (EOFException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
}
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
}
/**
* このメソッドはゲームのスコアをスコア順にソートして全て表示します。
*/
public void printScores()
{
try {
// ゲームのスコアを実装したコンパレータを使用してソートを行い、
// レコードを列挙します。
RecordEnumeration re = recordStore.enumerateRecords(null, this,
true);
printScoresHelper(re);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
}
/**
* 指定されたプレイヤーの名前のゲームのスコアをソートして全て表示します。
*/
public void printScores(String playerName)
{
try {
// ゲームのスコアを実装したコンパレータおよびフィルターを使用してソートを行い、
// レコードを列挙します。
RecordEnumeration re = recordStore.enumerateRecords(this, this,
true);
printScoresHelper(re);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
}
public static void main(String[] args)
{
RMSGameScores rmsgs = new RMSGameScores();
rmsgs.addScore(100, "Alice");
rmsgs.addScore(120, "Bill");
rmsgs.addScore(80, "Candice");
rmsgs.addScore(40, "Dean");
rmsgs.addScore(200, "Ethel");
rmsgs.addScore(110, "Farnsworth");
rmsgs.addScore(220, "Farnsworth");
System.out.println("All scores");
rmsgs.printScores();
System.out.println("Farnsworth's scores");
RMSGameScores.playerNameFilter = "Farnsworth";
rmsgs.printScores("Farnsworth");
}
}
|
Unofficial "CLDC 1.0 + MIDP 1.0" API Reference. (日本語版) |
|||||||||
| 前のパッケージ 次のパッケージ | フレームあり フレームなし | |||||||||
