001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.storage.sql;
013
014/**
015 * Queue of invalidations.
016 * <p>
017 * All invalidations added are accumulated (from multiple threads), then returned when asked for.
018 */
019public class InvalidationsQueue {
020
021    public Invalidations queue; // used under synchronization
022
023    /** used for debugging */
024    public final String name;
025
026    public InvalidationsQueue(String name) {
027        queue = new Invalidations();
028        this.name = name;
029    }
030
031    /**
032     * Adds invalidations.
033     * <p>
034     * May be called asynchronously from multiple threads.
035     */
036    public synchronized void addInvalidations(Invalidations invalidations) {
037        queue.add(invalidations);
038    }
039
040    /**
041     * Gets the queued invalidations and resets the queue.
042     */
043    public synchronized Invalidations getInvalidations() {
044        Invalidations invalidations = queue;
045        queue = new Invalidations();
046        return invalidations;
047    }
048
049    @Override
050    public String toString() {
051        return getClass().getSimpleName() + '(' + name + ')';
052    }
053
054}