/**
* Replaces each substring of this string that matches the literal target
* sequence with the specified literal replacement sequence. The
* replacement proceeds from the beginning of the string to the end, for
* example, replacing "aa" with "b" in the string "aaa" will result in
* "ba" rather than "ab".
* 翻译:
* 1.将字符串里匹配目标串的子串替换为指定的代替序列
* 2.替换过程从头到尾进行
* @param target The sequence of char values to be replaced
* @param replacement The replacement sequence of char values
* @return The resulting string
* @since 1.5
*/
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
replaceAll()
/**
* Replaces each substring of this string that matches the given <a
* href="../util/regex/Pattern.html#sum">regular expression</a> with the
* given replacement.
*
* 翻译:
* 1.将字符串中匹配正则表达式的子串替换为给定的序列
* <p> An invocation of this method of the form
* <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
* yields exactly the same result as the expression
*
* <blockquote>
* <code>
* {@link java.util.regex.Pattern}.{@link
* java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
* java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
* java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>)
* </code>
* </blockquote>
*
*<p>
* Note that backslashes ({@code \}) and dollar signs ({@code $}) in the
* replacement string may cause the results to be different than if it were
* being treated as a literal replacement string; see
* {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
* Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
* meaning of these characters, if desired.
*
* @param regex
* the regular expression to which this string is to be matched
* @param replacement
* the string to be substituted for each match
*
* @return The resulting {@code String}
*
* @throws PatternSyntaxException
* if the regular expression's syntax is invalid
*
* @see java.util.regex.Pattern
*
* @since 1.4
* @spec JSR-51
*/
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
首先分析replaceAll()执行的流程
(1)Pattern.compile(regex)生成一个正则表达式的编译表示Pattern
(2)Pattern对象调用matcher(this)得到一个Matcher对象
(3)Matcher对象replaceAll(replacement)返回一个字符串对象
这个方法的时间复杂度和空间复杂度体现在哪儿呢?
(1)生成正则表达式对象时
public static Pattern compile(String regex) {
return new Pattern(regex, 0);
}
/**
* This private constructor is used to create all Patterns. The pattern
* string and match flags are all that is needed to completely describe
* a Pattern. An empty pattern string results in an object tree with
* only a Start node and a LastNode node.
* 翻译:
* (1)这个私有的构造器是用于创建所有的Patterns
* (2)pattern string 和 match flags用于描述一个Pattern对象
* (3)一个空的Pattern string 导致创建一个只有开始节点和结束节点的对象树
*
*/
private Pattern(String p, int f) {
pattern = p;
flags = f;
// to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
if ((flags & UNICODE_CHARACTER_CLASS) != 0)
flags |= UNICODE_CASE;
// Reset group index count
capturingGroupCount = 1;
localCount = 0;
if (pattern.length() > 0) {
//调用这个方法
compile();
} else {
root = new Start(lastAccept);
matchRoot = lastAccept;
}
}
/**
* Copies regular expression to an int array and invokes the parsing
* of the expression which will create the object tree.
* 翻译:
* (1)将正则表达式复制为一个int类型的数组
* (2)将正则表达式复制到一个int数组,并调用将创建对象树的表达式的解析。
*/
private void compile() {
//此处省略n行生成树结构的核心代码
}
/**
* Creates a matcher that will match the given input against this pattern.
* 翻译:
* (1)创建一个将input和pattern进行匹配的Matcher对象
* @param input
* The character sequence to be matched
*
* @return A new matcher for this pattern
*/
public Matcher matcher(CharSequence input) {
if (!compiled) {
synchronized(this) {
if (!compiled)
compile();
}
}
Matcher m = new Matcher(this, input);
return m;
}
(3)Matcher对象调用自身的replaceAll()方法
public String replaceAll(String replacement) {
reset();
boolean result = find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
appendReplacement(sb, replacement);
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
/**
* Waits for this thread to die.
* 翻译:等待当前线程执行结束
* <p> An invocation of this method behaves in exactly the same
* way as the invocation
*
* <blockquote>
* {@linkplain #join(long) join}{@code (0)}
* </blockquote>
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final void join() throws InterruptedException {
//实际调用了join(int millis)
join(0);
}
/**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
* 翻译:
* 1.最多等待millis线程结束
* 2.0 意味着永远等待
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
//一进入这个方法就设置一个基础的时间标
long base = System.currentTimeMillis();
//当前时间距base的时间间隔
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
// public final native boolean isAlive();
//调用了本地方法,判断当前线程是否存活
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
//delay<0说明已经超过了预设的时间
if (delay <= 0) {
break;
}
wait(delay);
//还需要求一次now,用作下一次循环的判断
now = System.currentTimeMillis() - base;
}
}
}
/*
* Causes the current thread to wait until either another thread invokes the
* {@link java.lang.Object#notify()} method or the
* {@link java.lang.Object#notifyAll()} method for this object, or a
* specified amount of time has elapsed.
* 翻译:
* 使得当前线程等待,直到其他线程调用notify()或者notifyAll()唤醒当前线程,或者达到了预设的时间
*
* 线程以三种方法成为对象监视器的所有者:
* 1.调用改对象的同步方法
* 2.执行该对象上的synchronized修饰的语句块
* 3.对于类型为class的对象,执行该类的同步方法
*
*/
public final native void wait(long timeout) throws InterruptedException;
/**
* Wakes up a single thread that is waiting on this object's
* monitor. If any threads are waiting on this object, one of them
* is chosen to be awakened. The choice is arbitrary and occurs at
* the discretion of the implementation. A thread waits on an object's
* monitor by calling one of the {@code wait} methods.
*/
notify
/**
* Causes the current thread to wait until either another thread invokes the
* {@link java.lang.Object#notify()} method or the
* {@link java.lang.Object#notifyAll()} method for this object, or a
* specified amount of time has elapsed.
* <p>
* The current thread must own this object's monitor.
* <p>
*/
wait