com.torchmind.observable.utility.WeakCopyOnWriteSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of observables Show documentation
Show all versions of observables Show documentation
Provides simple observable properties with binding capabilities.
/*
* Copyright 2017 Johannes Donath
* and other copyright owners as documented in the project's IP log.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.torchmind.observable.utility;
import java.lang.ref.WeakReference;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.annotation.Nonnull;
/**
* Provides a set which relies on weak references to store its elements.
*
* @author Johannes Donath
*/
public class WeakCopyOnWriteSet extends AbstractSet {
private final CopyOnWriteArraySet> elements;
public WeakCopyOnWriteSet() {
this.elements = new CopyOnWriteArraySet<>();
}
public WeakCopyOnWriteSet(@Nonnull Collection collection) {
this();
this.addAll(collection);
}
/**
* {@inheritDoc}
*/
@Override
public boolean add(E e) {
if (e == null) {
throw new IllegalArgumentException("Cannot add null value");
}
if (this.contains(e)) {
return false;
}
WeakReference reference = new WeakReference<>(e);
return this.elements.add(reference);
}
/**
* Performs a set cleanup.
*/
private void performCleanup() {
this.elements.removeIf(element -> element.get() == null);
}
/**
* {@inheritDoc}
*/
@Override
public boolean contains(Object o) {
this.performCleanup();
for (WeakReference reference : this.elements) {
if (Objects.equals(o, reference.get())) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
@Nonnull
@Override
public Iterator iterator() {
this.performCleanup();
final Iterator> it = this.elements.iterator();
return new Iterator() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public E next() {
return it.next().get();
}
};
}
/**
* {@inheritDoc}
*/
@Override
public boolean remove(Object o) {
this.performCleanup();
return this.elements.removeIf((r) -> Objects.equals(o, r.get()));
}
/**
* {@inheritDoc}
*/
@Override
public int size() {
this.performCleanup();
return this.elements.size();
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
WeakCopyOnWriteSet> that = (WeakCopyOnWriteSet>) o;
return Objects.equals(this.elements, that.elements);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.elements);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy