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;
020
021import java.util.Arrays;
022import java.util.HashMap;
023import java.util.Map;
024
025/**
026 * A class for events using a simple name as the event's kind.
027 */
028public final class NamedEvent<T> extends Event<T> {
029
030    private final String kind;
031    private Map<Object, Object> data;
032
033    /**
034     * Creates a new named event with the given name.
035     * 
036     * @param kind the event's kind
037     */
038    public NamedEvent(String kind) {
039        super();
040        this.kind = kind;
041    }
042
043    /**
044     * Returns the kind of the event as the String passed to the
045     * constructor.
046     * 
047     * @return the kind
048     * 
049     * @see org.jgrapes.core.Channel#defaultCriterion()
050     */
051    @Override
052    public Object defaultCriterion() {
053        return kind;
054    }
055
056    /**
057     * Returns `true` if the criterion is `Event.class` (representing 
058     * "any event") or if the criterion is a String equal to this 
059     * event's kind (the String passed to the constructor).
060     * 
061     * @see org.jgrapes.core.Eligible#isEligibleFor(java.lang.Object)
062     */
063    @Override
064    public boolean isEligibleFor(Object criterion) {
065        return criterion.equals(Event.class) || criterion.equals(kind);
066    }
067
068    /**
069     * Returns a map with data that belongs to the event. The map
070     * is only created if requested. If a component uses
071     * {@link NamedEvent}s and data that consists of JDK types only,
072     * it is completely loosely coupled.
073     * 
074     * @return the map
075     */
076    public Map<Object, Object> data() {
077        if (data == null) {
078            data = new HashMap<>();
079        }
080        return data;
081    }
082
083    /*
084     * (non-Javadoc)
085     * 
086     * @see java.lang.Object#toString()
087     */
088    @Override
089    public String toString() {
090        StringBuilder result = new StringBuilder(50);
091        result.append("NamedEvent [name=")
092            .append(kind);
093        if (channels().length > 0) {
094            result.append(", channels=")
095                .append(Arrays.toString(channels()));
096        }
097        result.append(']');
098        return result.toString();
099    }
100}