view src/org/dancres/blitz/UIDSet.java @ 35:6f68e94c1fb8 default tip

Add CondensedStats monitoring utility, equivalent to vmstat
author Dominic Cleal <dominic-cleal@cdo2.com>
date Thu, 05 Aug 2010 11:07:25 +0100
parents 3dc0c5604566
children
line wrap: on
line source

package org.dancres.blitz;

import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;

/**
 * A set of UID's that is guarenteed to contain no duplicates and limits it's
 * size to the specified capacity
 */
public class UIDSet {
    private Set theFilter = new HashSet();
    private ArrayList theMembers = new ArrayList();
    private long theCapacity;

    UIDSet(long aMaxSize) {
        theCapacity = aMaxSize;
    }

    void add(SpaceEntryUID aUID) {
        synchronized(theFilter) {
            if (isFull())
                return;

            if (! theFilter.contains(aUID)) {
                theFilter.add(aUID);
                theMembers.add(aUID);
            }
        }
    }

    boolean isFull() {
        synchronized(theFilter) {
            return (theFilter.size() == theCapacity);
        }
    }

    SpaceEntryUID pop() {
        synchronized(theFilter) {
            if (theMembers.size() == 0)
                return null;
            else
                return (SpaceEntryUID) theMembers.remove(theMembers.size() - 1);
        }
    }
}