001/*
002 *  Copyright 2013 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.core.observation;
017
018import java.util.Map;
019import java.util.Set;
020
021/**
022 * Observer for consuming events.
023 */
024public interface Observer
025{
026    /** Minimum priority. */
027    public static final int MIN_PRIORITY = Integer.MAX_VALUE;
028    /** Maximum priority. */
029    public static final int MAX_PRIORITY = 0;
030    
031    /**
032     * Checks if the event is supported. If true, the observe(Event) method will be called.
033     * @param event the event.
034     * @return <code>true</code> for observing this event, <code>false</code> otherwise.
035     */
036    boolean supports(Event event);
037    
038    /**
039     * Retrieves the priority to observe this event.<br>
040     * This can be used to process a supported event before others observers.
041     * @param event the event.
042     * @return the priority where 0 the max priority and Integer.MAX_VALUE the min priority.
043     */
044    int getPriority(Event event);
045    
046    /**
047     * Observes an event.
048     * @param event the event.
049     * @param transientVars transientVars passed from one Observer to another when processing a single Event. 
050     * This may allow optimizations between observers.
051     * @throws Exception if an error occurs. 
052     * All exceptions will be logged but not propagated, as the observation mechanism should never fail.
053     */
054    public void observe(Event event, Map<String, Object> transientVars) throws Exception;
055    
056    /**
057     * Get the traits of the observer
058     * @return the traits of the observer
059     */
060    public default Set<String> getTraits()
061    {
062        return Set.of();
063    }
064}