You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntMap.java 3.6KB

15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
14 years ago
15 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class IntMap {
  16. // public static final IntMap EMPTY = new IntMap(0) {
  17. // public boolean directlyInAdvice() { return true; }
  18. // public ShadowMunger getEnclosingAdvice() { return null; } //XXX possible
  19. // };
  20. // XXX begin hack to avoid a signature refactoring in Pointcut
  21. private ResolvedType concreteAspect;
  22. private ShadowMunger enclosingAdvice;
  23. private List<ResolvedPointcutDefinition> enclosingDefinition = new ArrayList<ResolvedPointcutDefinition>();
  24. public void pushEnclosingDefinition(ResolvedPointcutDefinition def) {
  25. enclosingDefinition.add(def);
  26. }
  27. public void popEnclosingDefinitition() {
  28. enclosingDefinition.remove(enclosingDefinition.size() - 1);
  29. }
  30. public ResolvedPointcutDefinition peekEnclosingDefinition() {
  31. if (enclosingDefinition.size() == 0) {
  32. return null;
  33. }
  34. return enclosingDefinition.get(enclosingDefinition.size() - 1);
  35. }
  36. public boolean directlyInAdvice() {
  37. return enclosingDefinition.isEmpty();
  38. }
  39. public ShadowMunger getEnclosingAdvice() {
  40. return enclosingAdvice;
  41. }
  42. public void setEnclosingAdvice(ShadowMunger advice) {
  43. this.enclosingAdvice = advice;
  44. }
  45. public Member getAdviceSignature() {
  46. if (enclosingAdvice instanceof Advice) {
  47. return ((Advice) enclosingAdvice).getSignature();
  48. } else {
  49. return null;
  50. }
  51. }
  52. public ResolvedType getConcreteAspect() {
  53. return concreteAspect;
  54. }
  55. public void setConcreteAspect(ResolvedType concreteAspect) {
  56. this.concreteAspect = concreteAspect;
  57. }
  58. public void copyContext(IntMap bindings) {
  59. this.enclosingAdvice = bindings.enclosingAdvice;
  60. this.enclosingDefinition = bindings.enclosingDefinition;
  61. this.concreteAspect = bindings.concreteAspect;
  62. }
  63. // XXX end hack to avoid a signature refactoring in Pointcut
  64. private static final int MISSING = -1;
  65. private int[] map;
  66. private IntMap(int[] map) {
  67. this.map = map;
  68. }
  69. public IntMap() {
  70. map = new int[0];
  71. }
  72. public IntMap(int initialCapacity) {
  73. map = new int[initialCapacity];
  74. for (int i = 0; i < initialCapacity; i++) {
  75. map[i] = MISSING;
  76. }
  77. }
  78. public void put(int key, int val) {
  79. /* assert (val >= 0 && key >= 0) */
  80. if (key >= map.length) {
  81. int[] tmp = new int[key * 2 + 1]; // ??? better expansion function
  82. System.arraycopy(map, 0, tmp, 0, map.length);
  83. for (int i = map.length, len = tmp.length; i < len; i++) {
  84. tmp[i] = MISSING;
  85. }
  86. map = tmp;
  87. }
  88. map[key] = val;
  89. }
  90. public int get(int key) {
  91. return map[key];
  92. }
  93. public boolean hasKey(int key) {
  94. return (key < map.length && map[key] != MISSING);
  95. }
  96. // ---- factory methods
  97. public static IntMap idMap(int size) {
  98. int[] map = new int[size];
  99. for (int i = 0; i < size; i++) {
  100. map[i] = i;
  101. }
  102. return new IntMap(map);
  103. }
  104. // ---- from object
  105. public String toString() {
  106. StringBuffer buf = new StringBuffer("[");
  107. boolean seenFirst = false;
  108. for (int i = 0, len = map.length; i < len; i++) {
  109. if (map[i] != MISSING) {
  110. if (seenFirst) {
  111. buf.append(", ");
  112. }
  113. seenFirst = true;
  114. buf.append(i);
  115. buf.append(" -> ");
  116. buf.append(map[i]);
  117. }
  118. }
  119. buf.append("]");
  120. return buf.toString();
  121. }
  122. }