001/*
002 * (C) Copyright 2006-2020 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.runtime.pubsub.SerializableAccumulableInvalidations;
025
026/**
027 * Propagator of invalidations to a set of {@link InvalidationsQueue}s.
028 *
029 * @param <T> the type of invalidations
030 * @since 11.1
031 */
032public class InvalidationsPropagator<T extends SerializableAccumulableInvalidations> {
033
034    public final List<InvalidationsQueue<T>> queues; // used synchronized
035
036    public InvalidationsPropagator() {
037        queues = new ArrayList<>();
038    }
039
040    public synchronized void addQueue(InvalidationsQueue<T> queue) {
041        if (!queues.contains(queue)) {
042            queues.add(queue);
043        }
044    }
045
046    public synchronized void removeQueue(InvalidationsQueue<T> queue) {
047        queues.remove(queue);
048    }
049
050    @SuppressWarnings("unchecked")
051    public void propagateInvalidations(T invalidations, InvalidationsQueue<T> skipQueue) {
052        List<InvalidationsQueue<T>> qq;
053        synchronized (this) {
054            qq = (List<InvalidationsQueue<T>>) ((ArrayList<InvalidationsQueue<T>>) queues).clone();
055        }
056        for (InvalidationsQueue<T> q : qq) {
057            if (q != skipQueue) {
058                q.addInvalidations(invalidations);
059            }
060        }
061    }
062
063}