view org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/Queue.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2007, 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
 *******************************************************************************/
module org.eclipse.core.internal.databinding.Queue;

import java.lang.all;

/**
 * Created to avoid a dependency on java.util.LinkedList, see bug 205224.
 * 
 * @since 1.1
 * 
 */
public class Queue {

    static class Entry {
        Object object;

        this(Object o) {
            this.object = o;
        }

        Entry next;
    }

    Entry first;
    Entry last;

    /**
     * Adds the given object to the end of the queue.
     * 
     * @param o
     */
    public void enqueue(Object o) {
        Entry oldLast = last;
        last = new Entry(o);
        if (oldLast !is null) {
            oldLast.next = last;
        } else {
            first = last;
        }
    }

    /**
     * Returns the first object in the queue. The queue must not be empty.
     * 
     * @return the first object
     */
    public Object dequeue() {
        Entry oldFirst = first;
        if (oldFirst is null) {
            throw new IllegalStateException();
        }
        first = oldFirst.next;
        if (first is null) {
            last = null;
        }
        oldFirst.next = null;
        return oldFirst.object;
    }

    /**
     * Returns <code>true</code> if the list is empty.
     * 
     * @return <code>true</code> if the list is empty
     */
    public bool isEmpty() {
        return first is null;
    }
}