Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e07b0436c57d8aed23c541b07a4d26199a0992b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
 * Copyright (c) 2015 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0, which accompanies this distribution
 * and is available at https://www.eclipse.org/legal/epl-2.0/.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.deque;

import org.eclipse.jpt.common.utility.deque.Deque;

/**
 * Resizable-array implementation of the {@link Deque} interface.
 * @param <E> the type of elements maintained by the deque
 * @see FixedCapacityArrayDeque
 * @see DequeTools
 */
public class ArrayDeque<E>
	extends AbstractArrayDeque<E>
{
	private static final long serialVersionUID = 1L;


	// ********** constructors **********

	/**
	 * Construct an empty deque with the specified initial capacity.
	 */
	public ArrayDeque(int initialCapacity) {
		super(initialCapacity);
	}


	// ********** Deque implementation **********

	@Override
	public void enqueueTail(E element) {
		this.ensureCapacity(this.size + 1);
		super.enqueueTail(element);
	}

	@Override
	public void enqueueHead(E element) {
		this.ensureCapacity(this.size + 1);
		super.enqueueHead(element);
	}

	/**
	 * Increase the deque's capacity, if necessary, to ensure it has at least
	 * the specified minimum capacity.
	 */
	public void ensureCapacity(int minCapacity) {
		int oldCapacity = this.elements.length;
		if (oldCapacity < minCapacity) {
			int newCapacity = ((oldCapacity * 3) >> 1) + 1;
			if (newCapacity < minCapacity) {
				newCapacity = minCapacity;
			}
			this.elements = this.copyElements(newCapacity);
			this.head = 0;
			this.tail = this.size;
		}
	}

	/**
	 * Decrease the deque's capacity, if necessary, to match its current size.
	 */
	public void trimToSize() {
		if (this.elements.length > this.size) {
			this.elements = this.copyElements(this.size);
			this.head = 0;
			this.tail = this.size;
		}
	}


	// ********** standard methods **********

	@Override
	public ArrayDeque<E> clone() {
		return (ArrayDeque<E>) super.clone();
	}
}

Back to the top