1
/*******************************************************************************
2
* Copyright (c) 2009, 2011 QNX Software Systems
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* QNX Software Systems (Alena Laskavaia) - initial API and implementation
10
* Sergey Prigogin (Google)
11
*******************************************************************************/
12
package org.eclipse.cdt.codan.internal.core;
14
import java.util.HashMap;
17
import org.eclipse.cdt.codan.core.model.ICheckerInvocationContext;
18
import org.eclipse.cdt.codan.core.model.ICodanDisposable;
19
import org.eclipse.core.resources.IResource;
22
* Implementation of ICheckerInvocationContext.
23
* This class is thread-safe.
25
public class CheckerInvocationContext implements ICheckerInvocationContext {
26
private final IResource resource;
27
private final Map<Class<?>, Object> objectStorage;
30
* @param resource the resource this context is associated with.
32
public CheckerInvocationContext(IResource resource) {
33
this.resource = resource;
34
objectStorage = new HashMap<Class<?>, Object>();
37
public IResource getResource() {
41
@SuppressWarnings("unchecked")
42
public synchronized <T> T get(Class<T> objectClass) {
43
T object = (T) objectStorage.get(objectClass);
46
for (Map.Entry<Class<?>, Object> entry : objectStorage.entrySet()) {
47
if (objectClass.isAssignableFrom(entry.getKey()))
48
return (T) entry.getValue();
55
* @see ICheckerInvocationContext#add(Object)
57
public synchronized <T extends ICodanDisposable> void add(T object) {
58
Object old = objectStorage.put(object.getClass(), object);
59
if (old != null && object != old) {
60
objectStorage.put(old.getClass(), old); // Restore old value.
61
throw new IllegalArgumentException();
67
* @see IDisposableCache#dispose()
69
public void dispose() {
70
for (Map.Entry<Class<?>, Object> entry : objectStorage.entrySet()) {
71
Object obj = entry.getValue();
72
if (obj instanceof ICodanDisposable) {
73
((ICodanDisposable) obj).dispose();
76
objectStorage.clear();