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 org.jgrapes.core.annotation.Handler;
022import org.jgrapes.core.annotation.HandlerDefinition.ChannelReplacements;
023import org.jgrapes.core.internal.ComponentVertex;
024
025/**
026 * This class can be used as base class for implementing a component. 
027 * <P>
028 * This class implements the {@link Manager} interface. Contrary 
029 * to classes that only implement {@link ComponentType}, derived 
030 * classes therefore don't need a manager attribute to get access to the 
031 * component management methods provided by this interface.
032 * <P>
033 * This class also implements the {@code Channel} interface in such a way
034 * that each instance of this class can be used as an independent
035 * channel. Note that events that have a component as one of their
036 * channels are always handled by the component, independent of
037 * channels explicitly specified by the handler. 
038 * 
039 * @see Handler
040 * @see ComponentType
041 */
042public abstract class Component extends ComponentVertex
043        implements ComponentType, Channel {
044
045    private Channel componentChannel;
046
047    /**
048     * Creates a new component base with its channel set to
049     * itself.
050     */
051    public Component() {
052        super();
053        componentChannel = this;
054        initComponentsHandlers(null);
055    }
056
057    /**
058     * Creates a new component base with its channel set to the given 
059     * channel. As a special case {@link Channel#SELF} can be
060     * passed to the constructor to make the component use itself
061     * as channel. The special value is necessary as you 
062     * obviously cannot pass an object to be constructed to its 
063     * constructor.
064     *
065     * @param componentChannel the channel that the component's
066     * handlers listen on by default and that 
067     * {@link Manager#fire(Event, Channel...)} sends the event to
068     */
069    public Component(Channel componentChannel) {
070        super();
071        if (componentChannel == SELF) {
072            this.componentChannel = this;
073        } else {
074            this.componentChannel = componentChannel;
075        }
076        initComponentsHandlers(null);
077    }
078
079    /**
080     * Creates a new component base like {@link #Component(Channel)}
081     * but with channel mappings for {@link Handler} annotations.
082     *
083     * @param componentChannel the channel that the component's
084     * handlers listen on by default and that 
085     * {@link Manager#fire(Event, Channel...)} sends the event to
086     * @param channelReplacements the channel replacements to apply
087     * to the `channels` elements of the {@link Handler} annotations
088     */
089    public Component(
090            Channel componentChannel, ChannelReplacements channelReplacements) {
091        super();
092        if (componentChannel == SELF) {
093            this.componentChannel = this;
094        } else {
095            this.componentChannel = componentChannel;
096        }
097        initComponentsHandlers(channelReplacements);
098    }
099
100    /*
101     * (non-Javadoc)
102     * 
103     * @see org.jgrapes.core.internal.ComponentVertex#setName(java.lang.String)
104     */
105    @Override
106    public Component setName(String name) {
107        super.setName(name);
108        return this;
109    }
110
111    /*
112     * (non-Javadoc)
113     * 
114     * @see org.jgrapes.core.internal.ComponentVertex#getComponent()
115     */
116    @Override
117    protected Component component() {
118        return this;
119    }
120
121    /**
122     * Returns the channel associated with the component.
123     * 
124     * @return the channel as assigned by the constructor.
125     * 
126     * @see org.jgrapes.core.Manager#channel()
127     */
128    @Override
129    public Channel channel() {
130        return componentChannel;
131    }
132
133    /**
134     * Return the object itself as value.
135     */
136    @Override
137    public Object defaultCriterion() {
138        return this;
139    }
140
141    /**
142     * Matches the object itself (using identity comparison) or the
143     * {@link Channel} class.
144     * 
145     * @see Channel#isEligibleFor(Object)
146     */
147    @Override
148    public boolean isEligibleFor(Object value) {
149        return value.equals(Channel.class)
150            || value == defaultCriterion();
151    }
152
153}