view org.eclipse.core.databinding.observable/src/org/eclipse/core/databinding/observable/map/AbstractObservableMap.d @ 96:b74ac5dfcc06

package to module and java.lang.all import
author Frank Benoit <benoit@tionex.de>
date Tue, 21 Apr 2009 11:03:33 +0200
parents 6208d4f6a277
children c86eb8b3098e
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Brad Reynolds - bug 164653
 *     Matthew Hall - bugs 118516, 240931
 *******************************************************************************/

module org.eclipse.core.databinding.observable.map.AbstractObservableMap;

import java.lang.all;

import java.util.AbstractMap;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.ChangeSupport;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.IStaleListener;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.StaleEvent;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.AssertionFailedException;

/**
 * 
 * <p>
 * This class is thread safe. All state accessing methods must be invoked from
 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
 * listeners may be invoked from any thread.
 * </p>
 * @since 1.0
 */
public abstract class AbstractObservableMap : AbstractMap ,
        IObservableMap {

    private ChangeSupport changeSupport;

    private bool stale;

    /**
     */
    public this() {
        this(Realm.getDefault());
    }

    /**
     * 
     */
    protected void lastListenerRemoved() {
    }

    /**
     * 
     */
    protected void firstListenerAdded() {
    }

    /**
     * @param realm
     */
    public this(Realm realm) {
        Assert.isNotNull(realm, "Realm cannot be null"); //$NON-NLS-1$
        changeSupport = new class(realm) ChangeSupport {
            protected void firstListenerAdded() {
                this.outer.firstListenerAdded();
            }
            protected void lastListenerRemoved() {
                this.outer.lastListenerRemoved();
            }
        };
    }

    public synchronized void addMapChangeListener(IMapChangeListener listener) {
        if (changeSupport !is null) {
            changeSupport.addListener(MapChangeEvent.TYPE, listener);
        }
    }

    public synchronized void removeMapChangeListener(IMapChangeListener listener) {
        if (changeSupport !is null) {
            changeSupport.removeListener(MapChangeEvent.TYPE, listener);
        }
    }

    public synchronized void addChangeListener(IChangeListener listener) {
        if (changeSupport !is null) {
            changeSupport.addChangeListener(listener);
        }
    }

    public synchronized void addStaleListener(IStaleListener listener) {
        if (changeSupport !is null) {
            changeSupport.addStaleListener(listener);
        }
    }

    public synchronized void dispose() {
        if (changeSupport !is null) {
            changeSupport.dispose();
            changeSupport = null;
        }
    }

    public Realm getRealm() {
        if (changeSupport !is null) {
            return changeSupport.getRealm();
        }
        return null;
    }

    public bool isStale() {
        checkRealm();
        return stale;
    }

    public synchronized void removeChangeListener(IChangeListener listener) {
        if (changeSupport !is null) {
            changeSupport.removeChangeListener(listener);
        }
    }

    public synchronized void removeStaleListener(IStaleListener listener) {
        if (changeSupport !is null) {
            changeSupport.removeStaleListener(listener);
        }
    }

    /**
     * Sets the stale state.  Must be invoked from the current realm.
     * 
     * @param stale
     */
    public void setStale(bool stale) {
        checkRealm();
        this.stale = stale;
        if (stale) {
            fireStale();
        }
    }

    /**
     * Fires stale events.  Must be invoked from current realm.
     */
    protected void fireStale() {
        checkRealm();
        changeSupport.fireEvent(new StaleEvent(this));
    }

    /**
     * Fires change events.  Must be invoked from current realm.
     */
    protected void fireChange() {
        checkRealm();
        changeSupport.fireEvent(new ChangeEvent(this));
    }

    /**
     * Fires map change events.  Must be invoked from current realm.
     * 
     * @param diff
     */
    protected void fireMapChange(MapDiff diff) {
        checkRealm();
        changeSupport.fireEvent(new MapChangeEvent(this, diff));
    }

    /**
     * Asserts that the realm is the current realm.
     * 
     * @see Realm#isCurrent()
     * @throws AssertionFailedException
     *             if the realm is not the current realm
     */
    protected void checkRealm() {
        Assert.isTrue(getRealm().isCurrent(),
                "This operation must be run within the observable's realm"); //$NON-NLS-1$
    }
}