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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-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 java.util.Iterator;
  17. import org.aspectj.util.LangUtil;
  18. /**
  19. * Wrap an IMessageHandler to count messages handled.
  20. * Messages being ignored by the delegate IMessageHandler are not counted.
  21. */
  22. public class CountingMessageHandler implements IMessageHandler {
  23. public final IMessageHandler delegate;
  24. public final CountingMessageHandler proxy;
  25. private final Hashtable counters;
  26. public static CountingMessageHandler makeCountingMessageHandler(IMessageHandler handler) {
  27. if (handler instanceof CountingMessageHandler) {
  28. return (CountingMessageHandler)handler;
  29. } else {
  30. return new CountingMessageHandler(handler);
  31. }
  32. }
  33. public CountingMessageHandler(IMessageHandler delegate) {
  34. LangUtil.throwIaxIfNull(delegate, "delegate");
  35. this.delegate = delegate;
  36. this.counters = new Hashtable();
  37. proxy = (delegate instanceof CountingMessageHandler
  38. ? (CountingMessageHandler) delegate
  39. : null);
  40. }
  41. /** @return delegate.handleMessage(IMessage) */
  42. public boolean handleMessage(IMessage message) throws AbortException {
  43. if (null != proxy) {
  44. return proxy.handleMessage(message);
  45. }
  46. if (null != message) {
  47. IMessage.Kind kind = message.getKind();
  48. if (!isIgnoring(kind)) {
  49. increment(kind);
  50. }
  51. }
  52. return delegate.handleMessage(message);
  53. }
  54. /** @return delegate.isIgnoring(IMessage.Kind) */
  55. public boolean isIgnoring(IMessage.Kind kind) {
  56. return delegate.isIgnoring(kind);
  57. }
  58. /** @return delegate.toString() */
  59. public String toString() {
  60. return delegate.toString();
  61. }
  62. /**
  63. * Return count of messages seen through this interface.
  64. * @param kind the IMessage.Kind of the messages to count
  65. * (if null, count all)
  66. * @param orGreater if true, then count this kind and any
  67. * considered greater by the ordering of
  68. * IMessage.Kind.COMPARATOR
  69. * @return number of messages of this kind (optionally or greater)
  70. * @see IMessage.Kind.COMPARATOR
  71. */
  72. public int numMessages(IMessage.Kind kind, boolean orGreater) {
  73. if (null != proxy) {
  74. return proxy.numMessages(kind, orGreater);
  75. }
  76. int result = 0;
  77. if (null == kind) {
  78. for (Enumeration enu = counters.elements(); enu.hasMoreElements();) {
  79. result += ((IntHolder) enu.nextElement()).count;
  80. }
  81. } else if (!orGreater) {
  82. result = numMessages(kind);
  83. } else {
  84. for (Iterator iter = IMessage.KINDS.iterator(); iter.hasNext();) {
  85. IMessage.Kind k = (IMessage.Kind) iter.next();
  86. if (kind.isSameOrLessThan(k)) {
  87. result += numMessages(k);
  88. }
  89. }
  90. }
  91. return result;
  92. }
  93. /**
  94. * @return true if 0 is less than
  95. * <code>numMessages(IMessage.ERROR, true)</code>
  96. */
  97. public boolean hasErrors() {
  98. return (0 < numMessages(IMessage.ERROR, true));
  99. }
  100. private int numMessages(IMessage.Kind kind) {
  101. if (null != proxy) {
  102. return proxy.numMessages(kind);
  103. }
  104. IntHolder counter = (IntHolder) counters.get(kind);
  105. return (null == counter ? 0 : counter.count);
  106. }
  107. private void increment(IMessage.Kind kind) {
  108. if (null != proxy) {
  109. throw new IllegalStateException("not called when proxying");
  110. }
  111. IntHolder counter = (IntHolder) counters.get(kind);
  112. if (null == counter) {
  113. counter = new IntHolder();
  114. counters.put(kind, counter);
  115. }
  116. counter.count++;
  117. }
  118. private static class IntHolder {
  119. int count;
  120. }
  121. public void reset() {
  122. if (proxy != null) proxy.reset();
  123. counters.clear();
  124. }
  125. /**
  126. * Not supported
  127. * @param kind
  128. */
  129. public void dontIgnore(IMessage.Kind kind) {
  130. ;
  131. }
  132. }