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.internal;
020
021import java.util.Arrays;
022
023import org.jgrapes.core.Channel;
024
025/**
026 * This class provides a container for an event and an arbitrary 
027 * number of channels. Instances represent a particular event being fired
028 * on several channels. They are used e.g. to queue the information
029 * about an event being fired on some channels.
030 */
031public class EventChannelsTuple {
032    public EventBase<?> event;
033    public Channel[] channels;
034
035    /**
036     * Create a new instance.
037     * 
038     * @param event the event
039     * @param channels the channels
040     */
041    @SuppressWarnings("PMD.UseVarargs")
042    public EventChannelsTuple(EventBase<?> event, Channel[] channels) {
043        super();
044        this.event = event;
045        this.channels = Arrays.copyOf(channels, channels.length);
046    }
047
048    /*
049     * (non-Javadoc)
050     * 
051     * @see java.lang.Object#hashCode()
052     */
053    @Override
054    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
055    public int hashCode() {
056        @SuppressWarnings("PMD.AvoidFinalLocalVariable")
057        final int prime = 31;
058        int result = 1;
059        result = prime * result + Arrays.hashCode(channels);
060        result = prime * result + ((event == null) ? 0 : event.hashCode());
061        return result;
062    }
063
064    /*
065     * (non-Javadoc)
066     * 
067     * @see java.lang.Object#equals(java.lang.Object)
068     */
069    @Override
070    public boolean equals(Object obj) {
071        if (this == obj) {
072            return true;
073        }
074        if (obj == null) {
075            return false;
076        }
077        if (getClass() != obj.getClass()) {
078            return false;
079        }
080        EventChannelsTuple other = (EventChannelsTuple) obj;
081        if (!Arrays.equals(channels, other.channels)) {
082            return false;
083        }
084        if (event == null) {
085            if (other.event != null) {
086                return false;
087            }
088        } else if (!event.equals(other.event)) {
089            return false;
090        }
091        return true;
092    }
093
094    /*
095     * (non-Javadoc)
096     * 
097     * @see java.lang.Object#toString()
098     */
099    @Override
100    public String toString() {
101        StringBuilder builder = new StringBuilder(50);
102        builder.append("EventChannelsTuple [");
103        if (event != null) {
104            builder.append("event=")
105                .append(event)
106                .append(", ");
107        }
108        if (channels != null) {
109            builder.append("channels=")
110                .append(Arrays.toString(channels));
111        }
112        builder.append(']');
113        return builder.toString();
114    }
115
116}