001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.cms.content.indexing.solr.observation;
017
018/**
019 * This helper centralize a few common helper methods
020 */
021public final class ObserverHelper
022{
023    private static int _suspended;
024    
025    private ObserverHelper()
026    {
027        // Nothing
028    }
029    
030    /**
031     * All threads will not index anymore after observed events
032     * Call {@link #restartObservationForIndexation()} to listen to events again.
033     * You have to call {@link #restartObservationForIndexation()} the same number of times you call {@link #suspendObservationForIndexation()}
034     */
035    public static synchronized void suspendObservationForIndexation()
036    {
037        _suspended++;
038    }
039    
040    /**
041     * Is events observation suspended for indexation?
042     * @return true if events are not suspended, false otherwise.
043     */
044    public static synchronized boolean isNotSuspendedObservationForIndexation()
045    {
046        return _suspended == 0;
047    }
048
049    /**
050     * Listen to events again after a call to {@link #suspendObservationForIndexation()}
051     * You have to call {@link #restartObservationForIndexation()} the same number of times you call {@link #suspendObservationForIndexation()}
052     */
053    public static synchronized void restartObservationForIndexation()
054    {
055        _suspended--;
056    }
057}