📄 phonebook.java
字号:
//
// Copyright 2002 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at any time,
// without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation. Java and all Java-based marks are trademarks or registered
// trademarks of Sun Microsystems, Inc. Other product and company names
// mentioned herein may be trademarks or trade names of their respective owners.
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
//
import javax.microedition.rms.*;
import java.util.*;
class PhoneBook
{
private final static String RSNAME = "phonebook";
private final static char DELIMITER = '|';
private static PhoneBook phoneBook;
private static Vector names = new Vector();
private static Vector phones = new Vector();
static PhoneBook getPhoneBook()
{
if (phoneBook == null)
{
phoneBook = new PhoneBook();
}
return phoneBook;
}
private PhoneBook()
{
try
{
RecordStore rs = RecordStore.openRecordStore(RSNAME, true);
int peerNum = rs.getNumRecords();
for (int i = 0; i < peerNum; i++)
{
byte[] byteRec = rs.getRecord(i + 1);
String peer = new String(byteRec);
names.addElement(peer.substring(0, peer.indexOf(DELIMITER)));
phones.addElement(peer.substring(peer.indexOf(DELIMITER) + 1));
}
rs.closeRecordStore();
}
catch (RecordStoreException e)
{
ErrorScreen.showError("Open RMS Error");
}
}
synchronized String getName(String phoneNumber)
{
int index = phones.indexOf(phoneNumber);
return (index != -1) ? (String) names.elementAt(index) : null;
}
synchronized String getPhoneNumber(String name)
{
int index = names.indexOf(name);
return (index != -1) ? (String) phones.elementAt(index) : null;
}
synchronized String[] getNames()
{
String[] names = null;
if (this.names.size() > 0)
{
names = new String[this.names.size()];
this.names.copyInto(names);
}
return names;
}
synchronized String[] getNames(String[] phones)
{
String[] names = new String[phones.length];
for (int i = 0; i < names.length; i++)
{
names[i] = getName(phones[i]);
}
return names;
}
synchronized String validate(String peerName, String peerAddress)
{
int i;
String name = null;
String peerNum = (peerAddress.indexOf(':') == -1) ?
peerAddress :
peerAddress.substring(0, peerAddress.indexOf(':'));
for (i = 0; i < phones.size(); i++)
{
String phoneNumber = (String) phones.elementAt(i);
String phoneNum = (phoneNumber.indexOf(':') == -1) ?
phoneNumber :
phoneNumber.substring(0, phoneNumber.indexOf(':'));
// Only simple string-based matching of the phone numbers is
// performed below. More advanced matching based on the 'number
// structure' or 'numbering plan' of phone numbers is not
// performed. Specifically, we do not check if a phone number
// string using global number structure (e.g. "+3585012345"),
// matches a previously saved phone number that used a local
// structure within some geographic area (e.g. "05012345"
// dialed within Finland).
if (peerNum.equals(phoneNum))
{
name = (String) names.elementAt(i);
update(name, peerAddress);
break;
}
}
if (i == phones.size())
{
// new peer
for (i = 0; i < names.size(); i++)
{
name = (String) names.elementAt(i);
if (peerName.toLowerCase().equals(name.toLowerCase()))
{ // name exists, use name' instead
name = peerName + '\'';
update(name, peerAddress);
break;
}
}
if (i == names.size())
{
// brand new, save to rms
update(peerName, peerAddress);
name = peerName;
}
}
return name;
}
synchronized void update(String peerName, String peerAddress)
{
String peername = peerName.toLowerCase();
int i;
for (i = 0; i < names.size(); i++)
{
String name = ((String) names.elementAt(i)).toLowerCase();
String phoneNumber = (String) phones.elementAt(i);
if (peername.equals(name) || peerAddress.equals(phoneNumber))
{
if (peername.equals(name) && peerAddress.equals(phoneNumber))
{
return;
}
names.setElementAt(peerName, i);
phones.setElementAt(peerAddress, i);
break;
}
}
if (i == names.size())
{
names.insertElementAt(peerName, 0);
phones.insertElementAt(peerAddress, 0);
}
updateRecordStore();
}
synchronized void removeEntries(String[] peerNames)
{
for (int i = 0; i < peerNames.length; i++)
{
int index = names.indexOf(peerNames[i]);
names.removeElementAt(index);
phones.removeElementAt(index);
}
updateRecordStore();
}
synchronized void updateRecordStore()
{
try
{
RecordStore.deleteRecordStore(RSNAME);
RecordStore rs = RecordStore.openRecordStore(RSNAME, true);
for (int i = 0; i < names.size(); i++)
{
String peer = (String) names.elementAt(i) + DELIMITER +
(String) phones.elementAt(i);
rs.addRecord(peer.getBytes(), 0, peer.length());
}
rs.closeRecordStore();
}
catch (RecordStoreException e)
{
ErrorScreen.showError("Update RMS Error!");
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -