以上都是用来实现第一种返回matcher的方法。可以看到,这几种实现都是通过new Matcher实例, 以this(即Pattern实例)为参数,然后根据参数不同调用Matcher.setTarget()方法
public Matcher matcher(){
return new Matcher(this);
}
public Matcher matcher(String s){
Matcher m=new Matcher(this);
m.setTarget(s);
return m;
}
public Matcher matcher(char[] data,int start,int end){
Matcher m=new Matcher(this);
m.setTarget(data,start,end);
return m;
}
public Matcher matcher(MatchResult res,int groupId){
Matcher m=new Matcher(this);
if(res instanceof Matcher){
m.setTarget((Matcher)res,groupId);
}
else{
m.setTarget(res.targetChars(),res.start(groupId)+res.targetStart(),res.length(groupId));
}
return m;
}
public Matcher matcher(Reader text,int length)throws IOException{
Matcher m=new Matcher(this);
m.setTarget(text,length);
return m;
}
以上都是用来实现第一种返回matcher的方法。可以看到,这几种实现都是通过new Matcher实例, 以this(即Pattern实例)为参数,然后根据参数不同调用Matcher.setTarget()方法。
public Matcher matcher(MatchResult res,String groupName){
Integer id=res.pattern()。groupId(groupName);
if(id==null) throw new IllegalArgumentException("group not found:"+groupName);
int group=id.intValue();
return matcher(res,group);
}
这是第二种返回matcher的方法。
从这里我们可以发现一种比较好的方法:当你需要两个互相关联的类,一个类的实例需要构造另一 个类的实例,可以在第一个类中用一个public方法,方法实现为调用第二个类的构造函数,并可以 用构造出来的实例调用其他方法,根据第一个类的实例的数据来设置第二个类的实例。
同样的,也有Replacer, 和Tokenizer的构造方法。
public Replacer replacer(String expr){
return new Replacer(this,expr);
}
/**
* Returns a replacer will substitute all occurences of a pattern
* through applying a user-defined substitution model.
* @param model a Substitution object which is in charge for match substitution
* @see Replacer
*/
public Replacer replacer(Substitution model){
return new Replacer(this,model);
}
public RETokenizer tokenizer(String text){
return new RETokenizer(this,text);
}
/**
* Tokenizes a specified region by an occurences of the pattern.
* Note that a series of adjacent matches are regarded as a single separator.
* The same as new RETokenizer(Pattern,char[],int,int);
* @see RETokenizer
* @see RETokenizer#RETokenizer(jregex.Pattern,char[],int,int)
*/
public RETokenizer tokenizer(char[] data,int off,int len){
return new RETokenizer(this,data,off,len);
}
/**
* Tokenizes a specified region by an occurences of the pattern.
* Note that a series of adjacent matches are regarded as a single separator.
* The same as new RETokenizer(Pattern,Reader,int);
* @see RETokenizer
* @see RETokenizer#RETokenizer(jregex.Pattern,java.io.Reader,int)
*/
public RETokenizer tokenizer(Reader in,int length) throws IOException{
return new RETokenizer(this,in,length);
}
回忆一下,我在本文的开头,曾经提到过:"一个好的库必须是一个紧凑的关系紧密的整体,而不 是一个分散的关系松散的对象的集合。"从API说明文档所显示的这个库的树形结构,并不能看出这 些类之间的联系。而从源代码的角度,我们则可以清楚地看到这一点。在这一部分的讨论中,我们 也明白了两点:
1、如何编写重载构造函数
2、在一个类的实例中返回另外一个类的实例
接下来,看看Matcher类。这个类实现了MatchResult interface. 看看MatchResult的定义:
[games]$javap -classpath …… -s jregex.MatchResult
Compiled from jregex/MatchResult.java
public interface jregex.MatchResult
/* ACC_SUPER bit NOT set */
{
public static final int MATCH;
/* I */
public static final int PREFIX;
/* I */
public static final int SUFFIX;
/* I */
public static final int TARGET;
/* I */
public abstract jregex.Pattern pattern();
/* ()Ljregex/Pattern; */
public abstract int groupCount();
/* ()I */
public abstract boolean isCaptured();
/* ()Z */
public abstract boolean isCaptured(int);
/* (I)Z */
public abstract boolean isCaptured(java.lang.String);
/* (Ljava/lang/String;)Z */
public abstract java.lang.String group(int);
/* (I)Ljava/lang/String; */
public abstract boolean getGroup(int, java.lang.StringBuffer);
/* (ILjava/lang/StringBuffer;)Z */
public abstract boolean getGroup(int, jregex.TextBuffer);
/* (ILjregex/TextBuffer;)Z */
public abstract java.lang.String group(java.lang.String);
/* (Ljava/lang/String;)Ljava/lang/String; */
public abstract boolean getGroup(java.lang.String, java.lang.StringBuffer);
/* (Ljava/lang/String;Ljava/lang/StringBuffer;)Z */
public abstract boolean getGroup(java.lang.String, jregex.TextBuffer);
/* (Ljava/lang/String;Ljregex/TextBuffer;)Z */
public abstract java.lang.String prefix();
/* ()Ljava/lang/String; */
public abstract java.lang.String suffix();
/* ()Ljava/lang/String; */
public abstract java.lang.String target();
/* ()Ljava/lang/String; */
public abstract int targetStart();
/* ()I */
public abstract int targetEnd();
/* ()I */
public abstract char targetChars()[];
/* ()[C */
public abstract int start();
/* ()I */
public abstract int end();
/* ()I */
public abstract int length();
/* ()I */
public abstract int start(int);
/* (I)I */
public abstract int end(int);
/* (I)I */
public abstract int length(int);
/* (I)I */
public abstract char charAt(int);
/* (I)C */
public abstract char charAt(int, int);
/* (II)C */
}
jregex.MatchResult定义了一些abstract函数。有什么作用?在后面我们将会讨论到。
再看看Matcher的实现。
[games]$javap -classpath …… -s jregex.Matcher
Compiled from jregex/Matcher.java
public class jregex.Matcher extends java.lang.Object implements jregex.MatchResult {
public static final int ANCHOR_START;
/* I */
public static final int ANCHOR_LASTMATCH;
/* I */
public static final int ANCHOR_END;
/* I */
public static final int ACCEPT_INCOMPLETE;
查看本文来源