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
021/**
022 * This class provides channels that are identified by a name
023 * (<code>string</code>). Instances of this class represent channels that use
024 * their name as value for matching channels with handlers.
025 */
026public final class NamedChannel implements Channel {
027
028    private final String name;
029
030    /**
031     * Creates a new named channel with the given name.
032     * 
033     * @param name the channel's name
034     */
035    public NamedChannel(String name) {
036        super();
037        this.name = name;
038    }
039
040    /**
041     * Returns the name of the channel as its value.
042     * 
043     * @return the name
044     * 
045     * @see org.jgrapes.core.Channel#defaultCriterion()
046     */
047    @Override
048    public Object defaultCriterion() {
049        return name;
050    }
051
052    /**
053     * Returns <code>true</code> if the <code>value</code>
054     * matches the name of this channel or is the broadcast channel's value. 
055     * 
056     * @see org.jgrapes.core.Eligible#isEligibleFor(java.lang.Object)
057     */
058    @Override
059    public boolean isEligibleFor(Object value) {
060        return value.equals(BROADCAST.defaultCriterion())
061            || value.equals(name);
062    }
063
064    /*
065     * (non-Javadoc)
066     * 
067     * @see java.lang.Object#toString()
068     */
069    @Override
070    public String toString() {
071        return "NamedChannel [name=" + name + "]";
072    }
073
074    /*
075     * (non-Javadoc)
076     * 
077     * @see java.lang.Object#hashCode()
078     */
079    @Override
080    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
081    public int hashCode() {
082        @SuppressWarnings("PMD.AvoidFinalLocalVariable")
083        final int prime = 31;
084        int result = 1;
085        result = prime * result + ((name == null) ? 0 : name.hashCode());
086        return result;
087    }
088
089    /*
090     * (non-Javadoc)
091     * 
092     * @see java.lang.Object#equals(java.lang.Object)
093     */
094    @Override
095    public boolean equals(Object obj) {
096        if (this == obj) {
097            return true;
098        }
099        if (obj == null) {
100            return false;
101        }
102        if (getClass() != obj.getClass()) {
103            return false;
104        }
105        NamedChannel other = (NamedChannel) obj;
106        if (name == null) {
107            if (other.name != null) {
108                return false;
109            }
110        } else if (!name.equals(other.name)) {
111            return false;
112        }
113        return true;
114    }
115
116}