本文整理了Java中org.apache.sis.util.collection.WeakHashSet
类的一些代码示例,展示了WeakHashSet
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WeakHashSet
类的具体详情如下:
包路径:org.apache.sis.util.collection.WeakHashSet
类名称:WeakHashSet
[英]A set of objects hold by weak references. An entry in a WeakHashSet will automatically be removed when it is no longer in ordinary use. More precisely, the presence of an entry will not prevent the entry from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When an entry has been discarded it is effectively removed from the set, so this class behaves somewhat differently than other java.util.Set implementations.
If the elements stored in this set are arrays like int[], float[] or Object[], then the hash code computations and the comparisons are performed using the static hashCode(a) and equals(a1, a2) methods defined in the Arraysclass.
Optimizing memory use in factory implementations The WeakHashSet class has a #get(Object) method that is not part of the java.util.Set interface. This get method retrieves an entry from this set that is equals to the supplied object. The #unique(Object) method combines a get followed by a add operation if the specified object was not in the set. This is similar in spirit to the String#intern() method. The following example shows a convenient way to use WeakHashSet as an internal pool of immutable objects: javapublic Foo create(String definition) Foo created = new Foo(definition); return pool.unique(created); } } Thus, WeakHashSet can be used inside a factory to prevent creating duplicate immutable objects.
Thread safety
The same WeakHashSet instance can be safely used by many threads without synchronization on the part of the caller. But if a sequence of two or more method calls need to appear atomic from other threads perspective, then the caller can synchronize on this.
[中]由弱引用保存的一组对象。WeakHashSet中的条目在不再正常使用时将自动删除。更准确地说,条目的存在不会阻止垃圾收集器丢弃该条目,也就是说,使其可终结、终结,然后回收。当一个条目被丢弃时,它将被有效地从集合中移除,因此这个类的行为与其他java类有所不同。util。设置实现。
如果此集合中存储的元素是int[]、float[]或Object[]等数组,则使用Arraysclass中定义的静态哈希代码(a)和equals(a1,a2)方法执行哈希代码计算和比较。
优化工厂实现中的内存使用WeakHashSet类有一个不属于java的#get(Object)方法。util。设置接口。这个get方法从这个集合中检索一个与提供的对象相等的条目。如果指定的对象不在集合中,则#unique(Object)方法将get和add操作结合起来。这在本质上与String#intern()方法类似。下面的示例展示了一种将WeakHashSet用作不可变对象的内部池的便捷方法:javapublic Foo create(字符串定义)Foo created=new Foo(定义);返回池。唯一(已创建);}因此,WeakHashSet可以在工厂内使用,以防止创建重复的不可变对象。
线程安全
同一个WeakHashSet实例可以被多个线程安全地使用,而无需调用方进行同步。但是,如果从其他线程的角度来看,两个或多个方法调用的序列需要显示为原子的,那么调用方可以在此基础上进行同步。
代码示例来源:origin: org.apache.sis.core/sis-referencing
/**
* Replaces the given transform by a unique instance, if one already exists.
*/
private MathTransform unique(final MathTransform tr) {
return pool.unique(tr);
}
代码示例来源:origin: apache/sis
/**
* Creates a new factory.
*/
public DefaultNameFactory() {
pool = new WeakHashSet<>(GenericName.class);
}
代码示例来源:origin: org.apache.sis.core/sis-utility
/**
* Returns an array containing every instances of this type that have not yet been
* garbage collected. The first elements of the returned array are the constants
* defined in this class, in declaration order. All other elements are the instances
* created by the {@link #valueOf(String)} method, in no particular order.
*
* @return an array containing the instances of {@code NilReason}.
*/
public static NilReason[] values() {
final int predefinedCount = PREDEFINED.length;
NilReason[] reasons;
synchronized (POOL) {
reasons = POOL.toArray(new NilReason[predefinedCount + POOL.size()]);
}
int count = reasons.length;
while (count != 0 && reasons[count-1] == null) {
count--;
}
count += predefinedCount;
final NilReason[] source = reasons;
if (count != reasons.length) {
reasons = new NilReason[count];
}
System.arraycopy(source, 0, reasons, predefinedCount, count - predefinedCount);
System.arraycopy(PREDEFINED, 0, reasons, 0, predefinedCount);
return reasons;
}
代码示例来源:origin: apache/sis
/**
* Tests with array elements.
*/
@Test
@DependsOnMethod("testStrongReferences")
public void testWithArrayElements() {
final WeakHashSet<int[]> weakSet = new WeakHashSet<>(int[].class);
final int[] array = new int[] {2, 5, 3};
assertTrue (weakSet.add(array));
assertFalse(weakSet.add(array));
assertFalse(weakSet.add(array.clone()));
assertTrue (weakSet.add(new int[] {2, 5, 4}));
assertSame (array, weakSet.unique(array.clone()));
}
}
代码示例来源:origin: apache/sis
final Random random = new Random();
for (int pass=0; pass<NUM_RETRY; pass++) {
final WeakHashSet<Integer> weakSet = new WeakHashSet<>(Integer.class);
final HashSet<Integer> strongSet = new HashSet<>();
for (int i=0; i<SAMPLE_SIZE; i++) {
final boolean weakModified = weakSet .add(value);
final boolean strongModified = strongSet.add(value);
if (weakModified) {
assertNotSame(value, weakSet.get(value));
if (strongModified) {
weakSet.remove(value);
assertTrue(weakSet.add(value));
assertSame(value, weakSet.get(value));
final boolean c = weakSet.contains(value);
if (strongSet.remove(value)) {
assertTrue("contains:", c);
assertTrue("containsAll:", weakSet.containsAll(strongSet));
waitForGarbageCollection(() -> weakSet.size() == strongSet.size());
assertSetEquals(strongSet, weakSet);
代码示例来源:origin: apache/sis
final Random random = new Random();
for (int pass=0; pass<NUM_RETRY; pass++) {
final WeakHashSet<Integer> weakSet = new WeakHashSet<>(Integer.class);
final HashSet<Integer> strongSet = new HashSet<>();
for (int i=0; i<SAMPLE_SIZE; i++) {
final boolean weakModified = weakSet .add(value);
final boolean strongModified = strongSet.add(value);
assertEquals("add:", strongModified, weakModified);
if (strongModified) {
assertSame("get:", value, weakSet.get(value));
} else {
assertEquals("get:", value, weakSet.get(value));
final boolean weakModified = weakSet .remove(value);
final boolean strongModified = strongSet.remove(value);
assertEquals("remove:", strongModified, weakModified);
assertNull("get:", weakSet.get(value));
assertEquals("contains:", strongSet.contains(value), weakSet.contains(value));
assertEquals("equals:", strongSet, weakSet);
代码示例来源:origin: org.apache.sis.core/sis-utility
/**
* Returns an iterator over the elements contained in this collection.
* No element from this set will be garbage collected as long as a
* reference to the iterator is hold.
*
* @return an iterator over all elements in this set.
*/
@Override
public Iterator<E> iterator() {
return Arrays.asList(toArray()).iterator();
}
}
代码示例来源:origin: apache/sis
/**
* Returns an iterator over the elements contained in this collection.
* No element from this set will be garbage collected as long as a
* reference to the iterator is hold.
*
* @return an iterator over all elements in this set.
*/
@Override
public Iterator<E> iterator() {
return Arrays.asList(toArray()).iterator();
}
}
代码示例来源:origin: apache/sis
/**
* Replaces the given transform by a unique instance, if one already exists.
*/
private MathTransform unique(final MathTransform tr) {
return pool.unique(tr);
}
代码示例来源:origin: org.apache.sis.core/sis-utility
/**
* Creates a new factory.
*/
public DefaultNameFactory() {
pool = new WeakHashSet<>(GenericName.class);
}
代码示例来源:origin: apache/sis
/**
* Returns an array containing every instances of this type that have not yet been
* garbage collected. The first elements of the returned array are the constants
* defined in this class, in declaration order. All other elements are the instances
* created by the {@link #valueOf(String)} method, in no particular order.
*
* @return an array containing the instances of {@code NilReason}.
*/
public static NilReason[] values() {
final int predefinedCount = PREDEFINED.length;
NilReason[] reasons;
synchronized (POOL) {
reasons = POOL.toArray(new NilReason[predefinedCount + POOL.size()]);
}
int count = reasons.length;
while (count != 0 && reasons[count-1] == null) {
count--;
}
count += predefinedCount;
final NilReason[] source = reasons;
if (count != reasons.length) {
reasons = new NilReason[count];
}
System.arraycopy(source, 0, reasons, predefinedCount, count - predefinedCount);
System.arraycopy(PREDEFINED, 0, reasons, 0, predefinedCount);
return reasons;
}
代码示例来源:origin: org.apache.sis.core/sis-utility
/**
* Returns a unique fraction instance equals to {@code this}.
* If this method has been invoked previously on another {@code Fraction} with the same value than {@code this},
* then that previous instance is returned (provided that it has not yet been garbage collected). Otherwise this
* method adds this fraction to the pool of fractions that may be returned in next {@code unique()} invocations,
* then returns {@code this}.
*
* <p>This method is useful for saving memory when a potentially large amount of {@code Fraction} instances will
* be kept for a long time and many instances are likely to have the same values.
* It is usually not worth to invoke this method for short-lived instances.</p>
*
* @return a unique instance of a fraction equals to {@code this}.
*/
public Fraction unique() {
return POOL.unique(this);
}
代码示例来源:origin: apache/sis
/**
* Constructs a factory with the given default properties.
* {@code GeodeticObjectFactory} will fallback on the map given to this constructor for any property
* not present in the map provided to a {@code createFoo(Map<String,?>, …)} method.
*
* @param properties the default properties, or {@code null} if none.
*/
public GeodeticObjectFactory(Map<String,?> properties) {
if (properties == null || properties.isEmpty()) {
properties = Collections.emptyMap();
} else {
properties = CollectionsExt.compact(new HashMap<>(properties));
}
defaultProperties = properties;
pool = new WeakHashSet<>(AbstractIdentifiedObject.class);
parser = new AtomicReference<>();
}
代码示例来源:origin: apache/sis
/**
* Returns a unique fraction instance equals to {@code this}.
* If this method has been invoked previously on another {@code Fraction} with the same value than {@code this},
* then that previous instance is returned (provided that it has not yet been garbage collected). Otherwise this
* method adds this fraction to the pool of fractions that may be returned in next {@code unique()} invocations,
* then returns {@code this}.
*
* <p>This method is useful for saving memory when a potentially large amount of {@code Fraction} instances will
* be kept for a long time and many instances are likely to have the same values.
* It is usually not worth to invoke this method for short-lived instances.</p>
*
* @return a unique instance of a fraction equals to {@code this}.
*/
public Fraction unique() {
return POOL.unique(this);
}
代码示例来源:origin: org.apache.sis.core/sis-referencing
/**
* Constructs a factory with the given default properties.
* {@code GeodeticObjectFactory} will fallback on the map given to this constructor for any property
* not present in the map provided to a {@code createFoo(Map<String,?>, …)} method.
*
* @param properties the default properties, or {@code null} if none.
*/
public GeodeticObjectFactory(Map<String,?> properties) {
if (properties == null || properties.isEmpty()) {
properties = Collections.emptyMap();
} else {
properties = CollectionsExt.compact(new HashMap<>(properties));
}
defaultProperties = properties;
pool = new WeakHashSet<>(AbstractIdentifiedObject.class);
parser = new AtomicReference<>();
}
代码示例来源:origin: apache/sis
/**
* Invoked after deserialization in order to return a unique instance if possible.
*
* @return a unique instance of the deserialized {@code NilReason}.
* @throws ObjectStreamException required by specification but should never be thrown.
*/
private Object readResolve() throws ObjectStreamException {
if (reason instanceof String) {
for (final NilReason candidate : PREDEFINED) {
if (reason.equals(candidate.reason)) {
return candidate;
}
}
}
return POOL.unique(this);
}
代码示例来源:origin: org.apache.sis.core/sis-referencing
methodsByType = new IdentityHashMap<>();
lastMethod = new ThreadLocal<>();
pool = new WeakHashSet<>(MathTransform.class);
parser = new AtomicReference<>();
代码示例来源:origin: org.apache.sis.core/sis-utility
/**
* Invoked after deserialization in order to return a unique instance if possible.
*
* @return a unique instance of the deserialized {@code NilReason}.
* @throws ObjectStreamException required by specification but should never be thrown.
*/
private Object readResolve() throws ObjectStreamException {
if (reason instanceof String) {
for (final NilReason candidate : PREDEFINED) {
if (reason.equals(candidate.reason)) {
return candidate;
}
}
}
return POOL.unique(this);
}
代码示例来源:origin: apache/sis
methodsByType = new IdentityHashMap<>();
lastMethod = new ThreadLocal<>();
pool = new WeakHashSet<>(MathTransform.class);
parser = new AtomicReference<>();
代码示例来源:origin: org.apache.sis.core/sis-utility
/**
* Returns the code list for the given ordinal value. This methods depends
* only on the code list type; it does not depend on the content of this set.
*/
final E valueOf(final int ordinal) {
E[] array = codes;
if (array == null || ordinal >= array.length) {
codes = array = POOL.unique(Types.getCodeValues(elementType));
}
return array[ordinal];
}
内容来源于网络,如有侵权,请联系作者删除!