add methods

This commit is contained in:
Looly 2021-10-15 00:33:02 +08:00
parent 1b39b8271a
commit 172d457bea
4 changed files with 70 additions and 29 deletions

View File

@ -3,7 +3,7 @@ package cn.hutool.core.lang.mutable;
import java.io.Serializable; import java.io.Serializable;
/** /**
* 可变 <code>boolean</code> 类型 * 可变 {@code boolean} 类型
* *
* @see Boolean * @see Boolean
* @since 3.0.1 * @since 3.0.1
@ -59,12 +59,12 @@ public class MutableBool implements Comparable<MutableBool>, Mutable<Boolean>, S
* 相等需同时满足如下条件 * 相等需同时满足如下条件
* <ol> * <ol>
* <li>非空</li> * <li>非空</li>
* <li>类型为 {@link MutableBool}</li> * <li>类型为 MutableBool</li>
* <li>值相等</li> * <li>值相等</li>
* </ol> * </ol>
* *
* @param obj 比对的对象 * @param obj 比对的对象
* @return 相同返回<code>true</code>否则 <code>false</code> * @return 相同返回<code>true</code>否则 {@code false}
*/ */
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
@ -83,7 +83,7 @@ public class MutableBool implements Comparable<MutableBool>, Mutable<Boolean>, S
/** /**
* 比较 * 比较
* *
* @param other 其它 {@link MutableBool} 对象 * @param other 其它 MutableBool 对象
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
*/ */
@Override @Override

View File

@ -3,7 +3,7 @@ package cn.hutool.core.lang.mutable;
import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.NumberUtil;
/** /**
* 可变 <code>byte</code> 类型 * 可变 {@code byte} 类型
* *
* @see Byte * @see Byte
* @since 3.0.1 * @since 3.0.1
@ -157,12 +157,12 @@ public class MutableByte extends Number implements Comparable<MutableByte>, Muta
* 相等需同时满足如下条件 * 相等需同时满足如下条件
* <ol> * <ol>
* <li>非空</li> * <li>非空</li>
* <li>类型为 {@link MutableByte}</li> * <li>类型为 MutableByte</li>
* <li>值相等</li> * <li>值相等</li>
* </ol> * </ol>
* *
* @param obj 比对的对象 * @param obj 比对的对象
* @return 相同返回<code>true</code>否则 <code>false</code> * @return 相同返回<code>true</code>否则 {@code false}
*/ */
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
@ -181,7 +181,7 @@ public class MutableByte extends Number implements Comparable<MutableByte>, Muta
/** /**
* 比较 * 比较
* *
* @param other 其它 {@link MutableByte} 对象 * @param other 其它 MutableByte 对象
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
*/ */
@Override @Override

View File

