Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 239462932d07eadd8f365b721864be1dee6bd879 (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
/*******************************************************************************
 * Copyright (c) 2011, 2017 IBM Corporation and others.
 *
 * 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/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.core;

import java.util.HashMap;
import java.util.Map;

/**
 * An object pool backed by strong references.  Objects stored in this pool
 * will not be garbage collected as they refer to themselves.  The client is responsible for 
 * nulling all references to the pool instance when it is no longer needed so that
 * the contained objects can be garbage collected.  
 * <p>
 * If a long lived, memory managed pool is required use {@link org.eclipse.equinox.p2.core.WeakPool}.
 * </p>
 * @since 2.1
 */
public class StrongPool<T> implements IPool<T> {
	private Map<T, T> pool = new HashMap<>();

	@Override
	public T add(T newObject) {
		if (newObject == null) {
			return null;
		}

		T reference = pool.get(newObject);
		if (reference == null) {
			pool.put(newObject, newObject);
			return newObject;
		}
		return reference;
	}
}

Back to the top