/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.java.sip.communicator.service.contactsource; import java.util.*; /** * Provides an abstract implementation of the basic functionality of * ContactQuery and allows extenders to focus on the specifics of their * implementation. * * @author Lyubomir Marinov * @param the very type of ContactSourceService which performs the * ContactQuery */ public abstract class AbstractContactQuery implements ContactQuery { /** * The ContactSourceService which is performing this * ContactQuery. */ private final T contactSource; /** * The List of ContactQueryListeners which are to be * notified by this ContactQuery about changes in its status, the * receipt of new ContactSources via this ContactQuery, * etc. */ private final List listeners = new LinkedList(); /** * The status of this ContactQuery which is one of the * QUERY_XXX constants defined by the ContactQuery class. */ private int status = QUERY_IN_PROGRESS; /** * Initializes a new AbstractContactQuery which is to be performed * by a specific ContactSourceService. The status of the new * instance is {@link ContactQuery#QUERY_IN_PROGRESS}. * * @param contactSource the ContactSourceService which is to * perform the new AbstractContactQuery */ protected AbstractContactQuery(T contactSource) { this.contactSource = contactSource; } /** * Adds a ContactQueryListener to the list of listeners interested * in notifications about this ContactQuery changing its status, * the receipt of new SourceContacts via this * ContactQuery, etc. * * @param l the ContactQueryListener to be added to the list of * listeners interested in the notifications raised by this * ContactQuery * @see ContactQuery#addContactQueryListener(ContactQueryListener) */ public void addContactQueryListener(ContactQueryListener l) { if (l == null) throw new NullPointerException("l"); else { synchronized (listeners) { if (!listeners.contains(l)) listeners.add(l); } } } /** * Cancels this ContactQuery. * * @see ContactQuery#cancel() */ public void cancel() { if (getStatus() == QUERY_IN_PROGRESS) setStatus(QUERY_CANCELED); } /** * Notifies the ContactQueryListeners registered with this * ContactQuery that a new SourceContact has been * received. * * @param contact the SourceContact which has been received and * which the registered ContactQueryListeners are to be notified * about * @param showMoreEnabled indicates whether show more label should be shown * or not. */ protected void fireContactReceived(SourceContact contact, boolean showMoreEnabled) { ContactQueryListener[] ls; synchronized (listeners) { ls = listeners.toArray(new ContactQueryListener[listeners.size()]); } ContactReceivedEvent ev = new ContactReceivedEvent(this, contact, showMoreEnabled); for (ContactQueryListener l : ls) { l.contactReceived(ev); } } /** * Notifies the ContactQueryListeners registered with this * ContactQuery that a new SourceContact has been * received. * * @param contact the SourceContact which has been received and * which the registered ContactQueryListeners are to be notified * about */ protected void fireContactReceived(SourceContact contact) { fireContactReceived(contact, true); } /** * Notifies the ContactQueryListeners registered with this * ContactQuery that a SourceContact has been * removed. * * @param contact the SourceContact which has been removed and * which the registered ContactQueryListeners are to be notified * about */ protected void fireContactRemoved(SourceContact contact) { ContactQueryListener[] ls; synchronized (listeners) { ls = listeners.toArray(new ContactQueryListener[listeners.size()]); } ContactRemovedEvent ev = new ContactRemovedEvent(this, contact); for (ContactQueryListener l : ls) l.contactRemoved(ev); } /** * Notifies the ContactQueryListeners registered with this * ContactQuery that a SourceContact has been * changed. * * @param contact the SourceContact which has been changed and * which the registered ContactQueryListeners are to be notified * about */ protected void fireContactChanged(SourceContact contact) { ContactQueryListener[] ls; synchronized (listeners) { ls = listeners.toArray(new ContactQueryListener[listeners.size()]); } ContactChangedEvent ev = new ContactChangedEvent(this, contact); for (ContactQueryListener l : ls) l.contactChanged(ev); } /** * Notifies the ContactQueryListeners registered with this * ContactQuery that its state has changed. * * @param eventType the type of the ContactQueryStatusEvent to be * fired which can be one of the QUERY_XXX constants defined by * ContactQueryStatusEvent */ protected void fireQueryStatusChanged(int eventType) { ContactQueryListener[] ls; synchronized (listeners) { ls = listeners.toArray(new ContactQueryListener[listeners.size()]); } ContactQueryStatusEvent ev = new ContactQueryStatusEvent(this, eventType); for (ContactQueryListener l : ls) l.queryStatusChanged(ev); } /** * Gets the ContactSourceService which is performing this * ContactQuery. * * @return the ContactSourceService which is performing this * ContactQuery * @see ContactQuery#getContactSource() */ public T getContactSource() { return contactSource; } /** * Gets the status of this ContactQuery which can be one of the * QUERY_XXX constants defined by ContactQuery. * * @return the status of this ContactQuery which can be one of the * QUERY_XXX constants defined by ContactQuery * @see ContactQuery#getStatus() */ public int getStatus() { return status; } /** * Removes a ContactQueryListener from the list of listeners * interested in notifications about this ContactQuery changing its * status, the receipt of new SourceContacts via this * ContactQuery, etc. * * @param l the ContactQueryListener to be removed from the list of * listeners interested in notifications raised by this ContactQuery * @see ContactQuery#removeContactQueryListener(ContactQueryListener) */ public void removeContactQueryListener(ContactQueryListener l) { if (l != null) { synchronized (listeners) { listeners.remove(l); } } } /** * Sets the status of this ContactQuery. * * @param status {@link ContactQuery#QUERY_CANCELED}, * {@link ContactQuery#QUERY_COMPLETED}, or * {@link ContactQuery#QUERY_ERROR} */ public void setStatus(int status) { if (this.status != status) { int eventType; switch (status) { case QUERY_CANCELED: eventType = ContactQueryStatusEvent.QUERY_CANCELED; break; case QUERY_COMPLETED: eventType = ContactQueryStatusEvent.QUERY_COMPLETED; break; case QUERY_ERROR: eventType = ContactQueryStatusEvent.QUERY_ERROR; break; case QUERY_IN_PROGRESS: default: throw new IllegalArgumentException("status"); } this.status = status; fireQueryStatusChanged(eventType); } } }