📄 uniar.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import org.garret.perst.StorageFactory;
import org.garret.perst.Storage;
import org.garret.perst.Key;
import org.garret.perst.Index;
public class UniAR extends MIDlet implements CommandListener
{
static Command QUIT_CMD = new Command("Quit", Command.EXIT, 1);
static Command PLAYER_CMD = new Command("Player", Command.ITEM, 1);
static Command SEARCH_CMD = new Command("Search", Command.OK, 1);
static Command BACK_CMD = new Command("Back", Command.BACK, 1);
static Command NEXT_CMD = new Command("Next", Command.OK, 1);
static final String PLAYERS_LIST = "players.lst";
static final String TEAMS_LIST = "teams.lst";
static final int TRANSACTION_PERIOD = 100;
static final int PAGE_POOL_SIZE = 64*1024;
private String[] readCSVLine(int nColumns, InputStream in) throws IOException
{
StringBuffer buf = new StringBuffer();
String[] row = new String[nColumns];
int ch;
int column = 0;
while ((ch = in.read()) != -1) {
if (ch == '\t' || ch == '\n' || ch == '\r') {
row[column++] = buf.toString();
buf.delete(0, buf.length());
if (ch == '\n') {
return row;
} else if (ch == '\r') {
in.read();
return row;
}
} else {
buf.append((char)ch);
}
}
return null;
}
void importData()
{
String[] row;
Gauge gauge = new Gauge("Building Database...", false, 10000, 0);
Form form = new Form("Please wait...", new Item[] {gauge});
Display.getDisplay(this).setCurrent(form);
Root root = (Root)db.getRoot();
try {
InputStream in = UniAR.class.getResourceAsStream("/"+TEAMS_LIST);
gauge.setLabel("Loading Teams...");
int nRows = 0;
int maxRowsToLoad = 162;
gauge.setMaxValue(maxRowsToLoad);
while ((row = readCSVLine(17, in)) != null) {
Team team = new Team(db, row);
root.teamId.put(new Key(team.id), team);
root.teamName.put(team.name.toLowerCase(), team);
gauge.setValue(++nRows);
if (nRows % TRANSACTION_PERIOD == 0) {
db.commit();
root = (Root)db.getRoot();
}
if (nRows == maxRowsToLoad) {
break;
}
}
in = UniAR.class.getResourceAsStream("/"+PLAYERS_LIST);
gauge.setLabel("Loading Players...");
nRows = 0;
maxRowsToLoad = 500;
gauge.setMaxValue(maxRowsToLoad);
while ((row = readCSVLine(13, in)) != null) {
int teamId = Integer.parseInt(row[1]);
Team team = (Team)root.teamId.get(new Key(teamId));
if (team == null) {
System.out.println("Failed to find team " + teamId);
} else {
Player player = new Player(db, row);
player.team = team;
root.playerLName.put(player.lname.toLowerCase(), player);
team.players.add(player);
}
gauge.setValue(++nRows);
if (nRows % TRANSACTION_PERIOD == 0) {
db.commit();
root = (Root)db.getRoot();
}
if (nRows == maxRowsToLoad) {
break;
}
}
db.commit();
} catch (Exception x) {
x.printStackTrace();
}
}
public void startApp()
{
db = StorageFactory.getInstance().createStorage();
db.open("uniar.dbs", PAGE_POOL_SIZE);
Root root = (Root)db.getRoot();
if (root == null) {
root = new Root(db);
db.setRoot(root);
importData();
}
menu = new List("Find", List.IMPLICIT, new String[]{"Player", "Team"}, null);
menu.addCommand(QUIT_CMD);
menu.setCommandListener(this);
Display.getDisplay(this).setCurrent(menu);
}
public void commandAction(Command c, Displayable d)
{
if (c == QUIT_CMD) {
quit();
} else {
switch (menu.getSelectedIndex()) {
case 0:
new PlayerSearchForm(this);
break;
case 1:
new TeamSearchForm(this);
break;
}
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
db.close();
}
void quit() {
destroyApp(true);
notifyDestroyed();
}
void message(String msg) {
Alert alert = new Alert("Warning",
msg,
null,
AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert, menu);
}
Storage db;
List menu;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -