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 is the root base class for channels that use their class (type)
023 * as value for matching (see {@link Eligible}).
024 */
025public class ClassChannel implements Channel {
026
027    /**
028     * Returns the class of this channel as value.
029     * 
030     * @return the class of this channel
031     * 
032     * @see org.jgrapes.core.Eligible#defaultCriterion()
033     */
034    @Override
035    public Object defaultCriterion() {
036        return getClass();
037    }
038
039    /**
040     * Returns <code>true</code> if the <code>value</code>
041     * is the same class or a base class of this channel's class.
042     * 
043     * @see org.jgrapes.core.Eligible#isEligibleFor(java.lang.Object)
044     */
045    @Override
046    public boolean isEligibleFor(Object value) {
047        return Class.class.isInstance(value)
048            && ((Class<?>) value)
049                .isAssignableFrom((Class<?>) defaultCriterion());
050    }
051
052    /*
053     * (non-Javadoc)
054     * 
055     * @see java.lang.Object#hashCode()
056     */
057    @Override
058    public int hashCode() {
059        return defaultCriterion().hashCode();
060    }
061
062    /*
063     * (non-Javadoc)
064     * 
065     * @see java.lang.Object#equals(java.lang.Object)
066     */
067    @Override
068    public boolean equals(Object obj) {
069        if (this == obj) {
070            return true;
071        }
072        if (obj == null) {
073            return false;
074        }
075        if (getClass() != obj.getClass()) {
076            return false;
077        }
078        ClassChannel other = (ClassChannel) obj;
079        if (defaultCriterion() == null) {
080            if (other.defaultCriterion() != null) {
081                return false;
082            }
083        } else if (!defaultCriterion().equals(other.defaultCriterion())) {
084            return false;
085        }
086        return true;
087    }
088
089    /*
090     * (non-Javadoc)
091     * 
092     * @see java.lang.Object#toString()
093     */
094    @Override
095    public String toString() {
096        return Components.className(getClass())
097            + " [criterion="
098            + (defaultCriterion() instanceof Class
099                ? Components.className((Class<?>) defaultCriterion())
100                : defaultCriterion())
101            + "]";
102    }
103}