001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.core.internal;
020
021import java.lang.reflect.InvocationTargetException;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.concurrent.atomic.AtomicReference;
026import java.util.logging.Logger;
027
028import org.jgrapes.core.ComponentType;
029import org.jgrapes.core.annotation.HandlerDefinition;
030import org.jgrapes.core.annotation.HandlerDefinition.Evaluator;
031
032/**
033 * Common utility methods.
034 */
035@SuppressWarnings("PMD.MoreThanOneLogger")
036public final class CoreUtils {
037
038    @SuppressWarnings("PMD.FieldNamingConventions")
039    public static final Logger classNames
040        = Logger.getLogger(ComponentType.class.getPackage().getName()
041            + ".classNames");
042
043    @SuppressWarnings("PMD.FieldNamingConventions")
044    /* default */ static final Logger fireRestrictionLogger
045        = Logger.getLogger(CoreUtils.class.getPackage().getName()
046            + ".fireRestriction");
047
048    /** Handler factory cache. */
049    @SuppressWarnings("PMD.UseConcurrentHashMap")
050    private static Map<Class<? extends HandlerDefinition.Evaluator>,
051            HandlerDefinition.Evaluator> definitionEvaluators
052                = Collections.synchronizedMap(new HashMap<>());
053    private static AtomicReference<AssertionError> assertionError
054        = new AtomicReference<>();
055
056    private CoreUtils() {
057    }
058
059    /**
060     * Create a new definition evaluator.
061     *
062     * @param hda the handler definition annotation
063     * @return the evaluator
064     */
065    public static Evaluator definitionEvaluator(
066            HandlerDefinition hda) {
067        return definitionEvaluators.computeIfAbsent(hda.evaluator(), key -> {
068            try {
069                return hda.evaluator().getConstructor().newInstance();
070            } catch (InstantiationException | IllegalAccessException
071                    | IllegalArgumentException | InvocationTargetException
072                    | NoSuchMethodException | SecurityException e) {
073                throw new IllegalStateException(e);
074            }
075        });
076    }
077
078    /* default */ static void setAssertionError(AssertionError error) {
079        assertionError.compareAndSet(null, error);
080    }
081
082    /**
083     * Check if an assertion has been set and if, throw it.
084     */
085    public static void checkAssertions() {
086        AssertionError error = assertionError.getAndSet(null);
087        if (error != null) {
088            throw error;
089        }
090    }
091
092}