001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-2026 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
013 * License for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License
016 * along 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;
027import org.jgrapes.core.ComponentType;
028import org.jgrapes.core.annotation.HandlerDefinition;
029import org.jgrapes.core.annotation.HandlerDefinition.Evaluator;
030
031/**
032 * Common utility methods.
033 */
034@SuppressWarnings("PMD.MoreThanOneLogger")
035public final class CoreUtils {
036
037    @SuppressWarnings("PMD.FieldNamingConventions")
038    public static final Logger classNames
039        = Logger.getLogger(ComponentType.class.getPackage().getName()
040            + ".classNames");
041
042    @SuppressWarnings("PMD.FieldNamingConventions")
043    /* default */ static final Logger fireRestrictionLogger
044        = Logger.getLogger(CoreUtils.class.getPackage().getName()
045            + ".fireRestriction");
046
047    /** Handler factory cache. */
048    private static Map<Class<? extends HandlerDefinition.Evaluator>,
049            HandlerDefinition.Evaluator> definitionEvaluators
050                = Collections.synchronizedMap(new HashMap<>());
051    private static AtomicReference<AssertionError> assertionError
052        = new AtomicReference<>();
053
054    private CoreUtils() {
055    }
056
057    /**
058     * Create a new definition evaluator.
059     *
060     * @param hda the handler definition annotation
061     * @return the evaluator
062     */
063    public static Evaluator definitionEvaluator(
064            HandlerDefinition hda) {
065        return definitionEvaluators.computeIfAbsent(hda.evaluator(), key -> {
066            try {
067                return hda.evaluator().getConstructor().newInstance();
068            } catch (InstantiationException | IllegalAccessException
069                    | IllegalArgumentException | InvocationTargetException
070                    | NoSuchMethodException | SecurityException e) {
071                throw new IllegalStateException(e);
072            }
073        });
074    }
075
076    /* default */ static void setAssertionError(AssertionError error) {
077        assertionError.compareAndSet(null, error);
078    }
079
080    /**
081     * Check if an assertion has been set and if, throw it.
082     */
083    public static void checkAssertions() {
084        AssertionError error = assertionError.getAndSet(null);
085        if (error != null) {
086            throw error;
087        }
088    }
089
090}