mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -49,7 +49,7 @@ import java.io.PushbackInputStream;
|
||||
public class BOMInputStream extends InputStream {
|
||||
|
||||
private final PushbackInputStream in;
|
||||
private boolean isInited = false;
|
||||
private boolean initialized;
|
||||
private final String defaultCharset;
|
||||
private String charset;
|
||||
|
||||
@@ -92,7 +92,7 @@ public class BOMInputStream extends InputStream {
|
||||
* @return 编码
|
||||
*/
|
||||
public String getCharset() {
|
||||
if (!isInited) {
|
||||
if (!initialized) {
|
||||
try {
|
||||
init();
|
||||
} catch (final IOException ex) {
|
||||
@@ -104,13 +104,13 @@ public class BOMInputStream extends InputStream {
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
isInited = true;
|
||||
initialized = true;
|
||||
in.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
isInited = true;
|
||||
initialized = true;
|
||||
return in.read();
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ public class BOMInputStream extends InputStream {
|
||||
* @throws IOException 读取引起的异常
|
||||
*/
|
||||
protected void init() throws IOException {
|
||||
if (isInited) {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -146,6 +146,6 @@ public class BOMInputStream extends InputStream {
|
||||
in.unread(bom, (n - unread), unread);
|
||||
}
|
||||
|
||||
isInited = true;
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
@@ -57,6 +57,11 @@ public abstract class AtomicLoader<T> implements Loader<T>, Serializable {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitialized() {
|
||||
return null != reference.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化被加载的对象<br>
|
||||
* 如果对象从未被加载过,调用此方法初始化加载对象,此方法只被调用一次
|
||||
|
@@ -18,7 +18,6 @@ package org.dromara.hutool.core.lang.loader;
|
||||
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@@ -42,12 +41,13 @@ public class LazyFunLoader<T> extends LazyLoader<T> {
|
||||
|
||||
/**
|
||||
* 静态工厂方法,提供语义性与编码便利性
|
||||
*
|
||||
* @param supplier 用于生成对象的函数
|
||||
* @param <T> 对象类型
|
||||
* @param <T> 对象类型
|
||||
* @return 函数式懒加载加载器对象
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static <T> LazyFunLoader<T> on(final Supplier<T> supplier) {
|
||||
public static <T> LazyFunLoader<T> of(final Supplier<T> supplier) {
|
||||
Assert.notNull(supplier, "supplier must be not null!");
|
||||
return new LazyFunLoader<>(supplier);
|
||||
}
|
||||
@@ -68,27 +68,4 @@ public class LazyFunLoader<T> extends LazyLoader<T> {
|
||||
this.supplier = null;
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否已经初始化
|
||||
*
|
||||
* @return 是/否
|
||||
*/
|
||||
public boolean isInitialize() {
|
||||
return this.supplier == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果已经初始化,就执行传入函数
|
||||
*
|
||||
* @param consumer 待执行函数
|
||||
*/
|
||||
public void ifInitialized(final Consumer<T> consumer) {
|
||||
Assert.notNull(consumer);
|
||||
|
||||
// 已经初始化
|
||||
if (this.isInitialize()) {
|
||||
consumer.accept(this.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -51,6 +51,11 @@ public abstract class LazyLoader<T> implements Loader<T>, Serializable {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitialized() {
|
||||
return null != object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化被加载的对象<br>
|
||||
* 如果对象从未被加载过,调用此方法初始化加载对象,此方法只被调用一次
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.core.lang.loader;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 对象加载抽象接口<br>
|
||||
* 通过实现此接口自定义实现对象的加载方式,例如懒加载机制、多线程加载等
|
||||
@@ -34,4 +36,25 @@ public interface Loader<T> {
|
||||
* @return 加载完毕的对象
|
||||
*/
|
||||
T get();
|
||||
|
||||
/**
|
||||
* 是否已经初始化完毕
|
||||
*
|
||||
* @return 是否已经初始化完毕
|
||||
*/
|
||||
default boolean isInitialized() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果已经初始化,就执行传入函数
|
||||
*
|
||||
* @param consumer 待执行函数,为{@code null}表示不执行任何操作
|
||||
*/
|
||||
default void ifInitialized(final Consumer<T> consumer) {
|
||||
// 已经初始化
|
||||
if (null != consumer && this.isInitialized()) {
|
||||
consumer.accept(get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ public final class SensitiveUtil {
|
||||
/**
|
||||
* @return 是否已经被初始化
|
||||
*/
|
||||
public static boolean isInited() {
|
||||
public static boolean isInitialized() {
|
||||
return !sensitiveTree.isEmpty();
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.core.xml;
|
||||
|
||||
import org.dromara.hutool.core.lang.loader.LazyFunLoader;
|
||||
import org.dromara.hutool.core.lang.loader.Loader;
|
||||
|
||||
import javax.xml.parsers.SAXParserFactory;
|
||||
|
||||
/**
|
||||
@@ -29,7 +32,7 @@ public class SAXParserFactoryUtil {
|
||||
/**
|
||||
* Sax读取器工厂缓存
|
||||
*/
|
||||
private static volatile SAXParserFactory factory;
|
||||
private static final Loader<SAXParserFactory> factoryLoader = LazyFunLoader.of(()->createFactory(false, true));
|
||||
|
||||
/**
|
||||
* 获取全局{@link SAXParserFactory}<br>
|
||||
@@ -41,15 +44,7 @@ public class SAXParserFactoryUtil {
|
||||
* @return {@link SAXParserFactory}
|
||||
*/
|
||||
public static SAXParserFactory getFactory() {
|
||||
if (null == factory) {
|
||||
synchronized (SAXParserFactoryUtil.class) {
|
||||
if (null == factory) {
|
||||
factory = createFactory(false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return factory;
|
||||
return factoryLoader.get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -36,7 +36,7 @@ public class LazyFunLoaderTest {
|
||||
final LazyFunLoader<BigObject> loader = new LazyFunLoader<>(BigObject::new);
|
||||
|
||||
Assertions.assertNotNull(loader.get());
|
||||
Assertions.assertTrue(loader.isInitialize());
|
||||
Assertions.assertTrue(loader.isInitialized());
|
||||
|
||||
// 对于某些对象,在程序关闭时,需要进行销毁操作
|
||||
loader.ifInitialized(BigObject::destroy);
|
||||
@@ -56,16 +56,16 @@ public class LazyFunLoaderTest {
|
||||
it.destroy();
|
||||
});
|
||||
|
||||
Assertions.assertFalse(loader.isInitialize());
|
||||
Assertions.assertFalse(loader.isInitialized());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnLoadStaticFactoryMethod1() {
|
||||
public void testOfLoadStaticFactoryMethod1() {
|
||||
|
||||
final LazyFunLoader<BigObject> loader = LazyFunLoader.on(BigObject::new);
|
||||
final LazyFunLoader<BigObject> loader = LazyFunLoader.of(BigObject::new);
|
||||
|
||||
Assertions.assertNotNull(loader.get());
|
||||
Assertions.assertTrue(loader.isInitialize());
|
||||
Assertions.assertTrue(loader.isInitialized());
|
||||
|
||||
// 对于某些对象,在程序关闭时,需要进行销毁操作
|
||||
loader.ifInitialized(BigObject::destroy);
|
||||
@@ -74,9 +74,9 @@ public class LazyFunLoaderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnLoadStaticFactoryMethod2() {
|
||||
public void testOfLoadStaticFactoryMethod2() {
|
||||
|
||||
final LazyFunLoader<BigObject> loader = LazyFunLoader.on(BigObject::new);
|
||||
final LazyFunLoader<BigObject> loader = LazyFunLoader.of(BigObject::new);
|
||||
|
||||
// 若从未使用,则可以避免不必要的初始化
|
||||
loader.ifInitialized(it -> {
|
||||
|
Reference in New Issue
Block a user