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;
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    public int hashCode() {
081        final int prime = 31;
082        int result = 1;
083        result = prime * result + ((name == null) ? 0 : name.hashCode());
084        return result;
085    }
086
087    /*
088     * (non-Javadoc)
089     * 
090     * @see java.lang.Object#equals(java.lang.Object)
091     */
092    @Override
093    public boolean equals(Object obj) {
094        if (this == obj) {
095            return true;
096        }
097        if (obj == null) {
098            return false;
099        }
100        if (getClass() != obj.getClass()) {
101            return false;
102        }
103        NamedChannel other = (NamedChannel) obj;
104        if (name == null) {
105            if (other.name != null) {
106                return false;
107            }
108        } else if (!name.equals(other.name)) {
109            return false;
110        }
111        return true;
112    }
113
114}