diff options
author | Lyubomir Marinov <lyubomir.marinov@jitsi.org> | 2009-04-28 17:58:22 +0000 |
---|---|---|
committer | Lyubomir Marinov <lyubomir.marinov@jitsi.org> | 2009-04-28 17:58:22 +0000 |
commit | 2d35a92e12a442852ab3d50dbfb028c39b3b9781 (patch) | |
tree | 593c2b6a9c1e4168a8b68776e6848b04f8c0f2c0 /src | |
parent | b17dd2bc6d9c034b89c89a4675578bbc2030d2dd (diff) | |
download | jitsi-2d35a92e12a442852ab3d50dbfb028c39b3b9781.zip jitsi-2d35a92e12a442852ab3d50dbfb028c39b3b9781.tar.gz jitsi-2d35a92e12a442852ab3d50dbfb028c39b3b9781.tar.bz2 |
Provides a tiny Map-iteration optimization, removes duplication, minor code simplifications.
Diffstat (limited to 'src')
3 files changed, 93 insertions, 202 deletions
diff --git a/src/net/java/sip/communicator/impl/contactlist/MclStorageManager.java b/src/net/java/sip/communicator/impl/contactlist/MclStorageManager.java index e75e21e..6877b3d 100644 --- a/src/net/java/sip/communicator/impl/contactlist/MclStorageManager.java +++ b/src/net/java/sip/communicator/impl/contactlist/MclStorageManager.java @@ -44,8 +44,8 @@ import net.java.sip.communicator.util.xml.XMLUtils; public class MclStorageManager implements MetaContactListListener { - private static final Logger logger = - Logger.getLogger(MclStorageManager.class.getName()); + private static final Logger logger + = Logger.getLogger(MclStorageManager.class); /** * Indicates whether the storage manager has been properly started or in @@ -156,8 +156,8 @@ public class MclStorageManager "parent-proto-group-uid"; /** - * The name of the XML attribute that contains account identifers indicating - * proto group's and proto contacts' owning providers. + * The name of the XML attribute that contains account identifiers + * indicating proto group's and proto contacts' owning providers. */ private static final String ACCOUNT_ID_ATTR_NAME = "account-id"; @@ -289,10 +289,9 @@ public class MclStorageManager { contactlistFile = faService.getPrivatePersistentFile(fileName); - if (!contactlistFile.exists()) - if (!contactlistFile.createNewFile()) - throw new IOException("Failed to create file" - + contactlistFile.getAbsolutePath()); + if (!contactlistFile.exists() && !contactlistFile.createNewFile()) + throw new IOException("Failed to create file" + + contactlistFile.getAbsolutePath()); } catch (Exception ex) { @@ -467,22 +466,11 @@ public class MclStorageManager } /** - * Returns the object that we use to lock when writing the contact list. - * - * @return the object that we use to lock when writing the contact list. - */ - public Object getContactListRWLock() - { - return contactListRWLock; - } - - /** * Stops the storage manager and performs a final write */ public void storeContactListAndStopStorageManager() { - Object contactLstRWLock = getContactListRWLock(); - synchronized (contactLstRWLock) + synchronized (contactListRWLock) { if (!isStarted()) return; @@ -490,7 +478,7 @@ public class MclStorageManager started = false; // make sure everyone gets released after we finish. - contactLstRWLock.notifyAll(); + contactListRWLock.notifyAll(); // write the contact list ourselves before we go out.. try @@ -1560,11 +1548,11 @@ public class MclStorageManager Element metaContactNode = findMetaContactNode(evt.getSourceMetaContact().getMetaUID()); - // not sure what to do in case of null. we'll be loggin an internal err + // not sure what to do in case of null. we'll be logging an internal err // for now and that's all. if (metaContactNode == null) { - logger.error("Save after renam failed. Contact not found: " + logger.error("Save after rename failed. Contact not found: " + evt.getSourceMetaContact()); return; } @@ -2027,15 +2015,14 @@ public class MclStorageManager private static StoredProtoContactDescriptor findContactInList( String contactAddress, List<StoredProtoContactDescriptor> list) { - if (list == null || list.size() == 0) - return null; - - for (StoredProtoContactDescriptor desc : list) + if (list != null && list.size() > 0) { - if (desc.contactAddress.equals(contactAddress)) - return desc; + for (StoredProtoContactDescriptor desc : list) + { + if (desc.contactAddress.equals(contactAddress)) + return desc; + } } - return null; } } diff --git a/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java b/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java index 19a743f..77d1849 100644 --- a/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java +++ b/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java @@ -31,8 +31,8 @@ public class MetaContactListServiceImpl ServiceListener, ContactPresenceStatusListener { - private static final Logger logger = Logger - .getLogger(MetaContactListServiceImpl.class); + private static final Logger logger + = Logger.getLogger(MetaContactListServiceImpl.class); /** * The BundleContext that we got from the OSGI bus. @@ -74,7 +74,7 @@ public class MetaContactListServiceImpl * Listeners interested in events dispatched upon modification of the meta * contact list. */ - private final Vector<MetaContactListListener> metaContactListListeners + private final List<MetaContactListListener> metaContactListListeners = new Vector<MetaContactListListener>(); /** @@ -146,7 +146,7 @@ public class MetaContactListServiceImpl logger.debug("Starting the meta contact list implementation."); this.bundleContext = bc; - //initializne the meta contact list from what has been stored locally. + //initialize the meta contact list from what has been stored locally. try { storageManager.start(bundleContext, this); @@ -184,10 +184,10 @@ public class MetaContactListServiceImpl logger.debug("Found " + protocolProviderRefs.length + " already installed providers."); - for (int i = 0; i < protocolProviderRefs.length; i++) + for (ServiceReference providerRef : protocolProviderRefs) { - ProtocolProviderService provider = (ProtocolProviderService) bc - .getService(protocolProviderRefs[i]); + ProtocolProviderService provider + = (ProtocolProviderService) bc.getService(providerRef); this.handleProviderAdded(provider); } @@ -205,13 +205,8 @@ public class MetaContactListServiceImpl bc.removeServiceListener(this); //stop listening to all currently installed providers - Iterator<ProtocolProviderService> providers - = this.currentlyInstalledProviders.values().iterator(); - - while (providers.hasNext()) + for (ProtocolProviderService pp : currentlyInstalledProviders.values()) { - ProtocolProviderService pp = providers.next(); - OperationSetPersistentPresence opSetPersPresence = (OperationSetPersistentPresence)pp .getOperationSet(OperationSetPersistentPresence.class); @@ -251,7 +246,7 @@ public class MetaContactListServiceImpl synchronized (metaContactListListeners) { if(!metaContactListListeners.contains(listener)) - this.metaContactListListeners.add(listener); + metaContactListListeners.add(listener); } } @@ -780,7 +775,7 @@ public class MetaContactListServiceImpl ((MetaContactImpl)metaContact).setDisplayName(newDisplayName); - fireMetaContactPropertyChangeEvent(new MetaContactRenamedEvent( + fireMetaContactEvent(new MetaContactRenamedEvent( metaContact, oldDisplayName, newDisplayName)); //changing the display name has surely brought a change in the order as @@ -790,7 +785,6 @@ public class MetaContactListServiceImpl , null , null , MetaContactGroupEvent.CHILD_CONTACTS_REORDERED); - } /** @@ -816,10 +810,8 @@ public class MetaContactListServiceImpl byte[] oldAvatar = metaContact.getAvatar(true); ((MetaContactImpl)metaContact).cacheAvatar(protoContact, newAvatar); - fireMetaContactPropertyChangeEvent(new MetaContactAvatarUpdate( - metaContact, - oldAvatar, - newAvatar)); + fireMetaContactEvent( + new MetaContactAvatarUpdate(metaContact, oldAvatar, newAvatar)); } /** @@ -1859,17 +1851,16 @@ public class MetaContactListServiceImpl Contact contact, ProtocolProviderService ownerProvider) { - Enumeration<String> ignoredAddressList = contactEventIgnoreList.keys(); - - while(ignoredAddressList.hasMoreElements()) + for (Map.Entry<String, List<ProtocolProviderService>> contactEventIgnoreEntry + : contactEventIgnoreList.entrySet()) { - String contactAddress = ignoredAddressList.nextElement(); + String contactAddress = contactEventIgnoreEntry.getKey(); if(contact.getAddress().equals(contactAddress) || contact.equals(contactAddress)) { - List<ProtocolProviderService> existingProvList = - this.contactEventIgnoreList.get(contactAddress); + List<ProtocolProviderService> existingProvList + = contactEventIgnoreEntry.getValue(); return existingProvList != null && existingProvList.contains(ownerProvider); @@ -1944,9 +1935,9 @@ public class MetaContactListServiceImpl = event.getServiceReference().getBundle() .getRegisteredServices(); - for (int i = 0; i < allBundleServices.length; i++) + for (ServiceReference bundleServiceRef : allBundleServices) { - Object service = bundleContext.getService(allBundleServices[i]); + Object service = bundleContext.getService(bundleServiceRef); if(service instanceof ProtocolProviderFactory) { sourceFactory = (ProtocolProviderFactory) service; @@ -2453,8 +2444,6 @@ public class MetaContactListServiceImpl //protocol specific contact group. Contrary to contacts, meta //contact groups are to only be remove upon user indication or //otherwise it would be difficult for a user to create a new grp. - - } /** @@ -2465,7 +2454,6 @@ public class MetaContactListServiceImpl */ public void groupNameChanged(ServerStoredGroupEvent evt) { - logger.trace("ContactGroup renamed: " + evt); MetaContactGroup metaContactGroup @@ -2498,17 +2486,8 @@ public class MetaContactListServiceImpl logger.trace("Will dispatch the following mcl event: " + evt); - Iterator<MetaContactListListener> listeners = null; - synchronized (metaContactListListeners) - { - listeners = new ArrayList<MetaContactListListener>( - metaContactListListeners).iterator(); - } - - while (listeners.hasNext()) + for (MetaContactListListener listener : getMetaContactListListeners()) { - MetaContactListListener listener = listeners.next(); - switch (evt.getEventID()) { case MetaContactEvent.META_CONTACT_ADDED: @@ -2524,41 +2503,26 @@ public class MetaContactListServiceImpl } /** - * Creates the corresponding <tt>MetaContactPropertyChangeEvent</tt> - * instance and notifies all <tt>MetaContactListListener</tt>s that a - * MetaContact has been modified. + * Gets a copy of the list of current <code>MetaContactListListener</code> + * interested in events fired by this instance. * - * @param event the event to dispatch. + * @return an array of <code>MetaContactListListener</code>s currently + * interested in events fired by this instance. The returned array + * is a copy of the internal listener storage and thus can be safely + * modified. */ - void fireMetaContactEvent(MetaContactPropertyChangeEvent event) + private MetaContactListListener[] getMetaContactListListeners() { - logger.trace("Will dispatch the following mcl property change event: " - + event); + MetaContactListListener[] listeners; - Iterator<MetaContactListListener> listeners = null; synchronized (metaContactListListeners) { - listeners = new ArrayList<MetaContactListListener>( - metaContactListListeners).iterator(); - } - - while (listeners.hasNext()) - { - MetaContactListListener listener = listeners.next(); - - if (event instanceof MetaContactMovedEvent) - { - listener.metaContactMoved( (MetaContactMovedEvent) event); - } - else if (event instanceof MetaContactRenamedEvent) - { - listener.metaContactRenamed( (MetaContactRenamedEvent) event); - } - else if (event instanceof MetaContactModifiedEvent) - { - listener.metaContactModified( (MetaContactModifiedEvent) event); - } + listeners + = metaContactListListeners.toArray( + new MetaContactListListener[ + metaContactListListeners.size()]); } + return listeners; } /** @@ -2568,23 +2532,13 @@ public class MetaContactListServiceImpl * * @param event the event to dispatch. */ - private void fireMetaContactPropertyChangeEvent( - MetaContactPropertyChangeEvent event) + void fireMetaContactEvent(MetaContactPropertyChangeEvent event) { logger.trace("Will dispatch the following mcl property change event: " + event); - Iterator<MetaContactListListener> listeners = null; - synchronized (metaContactListListeners) - { - listeners = new ArrayList<MetaContactListListener>( - metaContactListListeners).iterator(); - } - - while (listeners.hasNext()) + for (MetaContactListListener listener : getMetaContactListListeners()) { - MetaContactListListener listener = listeners.next(); - if (event instanceof MetaContactMovedEvent) { listener.metaContactMoved( (MetaContactMovedEvent) event); @@ -2593,6 +2547,10 @@ public class MetaContactListServiceImpl { listener.metaContactRenamed( (MetaContactRenamedEvent) event); } + else if (event instanceof MetaContactModifiedEvent) + { + listener.metaContactModified( (MetaContactModifiedEvent) event); + } } } @@ -2622,17 +2580,8 @@ public class MetaContactListServiceImpl logger.trace("Will dispatch the following mcl property change event: " + event); - Iterator<MetaContactListListener> listeners = null; - synchronized (metaContactListListeners) - { - listeners = new ArrayList<MetaContactListListener>( - metaContactListListeners).iterator(); - } - - while (listeners.hasNext()) + for (MetaContactListListener listener : getMetaContactListListeners()) { - MetaContactListListener listener = listeners.next(); - if (eventName.equals(ProtoContactEvent.PROTO_CONTACT_ADDED)) { listener.protoContactAdded(event); @@ -2761,7 +2710,6 @@ public class MetaContactListServiceImpl containingMetaGroup.addProtoGroup(newProtoGroup); return newProtoGroup; - } /** @@ -2803,17 +2751,9 @@ public class MetaContactListServiceImpl (OperationSetPersistentPresence) sourceProvider .getOperationSet(OperationSetPersistentPresence.class); - Iterator<MclStorageManager.StoredProtoContactDescriptor> contactsIter - = protoContacts.iterator(); - while (contactsIter.hasNext()) + for (MclStorageManager.StoredProtoContactDescriptor contactDescriptor + : protoContacts) { - MclStorageManager.StoredProtoContactDescriptor contactDescriptor - = contactsIter.next(); - - if(contactDescriptor.contactAddress.indexOf("238431632") > -1) - logger.debug("asdfasdfasdfasdfasdfasdfasdf"); - - //this contact has already been registered by another meta contact //so we'll ignore it. If this is the only contact in the meta //contact, we'll throw an exception at the end of the method and @@ -2831,7 +2771,6 @@ public class MetaContactListServiceImpl continue; } - Contact protoContact = presenceOpSet.createUnresolvedContact( contactDescriptor.contactAddress, contactDescriptor.persistentData, @@ -2883,19 +2822,8 @@ public class MetaContactListServiceImpl logger.trace("Will dispatch the following mcl event: " + evt); - - - Iterator<MetaContactListListener> listeners = null; - synchronized (metaContactListListeners) - { - listeners = new ArrayList<MetaContactListListener>( - metaContactListListeners).iterator(); - } - - while (listeners.hasNext()) + for (MetaContactListListener listener : getMetaContactListListeners()) { - MetaContactListListener listener = listeners.next(); - switch (eventID) { case MetaContactGroupEvent.META_CONTACT_GROUP_ADDED: @@ -2951,15 +2879,12 @@ public class MetaContactListServiceImpl * @param evt a ServerStoredGroupChangeEvent containing a reference to * the newly created group. */ - public void groupCreated(ServerStoredGroupEvent event) + public synchronized void groupCreated(ServerStoredGroupEvent event) { - synchronized (this) + if (event.getSourceGroup().getGroupName().equals(groupName)) { - if (event.getSourceGroup().getGroupName().equals(groupName)) - { - this.evt = event; - this.notifyAll(); - } + this.evt = event; + this.notifyAll(); } } @@ -2990,24 +2915,19 @@ public class MetaContactListServiceImpl * @param millis the number of millis that we should wait before we * determine failure. */ - public void waitForEvent(long millis) + public synchronized void waitForEvent(long millis) { - synchronized (this) + //no need to wait if an event is already there. + if (evt == null) { - //no need to wait if an event is already there. - if (evt != null) - { - return; - } - try { this.wait(millis); } catch (InterruptedException ex) { - logger.error("Interrupted while waiting for group creation" - , ex); + logger.error("Interrupted while waiting for group creation", + ex); } } } @@ -3064,20 +2984,16 @@ public class MetaContactListServiceImpl * @param event a ServerStoredGroupEvent containing a reference to the * newly created group. */ - public void groupCreated(ServerStoredGroupEvent event) + public synchronized void groupCreated(ServerStoredGroupEvent event) { - synchronized (this) + Contact contact + = event.getSourceGroup().getContact(subscriptionAddress); + if ( contact != null) { - Contact contact - = event.getSourceGroup().getContact(subscriptionAddress); - if ( contact != null) - { - this.evt = event; - this.sourceContact = contact; - this.notifyAll(); - } + this.evt = event; + this.sourceContact = contact; + this.notifyAll(); } - } /** @@ -3086,18 +3002,15 @@ public class MetaContactListServiceImpl * @param event a <tt>SubscriptionEvent</tt> containing a reference to * the newly created contact. */ - public void subscriptionCreated(SubscriptionEvent event) + public synchronized void subscriptionCreated(SubscriptionEvent event) { - synchronized (this) - { - if (event.getSourceContact().getAddress() + if (event.getSourceContact().getAddress() .equals(subscriptionAddress) - || event.getSourceContact().equals(subscriptionAddress)) - { - this.evt = event; - this.sourceContact = event.getSourceContact(); - this.notifyAll(); - } + || event.getSourceContact().equals(subscriptionAddress)) + { + this.evt = event; + this.sourceContact = event.getSourceContact(); + this.notifyAll(); } } @@ -3114,17 +3027,14 @@ public class MetaContactListServiceImpl * @param event a <tt>SubscriptionEvent</tt> containing a reference to * the contact we are trying to subscribe. */ - public void subscriptionFailed(SubscriptionEvent event) + public synchronized void subscriptionFailed(SubscriptionEvent event) { - synchronized (this) - { - if (event.getSourceContact().getAddress() + if (event.getSourceContact().getAddress() .equals(subscriptionAddress)) - { - this.evt = event; - this.sourceContact = event.getSourceContact(); - this.notifyAll(); - } + { + this.evt = event; + this.sourceContact = event.getSourceContact(); + this.notifyAll(); } } @@ -3154,16 +3064,11 @@ public class MetaContactListServiceImpl * created event is received or milis miliseconds pass. * @param millis the number of milis to wait upon determining a failure. */ - public void waitForEvent(long millis) + public synchronized void waitForEvent(long millis) { - synchronized (this) + //no need to wait if an event is already there. + if (evt == null) { - //no need to wait if an event is already there. - if (evt != null) - { - return; - } - try { this.wait(millis); @@ -3177,5 +3082,4 @@ public class MetaContactListServiceImpl } } } - } diff --git a/src/net/java/sip/communicator/service/contactlist/event/MetaContactListListener.java b/src/net/java/sip/communicator/service/contactlist/event/MetaContactListListener.java index 6cd86b9..c5bce07 100644 --- a/src/net/java/sip/communicator/service/contactlist/event/MetaContactListListener.java +++ b/src/net/java/sip/communicator/service/contactlist/event/MetaContactListListener.java @@ -108,7 +108,7 @@ public interface MetaContactListListener /** * Indicates that the order under which the child contacts were ordered * inside the source group has changed. - * @param evt the <tt>MetaContactGroupEvent</tt> containind details of this + * @param evt the <tt>MetaContactGroupEvent</tt> containing details of this * event. */ public void childContactsReordered(MetaContactGroupEvent evt); |