All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.subgraph.orchid.data.RandomSet Maven / Gradle / Ivy

The newest version!
package com.subgraph.orchid.data;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.subgraph.orchid.TorException;

public class RandomSet {
	
	private final Set set;
	private final List list;
	private final SecureRandom random;
	
	public RandomSet() {
		set = new HashSet();
		list = new ArrayList();
		random = createRandom();
	}
	
	private static SecureRandom createRandom() {
		try {
			return SecureRandom.getInstance("SHA1PRNG");
		} catch (NoSuchAlgorithmException e) {
			throw new TorException(e);
		}
	}
	
	public boolean add(E o) {
		if(set.add(o)) {
			list.add(o);
			return true;
		} else {
			return false;
		}
	}
	
	public boolean contains(Object o) {
		return set.contains(o);
	}
	
	public boolean isEmpty() {
		return set.isEmpty();
	}
	
	public void clear() {
		set.clear();
		list.clear();
	}
	
	public boolean remove(Object o) {
		if(set.remove(o)) {
			list.remove(o);
			return true;
		} else {
			return false;
		}
	}
	
	public int size() {
		return set.size();
	}
	
	public E getRandomElement() {
		int idx = random.nextInt(list.size());
		return list.get(idx);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy