Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4d72e02d8d6b1ec73620815abc50305e9968d423 (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
/**********************************************************************
 * This file is part of the "Object Teams Runtime Environment"
 *
 * Copyright 2002-2009 Berlin Institute of Technology, Germany.
 *
 * 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
 * $Id: AttributeReadingGuard.java 23408 2010-02-03 18:07:35Z stephan $
 *
 * Please visit http://www.objectteams.org for updates and contact.
 *
 * Contributors:
 * Berlin Institute of Technology - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otre.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Christine Hundt
 * @author Stephan Herrmann
 */
public class AttributeReadingGuard {
	
	private static Map<ClassLoader, AttributeReadingGuard> instances = new HashMap<ClassLoader, AttributeReadingGuard>();
    private static AttributeReadingGuard defaultInstance = new AttributeReadingGuard();
    
    private ArrayList<String> servedClasses = new ArrayList<String>();
    
    // this one flag is really global (concerns the one main class of the application):
    private static boolean firstLoaded = true;

    /**
     * @param className
     * @return
     */
    public boolean iAmTheFirst(String className) {
        return !this.servedClasses.contains(className);    
    }
    
    /**
     * Processing the given class is done.
     * @param className
     */
    public void workDone(String className) {
    	this.servedClasses.add(className);    
    }
    
    /**
     * @return whether this class is the first being loaded => possibly the main class.
     */
    public static synchronized boolean isFirstLoadedClass() {
    	if (!firstLoaded)
    		return false;
    	firstLoaded = false;
    	return true;
    }

    /** First loaded class has no main => it was a false alarm. */
	public static void reset() {
		firstLoaded = true;
	}

	/**
	 * Since actual data are stored in an instance, static methods need to retrieve the appropriate
     * instance regarding the given class loader.
	 */
	public static AttributeReadingGuard getInstanceForLoader(ClassLoader loader) {
		if (loader == null)
			return defaultInstance;
		
		AttributeReadingGuard instance = instances.get(loader);
		if (instance == null)
			instances.put(loader, instance = new AttributeReadingGuard());
		return instance;
	}
}

Back to the top