findfilter.java

来自「J2ME程序设计实例教程的源码」· Java 代码 · 共 60 行

JAVA
60
字号
import java.io.IOException;
import javax.microedition.rms.*;

/**
 * 课程对象过滤器,用于设置查找条件
 */
class FindFilter implements RecordFilter {
    private String name = null; //课程名
    private int week = -1;      //上课日期
    private int jie = -1;       //上课时间
    private Curriculum curr;
    
    public FindFilter() {
        curr = new Curriculum(null);
    }
    
    //设置过滤条件
    public void setCondition(String name) {
        this.name = name;
        week = -1;
        jie = -1;
    }
    
    //设置过滤条件
    public void setCondition(int week) {
        this.week = week;
        name = null;
        jie = -1;
    }
    
    //设置过滤条件
    public void setCondition(int week, int jie) {
        this.week = week;
        this.jie = jie;
        name = null;
    }
    
    //接口RecordFilter中的方法,当记录存储获取记录的枚举时,
    //该接口方法不断将被回调。data为记录存储中的每一条记录。
    public boolean matches(byte[] data) {
        boolean suited = true;
        try {
            curr.setData(data);
            if(name != null && !name.equals(curr.getName())) {
                suited = false;
            }
            else if(week != -1 && jie == -1 && curr.getDay() != week) {
                suited = false;
            }
            else if(week != -1 && jie != -1 && (curr.getDay() != week || curr.getTime() != jie)) {
                suited = false;
            }
        }
        catch(IOException ioe) {
            suited = false;
        }
        
        return suited;
    }
}

⌨️ 快捷键说明

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