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

org.glassfish.pfl.tf.timer.impl.VersionedHashSet Maven / Gradle / Ivy

There is a newer version: 4.0.4
Show newest version
/*
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.glassfish.pfl.tf.timer.impl ;

import java.util.Collection ;
import java.util.Iterator ;
import java.util.Set ;
import java.util.HashSet ;

/** This is an implementation of the Set interface that keeps track
 * of its version so that we can tell when it is modified.
 * Each time an element is added to or removed from the set, the
 * version is incremented.
 * This implementation is synchronized so that the version
 * is consistently updated whenever the set is modified.
 */
public class VersionedHashSet extends HashSet {
    private long version = 0 ;

    public synchronized long version() {
	return version ;
    }

    public VersionedHashSet() {
	super() ;
    }

    public VersionedHashSet( Collection c ) {
	super( c ) ;
    }

    public VersionedHashSet( int initialCapacity, float loadFactor ) {
	super( initialCapacity, loadFactor ) ;
    }

    public VersionedHashSet( int initialCapacity ) {
	super( initialCapacity ) ;
    }

    public synchronized boolean add( E e ) {
	boolean result = super.add( e ) ;
	if (result) 
	    version++ ;
	return result ;
    }

    public synchronized boolean remove( Object o ) {
	boolean result = super.remove( o ) ;
	if (result)
	    version++ ;
	return result ;
    }

    public Iterator iterator() {
	final Iterator state = super.iterator() ;

	return new Iterator() {
	    public boolean hasNext() {
		return state.hasNext() ;
	    }

	    public E next() {
		return state.next() ;
	    }

	    public void remove() {
		synchronized (VersionedHashSet.this) {
		    state.remove() ;
		    version++ ;
		}
	    }
	} ;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy