001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.client.event.listener.unlisten;
023
024import de.novanic.eventservice.client.event.domain.Domain;
025
026import java.util.Set;
027
028/**
029 * An UnlistenEvent will be triggered when a timeout or a domain specific unlisten/deregistration occurs. The UnlistenEvent is created by
030 * {@link de.novanic.eventservice.client.event.service.EventService} when unlisten is called for a user. It will also be returned as an
031 * event (from the listen method) and will be added to the UnlistenDomain {@link de.novanic.eventservice.client.event.domain.DomainFactory#UNLISTEN_DOMAIN}.
032 * @see de.novanic.eventservice.client.event.service.EventService#unlisten()
033 * @see de.novanic.eventservice.client.event.service.EventService#unlisten(Domain)
034 * @see de.novanic.eventservice.client.event.service.EventService#unlisten(java.util.Set)
035 *
036 * @author sstrohschein
037 * <br>Date: 05.06.2008
038 * <br>Time: 19:24:45
039 */
040public class DefaultUnlistenEvent implements UnlistenEvent
041{
042    private Set<Domain> myDomains;
043    private String myUserId;
044    private boolean isTimeout;
045    private boolean isLocal;
046
047    /**
048     * Creates an UnlistenEvent for all domains (global). This is useful if the user exits
049     * the browser/application and a timeout occurs.
050     */
051    public DefaultUnlistenEvent() {}
052
053    /**
054     * Creates an UnlistenEvent for a specific domain. That will be created when a user will be deregistered from a domain.
055     * @param aDomains {@link de.novanic.eventservice.client.event.domain.Domain} which are unlistened (in combination with the user id)
056     * @param aUserId user id which is unlistened (in combination with the domain)
057     * @param isTimeout true when the creation of the UnlistenEvent is caused by a timeout
058     */
059    public DefaultUnlistenEvent(Set<Domain> aDomains, String aUserId, boolean isTimeout) {
060        myDomains = aDomains;
061        myUserId = aUserId;
062        this.isTimeout = isTimeout;
063    }
064
065    /**
066     * Creates an UnlistenEvent for a specific domain. That will be created when a user will be deregistered from a domain or when the
067     * UnlistenEvent is triggered from the client side (see {@link UnlistenEvent#isLocal()}).
068     * @param aDomains {@link de.novanic.eventservice.client.event.domain.Domain} which are unlistened (in combination with the user id)
069     * @param aUserId user id which is unlistened (in combination with the domain)
070     * @param isTimeout true when the creation of the UnlistenEvent is caused by a timeout
071     * @param isLocal true when the UnlistenEvent was created from the client side (see {@link UnlistenEvent#isLocal()}).
072     */
073    public DefaultUnlistenEvent(Set<Domain> aDomains, String aUserId, boolean isTimeout, boolean isLocal) {
074        this(aDomains, aUserId, isTimeout);
075        this.isLocal = isLocal;
076    }
077
078    /**
079     * A {@link de.novanic.eventservice.client.event.domain.Domain} can be set to the UnlistenEvent when the unlisten event
080     * is domain specific.
081     * @param aDomains unlistened domains
082     */
083    public void setDomains(Set<Domain> aDomains) {
084        myDomains = aDomains;
085    }
086
087    /**
088     * Returns the domain for which isn't listening anymore. If the UnlistenEvent is global (for example a timeout),
089     * this method returns NULL.
090     * @return unlistened domains
091     */
092    public Set<Domain> getDomains() {
093        return myDomains;
094    }
095
096    /**
097     * Sets the unlistened user id for the UnlistenEvent.
098     * @param aUserId unlistened user id
099     */
100    public void setUserId(String aUserId) {
101        myUserId = aUserId;
102    }
103
104    /**
105     * Returns the unlistened user id for the UnlistenEvent.
106     * @return unlistened user id
107     */
108    public String getUserId() {
109        return myUserId;
110    }
111
112    /**
113     * Returns true when the UnlistenEvent is a timeout, otherwise false (for example a domain specific UnlistenEvent).
114     * @return true when timeout, otherwise false (for example a domain specific UnlistenEvent)
115     */
116    public boolean isTimeout() {
117        return isTimeout;
118    }
119
120    /**
121     * Sets the timeout flag. It should be set true when the UnlistenEvent marks a timeout, otherwise false (for example a domain specific UnlistenEvent).
122     * @param aTimeout true when the UnlistenEvent marks a timeout, otherwise false (for example a domain specific UnlistenEvent)
123     */
124    public void setTimeout(boolean aTimeout) {
125        isTimeout = aTimeout;
126    }
127
128    /**
129     * Returns true when the UnlistenEvent is triggered from the client side. That can for example occur on connection errors.
130     * @return true when triggered from client side, otherwise false
131     */
132    public boolean isLocal() {
133        return isLocal;
134    }
135
136    /**
137     * Sets the local flag. It should be set true when the UnlistenEvent is triggered from the client side.  That can for example
138     * occur on connection errors.
139     * @param isLocal true when triggered from client side, otherwise false
140     */
141    public void setLocal(boolean isLocal) {
142        this.isLocal = isLocal;
143    }
144
145    public boolean equals(Object anObject) {
146        if(this == anObject) {
147            return true;
148        }
149        if(anObject == null || getClass() != anObject.getClass()) {
150            return false;
151        }
152
153        UnlistenEvent theOtherObject = (UnlistenEvent)anObject;
154        if(isLocal != theOtherObject.isLocal()) {
155            return false;
156        }
157        if(isTimeout != theOtherObject.isTimeout()) {
158            return false;
159        }
160        if(myDomains != null ? !myDomains.equals(theOtherObject.getDomains()) : theOtherObject.getDomains() != null) {
161            return false;
162        }
163        if(myUserId != null ? !myUserId.equals(theOtherObject.getUserId()) : theOtherObject.getUserId() != null) {
164            return false;
165        }
166        return true;
167    }
168
169    public int hashCode() {
170        int theResult = myDomains != null ? myDomains.hashCode() : 0;
171        theResult = 31 * theResult + (myUserId != null ? myUserId.hashCode() : 0);
172        theResult = 31 * theResult + (isTimeout ? 1 : 0);
173        theResult = 31 * theResult + (isLocal ? 1 : 0);
174        return theResult;
175    }
176
177    public String toString() {
178        final StringBuilder theStringBuilder = new StringBuilder(150);
179        theStringBuilder.append("Event: Unlisten");
180        if(isTimeout) {
181            theStringBuilder.append("(timeout)");
182        } else if(isLocal) {
183            theStringBuilder.append("(local)");
184        }
185
186        if(myUserId != null) {
187            theStringBuilder.append(" (user \"");
188            theStringBuilder.append(myUserId);
189        } else {
190            theStringBuilder.append(" (user ");
191            theStringBuilder.append("not available");
192        }
193
194        if(myDomains != null && !myDomains.isEmpty()) {
195            final int theDomainCount = myDomains.size();
196            if(theDomainCount == 1) {
197                theStringBuilder.append("\" for domain \"");
198                theStringBuilder.append(myDomains.iterator().next());
199                theStringBuilder.append("\")");
200            } else if(theDomainCount > 1) {
201                theStringBuilder.append("\" for ");
202                theStringBuilder.append(theDomainCount);
203                theStringBuilder.append(" domains)");
204            }
205        } else if(myUserId != null) {
206            theStringBuilder.append("\")");
207        } else {
208            theStringBuilder.append(')');
209        }
210        return theStringBuilder.toString();
211    }
212}