summaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/org/aspectj/weaver/IntMap.java
blob: c0af738ba38a223c5f6fbcdd9a79ecd84bbb2ac9 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * 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 
 *  
 * Contributors: 
 *     PARC     initial implementation 
 * ******************************************************************/

package org.aspectj.weaver;

import java.util.ArrayList;
import java.util.List;

public class IntMap {
	// public static final IntMap EMPTY = new IntMap(0) {
	// public boolean directlyInAdvice() { return true; }
	// public ShadowMunger getEnclosingAdvice() { return null; } //XXX possible
	// };

	// XXX begin hack to avoid a signature refactoring in Pointcut
	private ResolvedType concreteAspect;
	private ShadowMunger enclosingAdvice;
	private List<ResolvedPointcutDefinition> enclosingDefinition = new ArrayList<ResolvedPointcutDefinition>();

	public void pushEnclosingDefinition(ResolvedPointcutDefinition def) {
		enclosingDefinition.add(def);
	}

	public void popEnclosingDefinitition() {
		enclosingDefinition.remove(enclosingDefinition.size() - 1);
	}

	public ResolvedPointcutDefinition peekEnclosingDefinition() {
		if (enclosingDefinition.size() == 0) {
			return null;
		}
		return enclosingDefinition.get(enclosingDefinition.size() - 1);
	}

	public boolean directlyInAdvice() {
		return enclosingDefinition.isEmpty();
	}

	public ShadowMunger getEnclosingAdvice() {
		return enclosingAdvice;
	}

	public void setEnclosingAdvice(ShadowMunger advice) {
		this.enclosingAdvice = advice;
	}

	public Member getAdviceSignature() {
		if (enclosingAdvice instanceof Advice) {
			return ((Advice) enclosingAdvice).getSignature();
		} else {
			return null;
		}
	}

	public ResolvedType getConcreteAspect() {
		return concreteAspect;
	}

	public void setConcreteAspect(ResolvedType concreteAspect) {
		this.concreteAspect = concreteAspect;
	}

	public void copyContext(IntMap bindings) {
		this.enclosingAdvice = bindings.enclosingAdvice;
		this.enclosingDefinition = bindings.enclosingDefinition;
		this.concreteAspect = bindings.concreteAspect;
	}

	// XXX end hack to avoid a signature refactoring in Pointcut

	private static final int MISSING = -1;

	private int[] map;

	private IntMap(int[] map) {
		this.map = map;
	}

	public IntMap() {
		map = new int[0];
	}

	public IntMap(int initialCapacity) {
		map = new int[initialCapacity];
		for (int i = 0; i < initialCapacity; i++) {
			map[i] = MISSING;
		}
	}

	public void put(int key, int val) {
		/* assert (val >= 0 && key >= 0) */
		if (key >= map.length) {
			int[] tmp = new int[key * 2 + 1]; // ??? better expansion function
			System.arraycopy(map, 0, tmp, 0, map.length);
			for (int i = map.length, len = tmp.length; i < len; i++) {
				tmp[i] = MISSING;
			}
			map = tmp;
		}
		map[key] = val;
	}

	public int get(int key) {
		return map[key];
	}

	public boolean hasKey(int key) {
		return (key < map.length && map[key] != MISSING);
	}

	// ---- factory methods

	public static IntMap idMap(int size) {
		int[] map = new int[size];
		for (int i = 0; i < size; i++) {
			map[i] = i;
		}
		return new IntMap(map);
	}

	// ---- from object

	public String toString() {
		StringBuffer buf = new StringBuffer("[");
		boolean seenFirst = false;
		for (int i = 0, len = map.length; i < len; i++) {
			if (map[i] != MISSING) {
				if (seenFirst) {
					buf.append(", ");
				}
				seenFirst = true;
				buf.append(i);
				buf.append(" -> ");
				buf.append(map[i]);
			}
		}
		buf.append("]");
		return buf.toString();
	}

}