@ -3,7 +3,7 @@ package cn.hutool.core.lang.mutable;
import java.io.Serializable; import java.io.Serializable;
/** /**
* 可变<code>Object</code> * 可变{@code Object}
* *
* @param <T> 可变的类型 * @param <T> 可变的类型
* @since 3.0.1 * @since 3.0.1

View File

@ -3,11 +3,13 @@ package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Holder; import cn.hutool.core.lang.Holder;
import cn.hutool.core.lang.PatternPool; import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.RegexPool; import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.lang.Validator; import cn.hutool.core.lang.Validator;
import cn.hutool.core.lang.func.Func1; import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.mutable.MutableObj;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -17,6 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.regex.MatchResult; import java.util.regex.MatchResult;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -138,11 +141,9 @@ public class ReUtil {
return null; return null;
} }
final Matcher matcher = pattern.matcher(content); final MutableObj<String> result = new MutableObj<>();
if (matcher.find()) { get(pattern, content, matcher -> result.set(matcher.group(groupIndex)));
return matcher.group(groupIndex); return result.get();
}
return null;
} }
/** /**
@ -158,11 +159,29 @@ public class ReUtil {
if (null == content || null == pattern || null == groupName) { if (null == content || null == pattern || null == groupName) {
return null; return null;
} }
final MutableObj<String> result = new MutableObj<>();
get(pattern, content, matcher -> result.set(matcher.group(groupName)));
return result.get();
}
/**
* 在给定字符串中查找给定规则的字符如果找到则使用{@link Consumer}处理之<br>
* 如果内容中有多个匹配项则只处理找到的第一个结果
*
* @param pattern 匹配的正则
* @param content 被匹配的内容
* @param consumer 匹配到的内容处理器
* @since 5.7.15
*/
public static void get(Pattern pattern, CharSequence content, Consumer<Matcher> consumer) {
if (null == content || null == pattern || null == consumer) {
return;
}
final Matcher m = pattern.matcher(content); final Matcher m = pattern.matcher(content);
if (m.find()) { if (m.find()) {
return m.group(groupName); consumer.accept(m);
} }
return null;
} }
/** /**
@ -225,7 +244,7 @@ public class ReUtil {
if (m.find()) { if (m.find()) {
// 通过反射获取 namedGroups 方法 // 通过反射获取 namedGroups 方法
final Map<String, Integer> map = ReflectUtil.invoke(pattern, "namedGroups"); final Map<String, Integer> map = ReflectUtil.invoke(pattern, "namedGroups");
map.forEach((key, value)-> result.put(key, m.group(value))); map.forEach((key, value) -> result.put(key, m.group(value)));
} }
return result; return result;
} }
@ -278,7 +297,6 @@ public class ReUtil {
return null; return null;
} }
// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return extractMulti(pattern, content, template); return extractMulti(pattern, content, template);
} }
@ -329,7 +347,6 @@ public class ReUtil {
return null; return null;
} }
// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return extractMultiAndDelPre(pattern, contentHolder, template); return extractMultiAndDelPre(pattern, contentHolder, template);
} }
@ -346,7 +363,6 @@ public class ReUtil {
return StrUtil.str(content); return StrUtil.str(content);
} }
// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return delFirst(pattern, content); return delFirst(pattern, content);
} }
@ -427,7 +443,6 @@ public class ReUtil {
return StrUtil.str(content); return StrUtil.str(content);
} }
// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return delAll(pattern, content); return delAll(pattern, content);
} }
@ -459,9 +474,23 @@ public class ReUtil {
return StrUtil.str(content); return StrUtil.str(content);
} }
// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(content); return delPre(pattern, content);
}
/**
* 删除正则匹配到的内容之前的字符 如果没有找到则返回原文
*
* @param pattern 定位正则模式
* @param content 被查找的内容
* @return 删除前缀后的新内容
*/
public static String delPre(Pattern pattern, CharSequence content) {
if (null == content || null == pattern) {
return StrUtil.str(content);
}
final Matcher matcher = pattern.matcher(content);
if (matcher.find()) { if (matcher.find()) {
return StrUtil.sub(content, matcher.end(), content.length()); return StrUtil.sub(content, matcher.end(), content.length());
} }
@ -520,7 +549,7 @@ public class ReUtil {
return collection; return collection;
} }
return findAll(Pattern.compile(regex, Pattern.DOTALL), content, group, collection); return findAll(PatternPool.get(regex, Pattern.DOTALL), content, group, collection);
} }
/** /**
@ -574,16 +603,29 @@ public class ReUtil {
if (null == pattern || null == content) { if (null == pattern || null == content) {
return null; return null;
} }
Assert.notNull(collection, "Collection must be not null !");
if (null == collection) { findAll(pattern, content, (matcher) -> collection.add(matcher.group(group)));
throw new NullPointerException("Null collection param provided!"); return collection;
}
/**
* 取得内容中匹配的所有结果使用{@link Consumer}完成匹配结果处理
*
* @param pattern 编译后的正则模式
* @param content 被查找的内容
* @param consumer 匹配结果处理函数
* @since 5.7.15
*/
public static void findAll(Pattern pattern, CharSequence content, Consumer<Matcher> consumer) {
if (null == pattern || null == content) {
return;
} }
final Matcher matcher = pattern.matcher(content); final Matcher matcher = pattern.matcher(content);
while (matcher.find()) { while (matcher.find()) {
collection.add(matcher.group(group)); consumer.accept(matcher);
} }
return collection;
} }
/** /**
@ -598,7 +640,6 @@ public class ReUtil {
return 0; return 0;
} }
// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
return count(pattern, content); return count(pattern, content);
} }