package org.chiefly.sunlamp.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
/** {@link VariableBinding} utility class. */
public class VariableBindings {
/** Private default constructor. */
private VariableBindings() {
throw new UnsupportedOperationException();
}
/**
* Extracts the {@link OID}s from the given {@link VariableBinding}s and
* loads them into a {@link Set}.
*
* @param bindings The {@link VariableBinding}s to extract from.
* @return A {@link Set} of {@link OID}s, which may be empty.
*/
public static Set<OID> extractOids(final Collection<? extends VariableBinding> bindings) {
Preconditions.checkNotNull(bindings);
final Set<OID> results = Sets.newHashSet();
for (final VariableBinding binding : bindings) {
results.add(binding.getOid());
}
return results;
}
/**
* @param bindings The bindings we're interested in filtering.
* @param prefix The {@link OID} prefix to filter with.
* @param filterMatches True to filter out bindings whose {@link OID} matches the prefix, false to filter those that
* don't match.
*/
public static void filterByOidPrefix(final Map<OID, Variable> bindings, final OID prefix,
final boolean filterMatches) {
Preconditions.checkNotNull(bindings);
Preconditions.checkNotNull(prefix);
/* Filter the bindings */
final Iterator<Map.Entry<OID, Variable>> iterator = bindings.entrySet().iterator();
while (iterator.hasNext()) {
final Map.Entry<OID, Variable> entry = iterator.next();
/* See if the prefix matches */
if (entry.getKey().startsWith(prefix)) {
if (filterMatches) {
iterator.remove();
}
}
/*
* Prefix doesn't match, so only remove if we're targeting
* non-matches
*/
else if (!filterMatches) {
iterator.remove();
}
}
}
/**
* @param bindings The bindings we're interested in filtering.
* @param regex The regular expression to filter with.
* @param filterMatches True to filter out bindings whose {@link OID} matches the prefix, false to filter those that
* don't match.
*/
public static void filterByOidRegex(final Map<OID, Variable> bindings, final String regex,
final boolean filterMatches) {
Preconditions.checkNotNull(bindings);
Preconditions.checkNotNull(regex);
/* Filter the bindings */
final Iterator<Map.Entry<OID, Variable>> iterator = bindings.entrySet().iterator();
while (iterator.hasNext()) {
final Map.Entry<OID, Variable> entry = iterator.next();
/* See if the regex matches */
if (entry.getKey().toString().matches(regex)) {
if (filterMatches) {
iterator.remove();
}
}
/*
* Regex doesn't match, so only remove if we're targeting
* non-matches
*/
else if (!filterMatches) {
iterator.remove();
}
}
}
/**
* Wraps {@link OID}s in {@link VariableBinding}s.
*
* @param oids The {@link OID}s to wrap.
* @return Returns the {@link VariableBinding}s.
*/
public static VariableBinding[] wrapOids(final OID... oids) {
Preconditions.checkNotNull(oids);
final VariableBinding[] wrapped = new VariableBinding[oids.length];
int index = 0;
for (final OID oid : oids) {
wrapped[index++] = new VariableBinding(oid);
}
return wrapped;
}
}