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;
022import java.util.Queue;
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     * Adds a newly created {@link EventChannelsTuple} to the given queue.
050     *
051     * @param queue the queue
052     * @param event the event
053     * @param channels the channels
054     */
055    public static void addTo(Queue<EventChannelsTuple> queue,
056            EventBase<?> event, Channel... channels) {
057        queue.add(new EventChannelsTuple(event, channels));
058    }
059
060    /*
061     * (non-Javadoc)
062     * 
063     * @see java.lang.Object#hashCode()
064     */
065    @Override
066    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
067    public int hashCode() {
068        @SuppressWarnings("PMD.AvoidFinalLocalVariable")
069        final int prime = 31;
070        int result = 1;
071        result = prime * result + Arrays.hashCode(channels);
072        result = prime * result + ((event == null) ? 0 : event.hashCode());
073        return result;
074    }
075
076    /*
077     * (non-Javadoc)
078     * 
079     * @see java.lang.Object#equals(java.lang.Object)
080     */
081    @Override
082    public boolean equals(Object obj) {
083        if (this == obj) {
084            return true;
085        }
086        if (obj == null) {
087            return false;
088        }
089        if (getClass() != obj.getClass()) {
090            return false;
091        }
092        EventChannelsTuple other = (EventChannelsTuple) obj;
093        if (!Arrays.equals(channels, other.channels)) {
094            return false;
095        }
096        if (event == null) {
097            if (other.event != null) {
098                return false;
099            }
100        } else if (!event.equals(other.event)) {
101            return false;
102        }
103        return true;
104    }
105
106    /*
107     * (non-Javadoc)
108     * 
109     * @see java.lang.Object#toString()
110     */
111    @Override
112    public String toString() {
113        StringBuilder builder = new StringBuilder(50);
114        builder.append("EventChannelsTuple [");
115        if (event != null) {
116            builder.append("event=")
117                .append(event)
118                .append(", ");
119        }
120        if (channels != null) {
121            builder.append("channels=")
122                .append(Arrays.toString(channels));
123        }
124        builder.append(']');
125        return builder.toString();
126    }
127
128}