001    /**
002     * <copyright>
003     *
004     * Copyright (c) 2002-2006 IBM Corporation and others.
005     * All rights reserved.   This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors: 
011     *   IBM - Initial API and implementation
012     *
013     * </copyright>
014     *
015     * $Id: NotificationChainImpl.java,v 1.4 2007/06/12 20:56:17 emerks Exp $
016     */
017    package org.eclipse.emf.common.notify.impl;
018    
019    
020    import org.eclipse.emf.common.notify.Notification;
021    import org.eclipse.emf.common.notify.NotificationChain;
022    import org.eclipse.emf.common.notify.Notifier;
023    import org.eclipse.emf.common.util.BasicEList;
024    
025    
026    /**
027     * A list that acts as a notification chain.
028     */
029    public class NotificationChainImpl extends BasicEList<Notification> implements NotificationChain
030    {
031      private static final long serialVersionUID = 1L;
032    
033      /**
034       * Creates an empty instance.
035       */
036      public NotificationChainImpl()
037      {
038        super();
039      }
040    
041      /**
042       * Creates an empty instance with a given capacity.
043       * @param initialCapacity the initial capacity of the list before it must grow.
044       */
045      public NotificationChainImpl(int initialCapacity)
046      {
047        super(initialCapacity);
048      }
049    
050      /**
051       * Returns new data storage of type {@link Notification}[].
052       * @return new data storage.
053       */
054      @Override
055      protected Object [] newData(int capacity)
056      {
057        return new Notification [capacity];
058      }
059    
060      /**
061       * Adds or merges a new notification.
062       * @param newNotification a notification.
063       * @return <code>true</code> when the notification is added and <code>false</code> when it is merged.
064       */
065      @Override
066      public boolean add(Notification newNotification)
067      {
068        if (newNotification == null)
069        {
070          return false;
071        }
072        else 
073        {
074          for (int i = 0; i < size; ++i)
075          {
076            Notification notification = (Notification)data[i];
077            if (notification.merge(newNotification))
078            {
079              return false;
080            }
081          }
082    
083          return super.add(newNotification);
084        }
085      }
086    
087      public void dispatch()
088      {
089        for (int i = 0; i < size; ++i)
090        {
091          Notification notification = (Notification)data[i];
092          dispatch(notification);
093        }
094      }
095    
096      /**
097       * Dispatches the notification to its notifier.
098       */
099      protected void dispatch(Notification notification)
100      {
101        Object notifier = notification.getNotifier();
102        if (notifier != null && notification.getEventType() != -1)
103        {
104          ((Notifier)notifier).eNotify(notification);
105        }
106      }
107    }