mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.lang.ref;
|
||||
|
||||
import java.lang.ref.PhantomReference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
|
||||
/**
|
||||
* 引用类型
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public enum ReferenceType {
|
||||
/**
|
||||
* 软引用,在GC报告内存不足时会被GC回收
|
||||
*/
|
||||
SOFT,
|
||||
/**
|
||||
* 弱引用,在GC时发现弱引用会回收其对象
|
||||
*/
|
||||
WEAK,
|
||||
/**
|
||||
* 虚引用,在GC时发现虚引用对象,会将{@link PhantomReference}插入{@link ReferenceQueue}。 <br>
|
||||
* 此时对象未被真正回收,要等到{@link ReferenceQueue}被真正处理后才会被回收。
|
||||
*/
|
||||
PHANTOM
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Copyright (c) 2023-2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
@@ -10,7 +10,9 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.util;
|
||||
package org.dromara.hutool.core.lang.ref;
|
||||
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.lang.ref.PhantomReference;
|
||||
import java.lang.ref.Reference;
|
||||
@@ -77,26 +79,4 @@ public class ReferenceUtil {
|
||||
public static <T> T get(final Reference<T> obj) {
|
||||
return ObjUtil.apply(obj, Reference::get);
|
||||
}
|
||||
|
||||
/**
|
||||
* 引用类型
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public enum ReferenceType {
|
||||
/**
|
||||
* 软引用,在GC报告内存不足时会被GC回收
|
||||
*/
|
||||
SOFT,
|
||||
/**
|
||||
* 弱引用,在GC时发现弱引用会回收其对象
|
||||
*/
|
||||
WEAK,
|
||||
/**
|
||||
* 虚引用,在GC时发现虚引用对象,会将{@link PhantomReference}插入{@link ReferenceQueue}。 <br>
|
||||
* 此时对象未被真正回收,要等到{@link ReferenceQueue}被真正处理后才会被回收。
|
||||
*/
|
||||
PHANTOM
|
||||
}
|
||||
|
||||
}
|
@@ -10,15 +10,16 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.map.reference;
|
||||
package org.dromara.hutool.core.lang.ref;
|
||||
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 弱键
|
||||
* 软引用对象,在GC报告内存不足时会被GC回收
|
||||
*
|
||||
* @param <T> 键类型
|
||||
*/
|
||||
@@ -28,12 +29,12 @@ public class SoftObj<T> extends SoftReference<T> {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param key 原始Key,不能为{@code null}
|
||||
* @param obj 原始对象
|
||||
* @param queue {@link ReferenceQueue}
|
||||
*/
|
||||
public SoftObj(final T key, final ReferenceQueue<? super T> queue) {
|
||||
super(key, queue);
|
||||
hashCode = key.hashCode();
|
||||
public SoftObj(final T obj, final ReferenceQueue<? super T> queue) {
|
||||
super(obj, queue);
|
||||
hashCode = Objects.hashCode(obj);
|
||||
}
|
||||
|
||||
@Override
|
@@ -10,15 +10,16 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.map.reference;
|
||||
package org.dromara.hutool.core.lang.ref;
|
||||
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 弱键
|
||||
* 弱引用对象,在GC时发现弱引用会回收其对象
|
||||
*
|
||||
* @param <T> 键类型
|
||||
*/
|
||||
@@ -28,12 +29,12 @@ public class WeakObj<T> extends WeakReference<T> {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param key 原始Key,不能为{@code null}
|
||||
* @param obj 原始对象
|
||||
* @param queue {@link ReferenceQueue}
|
||||
*/
|
||||
public WeakObj(final T key, final ReferenceQueue<? super T> queue) {
|
||||
super(key, queue);
|
||||
hashCode = key.hashCode();
|
||||
public WeakObj(final T obj, final ReferenceQueue<? super T> queue) {
|
||||
super(obj, queue);
|
||||
hashCode = Objects.hashCode(obj);
|
||||
}
|
||||
|
||||
@Override
|
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 引用工具封装,主要针对{@link java.lang.ref.Reference} 工具化封装<br>
|
||||
* 主要封装包括:
|
||||
* <pre>
|
||||
* 1. {@link java.lang.ref.SoftReference} 软引用,在GC报告内存不足时会被GC回收
|
||||
* 2. {@link java.lang.ref.WeakReference} 弱引用,在GC时发现弱引用会回收其对象
|
||||
* 3. {@link java.lang.ref.PhantomReference} 虚引用,在GC时发现虚引用对象,会将{@link java.lang.ref.PhantomReference}插入{@link java.lang.ref.ReferenceQueue}。 此时对象未被真正回收,要等到{@link java.lang.ref.ReferenceQueue}被真正处理后才会被回收。
|
||||
* </pre>
|
||||
*/
|
||||
package org.dromara.hutool.core.lang.ref;
|
@@ -13,7 +13,7 @@
|
||||
package org.dromara.hutool.core.map.reference;
|
||||
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.util.ReferenceUtil;
|
||||
import org.dromara.hutool.core.lang.ref.ReferenceUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.ref.Reference;
|
||||
|
@@ -13,7 +13,9 @@
|
||||
package org.dromara.hutool.core.map.reference;
|
||||
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.util.ReferenceUtil;
|
||||
import org.dromara.hutool.core.lang.ref.ReferenceType;
|
||||
import org.dromara.hutool.core.lang.ref.SoftObj;
|
||||
import org.dromara.hutool.core.lang.ref.WeakObj;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.ref.Reference;
|
||||
@@ -38,7 +40,7 @@ public class ReferenceKeyConcurrentMap<K, V> implements ConcurrentMap<K, V>, Ite
|
||||
|
||||
final ConcurrentMap<Reference<K>, V> raw;
|
||||
private final ReferenceQueue<K> lastQueue;
|
||||
private final ReferenceUtil.ReferenceType keyType;
|
||||
private final ReferenceType keyType;
|
||||
/**
|
||||
* 回收监听
|
||||
*/
|
||||
@@ -52,7 +54,7 @@ public class ReferenceKeyConcurrentMap<K, V> implements ConcurrentMap<K, V>, Ite
|
||||
* @param raw {@link ConcurrentMap}实现
|
||||
* @param referenceType Reference类型
|
||||
*/
|
||||
public ReferenceKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw, final ReferenceUtil.ReferenceType referenceType) {
|
||||
public ReferenceKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw, final ReferenceType referenceType) {
|
||||
this.raw = raw;
|
||||
this.keyType = referenceType;
|
||||
lastQueue = new ReferenceQueue<>();
|
||||
|
@@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.core.map.reference;
|
||||
|
||||
import org.dromara.hutool.core.lang.ref.ReferenceType;
|
||||
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
|
||||
import org.dromara.hutool.core.util.ReferenceUtil;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
@@ -42,6 +42,6 @@ public class SoftKeyConcurrentMap<K, V> extends ReferenceKeyConcurrentMap<K, V>
|
||||
* @param raw {@link ConcurrentMap}实现
|
||||
*/
|
||||
public SoftKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw) {
|
||||
super(raw, ReferenceUtil.ReferenceType.SOFT);
|
||||
super(raw, ReferenceType.SOFT);
|
||||
}
|
||||
}
|
||||
|
@@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.core.map.reference;
|
||||
|
||||
import org.dromara.hutool.core.lang.ref.ReferenceType;
|
||||
import org.dromara.hutool.core.map.concurrent.SafeConcurrentHashMap;
|
||||
import org.dromara.hutool.core.util.ReferenceUtil;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
@@ -43,6 +43,6 @@ public class WeakKeyConcurrentMap<K, V> extends ReferenceKeyConcurrentMap<K, V>
|
||||
* @param raw {@link ConcurrentMap}实现
|
||||
*/
|
||||
public WeakKeyConcurrentMap(final ConcurrentMap<Reference<K>, V> raw) {
|
||||
super(raw, ReferenceUtil.ReferenceType.WEAK);
|
||||
super(raw, ReferenceType.WEAK);
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,8 @@ package org.dromara.hutool.core.util;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableObj;
|
||||
import org.dromara.hutool.core.lang.ref.ReferenceType;
|
||||
import org.dromara.hutool.core.lang.ref.ReferenceUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -27,21 +29,21 @@ public class ReferenceUtilTest {
|
||||
|
||||
@Test
|
||||
public void createWeakTest(){
|
||||
final Reference<Integer> integerReference = ReferenceUtil.of(ReferenceUtil.ReferenceType.WEAK, 1);
|
||||
final Reference<Integer> integerReference = ReferenceUtil.of(ReferenceType.WEAK, 1);
|
||||
Assertions.assertInstanceOf(WeakReference.class, integerReference);
|
||||
Assertions.assertEquals(Integer.valueOf(1), integerReference.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSoftTest(){
|
||||
final Reference<Integer> integerReference = ReferenceUtil.of(ReferenceUtil.ReferenceType.SOFT, 1);
|
||||
final Reference<Integer> integerReference = ReferenceUtil.of(ReferenceType.SOFT, 1);
|
||||
Assertions.assertInstanceOf(SoftReference.class, integerReference);
|
||||
Assertions.assertEquals(Integer.valueOf(1), integerReference.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createPhantomTest(){
|
||||
final Reference<Integer> integerReference = ReferenceUtil.of(ReferenceUtil.ReferenceType.PHANTOM, 1);
|
||||
final Reference<Integer> integerReference = ReferenceUtil.of(ReferenceType.PHANTOM, 1);
|
||||
Assertions.assertInstanceOf(PhantomReference.class, integerReference);
|
||||
// get方法永远都返回null,PhantomReference只能用来监控对象的GC状况
|
||||
Assertions.assertNull(integerReference.get());
|
||||
|
Reference in New Issue
Block a user