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.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    public int hashCode() {
067        final int prime = 31;
068        int result = 1;
069        result = prime * result + Arrays.hashCode(channels);
070        result = prime * result + ((event == null) ? 0 : event.hashCode());
071        return result;
072    }
073
074    /*
075     * (non-Javadoc)
076     * 
077     * @see java.lang.Object#equals(java.lang.Object)
078     */
079    @Override
080    public boolean equals(Object obj) {
081        if (this == obj) {
082            return true;
083        }
084        if (obj == null) {
085            return false;
086        }
087        if (getClass() != obj.getClass()) {
088            return false;
089        }
090        EventChannelsTuple other = (EventChannelsTuple) obj;
091        if (!Arrays.equals(channels, other.channels)) {
092            return false;
093        }
094        if (event == null) {
095            if (other.event != null) {
096                return false;
097            }
098        } else if (!event.equals(other.event)) {
099            return false;
100        }
101        return true;
102    }
103
104    /*
105     * (non-Javadoc)
106     * 
107     * @see java.lang.Object#toString()
108     */
109    @Override
110    public String toString() {
111        StringBuilder builder = new StringBuilder(50);
112        builder.append("EventChannelsTuple [");
113        if (event != null) {
114            builder.append("event=")
115                .append(event)
116                .append(", ");
117        }
118        if (channels != null) {
119            builder.append("channels=")
120                .append(Arrays.toString(channels));
121        }
122        builder.append(']');
123        return builder.toString();
124    }
125
126}