~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/CheckerInvocationContext.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     QNX Software Systems (Alena Laskavaia)  - initial API and implementation
 
10
 *     Sergey Prigogin (Google)
 
11
 *******************************************************************************/
 
12
package org.eclipse.cdt.codan.internal.core;
 
13
 
 
14
import java.util.HashMap;
 
15
import java.util.Map;
 
16
 
 
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;
 
20
 
 
21
/**
 
22
 * Implementation of ICheckerInvocationContext.
 
23
 * This class is thread-safe.
 
24
 */
 
25
public class CheckerInvocationContext implements ICheckerInvocationContext {
 
26
        private final IResource resource;
 
27
        private final Map<Class<?>, Object> objectStorage;
 
28
 
 
29
        /**
 
30
         * @param resource the resource this context is associated with.
 
31
         */
 
32
        public CheckerInvocationContext(IResource resource) {
 
33
                this.resource = resource;
 
34
                objectStorage = new HashMap<Class<?>, Object>();
 
35
        }
 
36
 
 
37
        public IResource getResource() {
 
38
                return resource;
 
39
        }
 
40
 
 
41
        @SuppressWarnings("unchecked")
 
42
        public synchronized <T> T get(Class<T> objectClass) {
 
43
                T object = (T) objectStorage.get(objectClass);
 
44
                if (object != null)
 
45
                        return object;
 
46
                for (Map.Entry<Class<?>, Object> entry : objectStorage.entrySet()) {
 
47
                        if (objectClass.isAssignableFrom(entry.getKey()))
 
48
                                return (T) entry.getValue();
 
49
                }
 
50
                return null;
 
51
        }
 
52
 
 
53
        /*
 
54
         * (non-Javadoc)
 
55
         * @see ICheckerInvocationContext#add(Object)
 
56
         */
 
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();
 
62
                }
 
63
        }
 
64
 
 
65
        /*
 
66
         * (non-Javadoc)
 
67
         * @see IDisposableCache#dispose()
 
68
         */
 
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();
 
74
                        }
 
75
                }
 
76
                objectStorage.clear();
 
77
        }
 
78
}