com.signalfx.shaded.google.common.graph.InvalidatableSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of signalfx-codahale Show documentation
Show all versions of signalfx-codahale Show documentation
Dropwizard Codahale metrics plugin for signalfx
The newest version!
package com.signalfx.shaded.google.common.graph;
import static com.signalfx.shaded.google.common.base.Preconditions.checkNotNull;
import com.signalfx.shaded.google.common.base.Supplier;
import com.signalfx.shaded.google.common.collect.ForwardingSet;
import java.util.Set;
/**
* A subclass of `ForwardingSet` that throws `IllegalStateException` on invocation of any method
* (except `hashCode` and `equals`) if the provided `Supplier` returns false.
*/
@ElementTypesAreNonnullByDefault
final class InvalidatableSet extends ForwardingSet {
private final Supplier validator;
private final Set delegate;
private final Supplier errorMessage;
public static final InvalidatableSet of(
Set delegate, Supplier validator, Supplier errorMessage) {
return new InvalidatableSet<>(
checkNotNull(delegate), checkNotNull(validator), checkNotNull(errorMessage));
}
@Override
protected Set delegate() {
validate();
return delegate;
}
private InvalidatableSet(
Set delegate, Supplier validator, Supplier errorMessage) {
this.delegate = delegate;
this.validator = validator;
this.errorMessage = errorMessage;
}
// Override hashCode() to access delegate directly (so that it doesn't trigger the validate() call
// via delegate()); it seems inappropriate to throw ISE on this method.
@Override
public int hashCode() {
return delegate.hashCode();
}
private void validate() {
// Don't use checkState(), because we don't want the overhead of generating the error message
// unless it's actually going to be used; validate() is called for all set method calls, so it
// needs to be fast.
// (We could instead generate the message once, when the set is created, but zero is better.)
if (!validator.get()) {
throw new IllegalStateException(errorMessage.get());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy