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.

CountingMessageHandler.java 4.2KB

21 years ago
21 years ago
21 years ago
14 years ago
21 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
21 years ago
14 years ago
21 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.bridge;
  14. import java.util.Enumeration;
  15. import java.util.Hashtable;
  16. import org.aspectj.util.LangUtil;
  17. /**
  18. * Wrap an IMessageHandler to count messages handled. Messages being ignored by the delegate IMessageHandler are not counted.
  19. */
  20. public class CountingMessageHandler implements IMessageHandler {
  21. public final IMessageHandler delegate;
  22. public final CountingMessageHandler proxy;
  23. private final Hashtable<IMessage.Kind, IntHolder> counters;
  24. public static CountingMessageHandler makeCountingMessageHandler(IMessageHandler handler) {
  25. if (handler instanceof CountingMessageHandler) {
  26. return (CountingMessageHandler) handler;
  27. } else {
  28. return new CountingMessageHandler(handler);
  29. }
  30. }
  31. public CountingMessageHandler(IMessageHandler delegate) {
  32. LangUtil.throwIaxIfNull(delegate, "delegate");
  33. this.delegate = delegate;
  34. this.counters = new Hashtable<>();
  35. proxy = (delegate instanceof CountingMessageHandler ? (CountingMessageHandler) delegate : null);
  36. }
  37. /** @return delegate.handleMessage(IMessage) */
  38. public boolean handleMessage(IMessage message) throws AbortException {
  39. if (null != proxy) {
  40. return proxy.handleMessage(message);
  41. }
  42. if (null != message) {
  43. IMessage.Kind kind = message.getKind();
  44. if (!isIgnoring(kind)) {
  45. increment(kind);
  46. }
  47. }
  48. return delegate.handleMessage(message);
  49. }
  50. /** @return delegate.isIgnoring(IMessage.Kind) */
  51. public boolean isIgnoring(IMessage.Kind kind) {
  52. return delegate.isIgnoring(kind);
  53. }
  54. /**
  55. * Delegate
  56. *
  57. * @see org.aspectj.bridge.IMessageHandler#isIgnoring(org.aspectj.bridge.IMessage.Kind)
  58. * @param kind
  59. */
  60. public void dontIgnore(IMessage.Kind kind) {
  61. delegate.dontIgnore(kind);
  62. }
  63. /**
  64. * Delegate
  65. *
  66. * @see org.aspectj.bridge.IMessageHandler#ignore(org.aspectj.bridge.IMessage.Kind)
  67. * @param kind
  68. */
  69. public void ignore(IMessage.Kind kind) {
  70. delegate.ignore(kind);
  71. }
  72. /** @return delegate.toString() */
  73. public String toString() {
  74. return delegate.toString();
  75. }
  76. /**
  77. * Return count of messages seen through this interface.
  78. *
  79. * @param kind the IMessage.Kind of the messages to count (if null, count all)
  80. * @param orGreater if true, then count this kind and any considered greater by the ordering of IMessage.Kind#COMPARATOR
  81. * @return number of messages of this kind (optionally or greater)
  82. * @see IMessage.Kind#COMPARATOR
  83. */
  84. public int numMessages(IMessage.Kind kind, boolean orGreater) {
  85. if (null != proxy) {
  86. return proxy.numMessages(kind, orGreater);
  87. }
  88. int result = 0;
  89. if (null == kind) {
  90. for (Enumeration<IntHolder> enu = counters.elements(); enu.hasMoreElements();) {
  91. result += (enu.nextElement()).count;
  92. }
  93. } else if (!orGreater) {
  94. result = numMessages(kind);
  95. } else {
  96. for (IMessage.Kind k : IMessage.KINDS) {
  97. if (kind.isSameOrLessThan(k)) {
  98. result += numMessages(k);
  99. }
  100. }
  101. }
  102. return result;
  103. }
  104. /**
  105. * @return true if 0 is less than <code>numMessages(IMessage.ERROR, true)</code>
  106. */
  107. public boolean hasErrors() {
  108. return (0 < numMessages(IMessage.ERROR, true));
  109. }
  110. private int numMessages(IMessage.Kind kind) {
  111. if (null != proxy) {
  112. return proxy.numMessages(kind);
  113. }
  114. IntHolder counter = counters.get(kind);
  115. return (null == counter ? 0 : counter.count);
  116. }
  117. private void increment(IMessage.Kind kind) {
  118. if (null != proxy) {
  119. throw new IllegalStateException("not called when proxying");
  120. }
  121. IntHolder counter = counters.get(kind);
  122. if (null == counter) {
  123. counter = new IntHolder();
  124. counters.put(kind, counter);
  125. }
  126. counter.count++;
  127. }
  128. private static class IntHolder {
  129. int count;
  130. }
  131. public void reset() {
  132. if (proxy != null) {
  133. proxy.reset();
  134. }
  135. counters.clear();
  136. }
  137. }