⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.java

📁 基于j2me的电话本基于j2me的电话本
💻 JAVA
字号:
package com.hziee.phone_book.model;

import java.io.IOException;
import javax.microedition.rms.*;
import com.hziee.phone_book.ui.*;
//这是一个数据模型类
public class Model {
	//RMS该类定义了对数据的操作,将RS中的ID和关键字记录再索引中
	 private RecordStore rs = null;//2个restore对象
	 private RecordStore rs_index = null;

	    private static class AccountFilter implements RecordFilter
	    {//AccountFilter 将数据传递给matches方法 
	        private String userName;

	        public AccountFilter(String userName)
	        {
	            this.userName = userName;
	        }
            
	        public boolean matches(byte[] data)
	        {
	            try
	            {
	            //调用Account.matches  将从RS中读出的username字节流转化为输入流
	              return Account.matches(data, userName);
	            } catch (IOException e)
	            {
	                e.printStackTrace();
	                return false;
	            }
	        }
	    }

	    private static class IndexFilter implements RecordFilter
	    {
	        private String key;
	        private int type = 101;
	        public static final int EQUALS = 101;
	        public static final int STARTWITH = 102;
            //传递数据key,type
	        public IndexFilter(String key)
	        {
	            this.key = key;
	        }
	        
	        public IndexFilter(String key,int type)
	        {
	            this.key = key;
	            this.type = type;
	        }

	        public boolean matches(byte[] data)
	        {
	            try
	            {//调用Index.matches读取key,type 
	                return Index.matches(data, key,type);
	            } catch (IOException e)
	            {
	                e.printStackTrace();
	                return false;
	            }
	        }
	    }

	    public Model() throws ApplicationException
	    {
	        try
	        {//通讯录,索引
	            rs = RecordStore.openRecordStore(Title.rsName, true);
	            rs_index = RecordStore.openRecordStore(Title.rs_index, true);

	        } catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        }
	    } 
       //重新打开RS
	    public void reOpenRecordStore() throws ApplicationException
	    {
	        try
	        {
	            rs = RecordStore.openRecordStore(Title.rsName, true);
	            rs_index = RecordStore.openRecordStore(Title.rs_index, true);
	        } catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        }
	    }
        
	    //添加索引
	    public void addIndex(Index index) throws ApplicationException
	    {
	        try
	        {
	            byte[] index_data = index.serialize();//转化字节流
	            if (rs_index.getNumRecords() > 0)//索引记录数目 
	            {
//	            	将rs_index的数据存到缓存(Hashtable)来提高效率
	                RecordEnumeration records = rs_index.enumerateRecords(
	                        new IndexFilter(index.getKey()), null, false);
	                if (records.hasNextElement())//after与最后以条记录时为False
	                {
	                    rs_index.setRecord(records.nextRecordId(), index_data, 0,
	                            index_data.length);

	                } else
	                {
	                    rs_index.addRecord(index_data, 0, index_data.length);
	                }
	            } else
	            {
	                rs_index.addRecord(index_data, 0, index_data.length);
	            }
	        } catch (IOException e)
	        {
	            throw new ApplicationException(e);
	        } catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        }
	    }

	    public Index[] searchIndex(String key) throws ApplicationException
	    {
	        try
	        {
	            if (rs_index.getNumRecords() > 0)
	            {
	                RecordEnumeration records = rs_index.enumerateRecords(
	                        new IndexFilter(key,102), null, false);
	                int length = records.numRecords();
	                if (length == 0)
	                {
	                    return new Index[0];
	                } else
	                {
	                    Index[] index = new Index[length];
	                    for (int i = 0; i < length; i++)
	                    {
	                       index[i] = Index.deserialize(rs_index.getRecord(records
	                                .nextRecordId()));
	                    }
	                    return index;
	                }

	            } else
	            {
	                return new Index[0];
	            }
	        } catch (RecordStoreException e)
	        {
	            e.printStackTrace();
	            return new Index[0];
	        }
	        catch(IOException e)
	        {
	            e.printStackTrace();
	            return new Index[0];
	        }
	    }
