view dwtx/core/commands/common/HandleObjectManager.d @ 3:6518c18a01f7

eclipse.core package without osgi dependencies
author Frank Benoit <benoit@tionex.de>
date Wed, 26 Mar 2008 00:57:19 +0100
parents
children 04b47443bb01
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2005, 2006 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
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/

module dwtx.core.commands.common.HandleObjectManager;

import dwtx.core.commands.common.HandleObject;
import dwtx.core.commands.common.EventManager;
import dwt.dwthelper.utils;

import tango.util.collection.HashSet;
import tango.util.collection.HashMap;
import tango.util.collection.model.Set;
import tango.util.collection.model.Map;

// import java.util.HashMap;
// import java.util.HashSet;
// import java.util.Iterator;
// import java.util.Map;
// import java.util.Set;

/**
 * <p>
 * A manager of {@link HandleObject} instances. This has some common behaviour
 * which is shared between all such managers.
 * </p>
 * <p>
 * Clients may extend.
 * </p>
 *
 * @since 3.2
 */
public abstract class HandleObjectManager : EventManager {

    /**
     * The set of handle objects that are defined. This value may be empty, but
     * it is never <code>null</code>.
     */
    protected const Set!(Object) definedHandleObjects;

    /**
     * The map of identifiers (<code>String</code>) to handle objects (
     * <code>HandleObject</code>). This collection may be empty, but it is
     * never <code>null</code>.
     */
    protected const Map!( String, Object ) handleObjectsById;

    public this(){
        definedHandleObjects = new HashSet!(Object)();
        handleObjectsById = new HashMap!( String, Object )();
    }

    /**
     * Verifies that the identifier is valid. Exceptions will be thrown if the
     * identifier is invalid in some way.
     *
     * @param id
     *            The identifier to validate; may be anything.
     */
    protected final void checkId(String id) {
        if (id is null) {
            throw new NullPointerException(
                    "A handle object may not have a null identifier"); //$NON-NLS-1$
        }

        if (id.length < 1) {
            throw new IllegalArgumentException(
                    "The handle object must not have a zero-length identifier"); //$NON-NLS-1$
        }
    }

    /**
     * Returns the set of identifiers for those handle objects that are defined.
     *
     * @return The set of defined handle object identifiers; this value may be
     *         empty, but it is never <code>null</code>.
     */
    protected final HashSet!(String) getDefinedHandleObjectIds() {
        HashSet!(String) definedHandleObjectIds = new HashSet!(String)(/+definedHandleObjects
                .size()+/);
        foreach ( v; definedHandleObjects.elements() ) {
            auto handleObject = cast(HandleObject)v;
            String id = handleObject.getId();
            definedHandleObjectIds.add(id);
        }
        return definedHandleObjectIds;
    }
}