接口:
1.supplier定义了接口,接口只定义了一个方法get,用以返回实例
public interface Supplier<T> {
/*** Retrieves an instance of the appropriate type. The returned object may or
* may not be a new instance, depending on the implementation.
*返回的可能是新的instance,或者不是。这个根据具体的实现。
* @return an instance of the appropriate type
*/
T get();
}
2.preditate 定义了接口Returns the result of applying this predicate to input
.
public interface Predicate<T> {
boolean apply(@Nullable T input);
@Override
boolean equals(@Nullable Object object);
}
3.Function<F,T>定义了接口
public interface Function<F, T> {
@Nullable T apply(@Nullable F input);
@Override
boolean equals(@Nullable Object object);
}
4.FinalizableReference
public interface FinalizableReference {
void finalizeReferent();
}
类:
1.final class Ascii,定义了private 构造函数,不能创建新对象。
定义了1-32,0,127
以及一些静态方法:
public static boolean isUpperCase(char c) {
return (c >= 'A') && (c <= 'Z');
}
public static boolean isLowerCase(char c) {
// Note: This was benchmarked against the alternate expression "(char)(c - 'a') < 26" (Nov '13)
// and found to perform at least as well, or better.
return (c >= 'a') && (c <= 'z');
}
//通过与运算,可将讲lowercase 字符转换成大写字符
public static char toUpperCase(char c) {
return isLowerCase(c) ? (char) (c & 0x5f) : c;
}
public static char toLowerCase(char c) {
return isUpperCase(c) ? (char) (c ^ 0x20) : c;
}
//定义了讲chars ,string进行upcase与lowercase的方法 CharSequence是String实现的一个接口,相当简单,
//就是要求是一串字符。所以每个参数类型是/CharSequence的方法,都可以实际代入String对象。
// 为什么这些方法不干脆定义String作为参数类型?因为还有其他的CharSequence类型的类,
//比如StringBuffer和StringBuilder这两个很重要的类。 String对象是不可变的,这两个可变,所以我们在构造
//字符串的过程中往往要用到StringBuffer和StringBuilder。如果那些方法定义String作为参数类型,那么就没法对
//它们用那些方法,先得转化成String才能用。但StringBuffer和StringBuilder转换为String再转换过来很化时间的,
//用它们而不是直接用String的“加法”来构造新String本来就是为了省时间,所以如果用String作为参数类型就杯具了。
/*只写一种转换*/
public static String toLowerCase(CharSequence chars) {
if (chars instanceof String) {//判断是否是String类型
return toLowerCase((String) chars);
}
int length = chars.length();
//新建StringBuilder,并放入参数
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
builder.append(toLowerCase(chars.charAt(i)));
}
return builder.toString();
}
//返回字母的顺序。
private static int getAlphaIndex(char c) {
// Fold upper-case ASCII to lower-case and
// make zero-indexed and unsigned (by casting to char).
return (char) ((c | 0x20) - 'a');
}
//判断两个字母串,忽略大小写是否相等
public static boolean equalsIgnoreCase(CharSequence S1,CharSequence S2){
if(S1 == S2){
return true;
}
if(S1.length() != S2.length()){
return false;
}
int len = S1.length();
for(int i = 0;i < len;i++){
char c1 = S1.charAt(i);
char c2 = S2.charAt(i);
if(c1 == c2){
continue;
}
int n1 = getAlphaIndex(c1);
int n2 = getAlphaIndex(c2);
if(n1 < 26 && n1 == n2){
continue;
}else{
return false;
}
}
return true;
}
//还有字符代换的函数
@Beta
@CheckReturnValue
public static String truncate(CharSequence seq, int maxLength, String truncationIndicator)
2:charmatcher,其实现Predicate<Character>接口,要重写
public boolean apply(Character character) {
return matches(character);
}
返回第一个匹配的下标:
public int indexIn(CharSequence sequence) {
int length = sequence.length();
for (int i = 0; i < length; i++) {
if (matches(sequence.charAt(i))) {
return i;
}
}
return -1;
}
包含起始位置的:
public int indexIn(CharSequence sequence, int start) {
int length = sequence.length();
Preconditions.checkPositionIndex(start, length);
for (int i = start; i < length; i++) {
if (matches(sequence.charAt(i))) {
return i;
}
}
return -1;
}
可以使用的方法,如下图:
以及用marcher进行两个charsequence替换代码:
public String replaceFrom(CharSequence sequence, CharSequence replacement) {
int replacementLen = replacement.length();
if (replacementLen == 0) {
return removeFrom(sequence);
}
if (replacementLen == 1) {
return replaceFrom(sequence, replacement.charAt(0));
}
String string = sequence.toString();
int pos = indexIn(string);
if (pos == -1) {
return string;
}
int len = string.length();
StringBuilder buf = new StringBuilder((len * 3 / 2) + 16);//?
int oldpos = 0;
do {
buf.append(string, oldpos, pos);
buf.append(replacement);
oldpos = pos + 1;
pos = indexIn(string, oldpos);
} while (pos != -1);
buf.append(string, oldpos, len);
return buf.toString();
}
源码中构建了一些常用的macher:
和一些构造方法:
以anyOf为例:
public static CharMatcher anyOf(final CharSequence sequence) {
switch (sequence.length()) {//判断charsequence长度,以避免重复代码。
<span style="white-space:pre"> </span>假如长度大于2,新建字符数组,先进行排序。重写了matches方法(通过Arrays的二分法查找判断结果是不是大于0来判断);
case 0:
return NONE;
case 1:
return is(sequence.charAt(0));
case 2:
return isEither(sequence.charAt(0), sequence.charAt(1));
default:
// continue below to handle the general case
}
// TODO(user): is it potentially worth just going ahead and building a precomputed matcher?
final char[] chars = sequence.toString().toCharArray();
Arrays.sort(chars);
StringBuilder description = new StringBuilder("CharMatcher.anyOf(\"");
for (char c : chars) {
description.append(showCharacter(c));
}
description.append("\")");
return new CharMatcher(description.toString()) {
@Override public boolean matches(char c) {
return Arrays.binarySearch(chars, c) >= 0;
}
@Override
@GwtIncompatible("java.util.BitSet")
void setBits(BitSet table) {
for (char c : chars) {
table.set(c);
}
}
};
}