//获得索引值
	    public Index getIndex(String key) throws ApplicationException
	    {
	        try
	        {
	            if (rs_index.getNumRecords() > 0)
	            {
	                RecordEnumeration records = rs_index.enumerateRecords(
	                        new AccountFilter(key), null, false);

	                Index index = null;
	                if (records.numRecords() == 1)
	                {
	                    index = Index.deserialize(rs_index.getRecord(records
	                            .nextRecordId()));

	                }
	                return index;

	            } else
	            {
	                return null;
	            }
	        } catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        } catch (IOException e)
	        {
	            throw new ApplicationException(e);
	        }
	    }

	    public void deleteIndex(String key) throws ApplicationException
	    {
	        try
	        {
	            if (rs_index.getNumRecords() > 0)
	            {
	                RecordEnumeration records = rs_index.enumerateRecords(
	                        new IndexFilter(key), null, false);
	                while (records.hasNextElement())
	                {
	                    rs_index.deleteRecord(records.nextRecordId());
	                }
	            }
	            return;
	        } catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        }
	    }

	    public boolean isRecordExist(String userName) throws ApplicationException
	    {
	        try
	        {
	            if (rs_index.getNumRecords() > 0)
	            {//搜索username
	                RecordEnumeration records = rs_index.enumerateRecords(
	                        new IndexFilter(userName), null, false);
	                if (records.numRecords() > 0)
	                {
	                    return true;
	                } else
	                {
	                    return false;
	                }
	            } else
	            {
	                return false;
	            }
	        } catch (RecordStoreException e)
	        {
	            e.printStackTrace();
	            return true;
	        }
	    }

	    public void addRecord(Account account) throws ApplicationException
	    {
	        try
	        {
	            byte[] data = account.serialize();
	            int id = rs.addRecord(data, 0, data.length);//存储Account对象
	            Index index = new Index(account.getUserName(), id);
	            addIndex(index);
	        } catch (IOException e)
	        {

	        } catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        }

	    }

	     public void deleteRecord(int recordID) throws ApplicationException
	    {

	    }

	    public void deleteRecord(String userName) throws ApplicationException
	    {
	        try
	        {
	            Index index = getIndex(userName);
	            if (index != null)
	            {
	                int recordID = index.getRecordID();
	                System.out.println(recordID);
	                rs.deleteRecord(recordID);
	                deleteIndex(userName);
	            }
	        } catch (RecordStoreException e)
	        {

	            throw new ApplicationException(e);
	        }

	    }

	    public void clearAllRecord() throws ApplicationException
	    {
	        try
	        {
	            rs.closeRecordStore();
	            rs_index.closeRecordStore();
	            RecordStore.deleteRecordStore(Title.rsName);
	            RecordStore.deleteRecordStore(Title.rs_index);
	            this.reOpenRecordStore();
	        }

	        catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        }
	    }

	    public Account getAccount(String userName) throws ApplicationException
	    {
	        try
	        {
	            Index index = getIndex(userName);
	            Account account = null;
	            if (index != null)
	            {
	                account = Account
	                        .deserialize(rs.getRecord(index.getRecordID()));
	            }
	            return account;
	        }

	        catch (RecordStoreException e)
	        {
	            throw new ApplicationException(e);
	        } catch (IOException e)
	        {
	            throw new ApplicationException(e);
	        }
	    }

	    public Index[] listRecord() throws ApplicationException
	    {
	        try
	        {
	            RecordEnumeration enum2 = rs_index
	                    .enumerateRecords(null, null, true);
	            byte[] data = new byte[20];
	            int length = -1;
	            int i = 0;
	            int id = 0;
	            Index[] index = new Index[rs_index.getNumRecords()];
	            while (enum2.hasNextElement())
	            {
	                id = enum2.nextRecordId();
	                length = rs_index.getRecordSize(id);
	                if (length > data.length)
	                {
	                    data = new byte[length + 10];
	                }
	                rs_index.getRecord(id, data, 0);
	                index[i] = Index.deserialize(data);
	                i++;

	            }
	            return index;
	        } catch (IOException e)
	        {
	            e.printStackTrace();
	            return new Index[0];
	        } catch (RecordStoreException e)
	        {
	            e.printStackTrace();
	            return new Index[0];
	        }
	    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -