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.

TypePatternQuestions.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import org.aspectj.util.FuzzyBoolean;
  16. import org.aspectj.weaver.ResolvedType;
  17. public class TypePatternQuestions {
  18. private Map<Question,FuzzyBoolean> questionsAndAnswers = new HashMap<>();
  19. public FuzzyBoolean askQuestion(TypePattern pattern, ResolvedType type,
  20. TypePattern.MatchKind kind)
  21. {
  22. Question question = new Question(pattern, type, kind);
  23. //??? should we use this table to do caching or is that a pessimization
  24. //??? if we do that optimization we can also do error checking that the result
  25. //??? doesn't change
  26. FuzzyBoolean answer = question.ask();
  27. questionsAndAnswers.put(question, answer);
  28. return answer;
  29. }
  30. public Question anyChanges() {
  31. for (Map.Entry<Question, FuzzyBoolean> entry : questionsAndAnswers.entrySet()) {
  32. Question question = entry.getKey();
  33. FuzzyBoolean expectedAnswer = entry.getValue();
  34. FuzzyBoolean currentAnswer = question.ask();
  35. //System.out.println(question + ":" + currentAnswer);
  36. if (currentAnswer != expectedAnswer) {
  37. return question;
  38. }
  39. }
  40. return null;
  41. }
  42. public String toString() {
  43. StringBuilder buf = new StringBuilder();
  44. buf.append("TypePatternQuestions{");
  45. for (Map.Entry<Question,FuzzyBoolean> entry: questionsAndAnswers.entrySet()) {
  46. Question question = entry.getKey();
  47. FuzzyBoolean expectedAnswer = entry.getValue();
  48. buf.append(question);
  49. buf.append(":");
  50. buf.append(expectedAnswer);
  51. buf.append(", ");
  52. }
  53. buf.append("}");
  54. return buf.toString();
  55. }
  56. public class Question {
  57. TypePattern pattern;
  58. ResolvedType type;
  59. TypePattern.MatchKind kind;
  60. public Question(TypePattern pattern, ResolvedType type,
  61. TypePattern.MatchKind kind) {
  62. super();
  63. this.pattern = pattern;
  64. this.type = type;
  65. this.kind = kind;
  66. }
  67. public FuzzyBoolean ask() {
  68. return pattern.matches(type, kind);
  69. }
  70. public boolean equals(Object other) {
  71. if (!(other instanceof Question)) return false;
  72. Question o = (Question)other;
  73. return o.pattern.equals(pattern) && o.type.equals(type) && o.kind == kind;
  74. }
  75. public int hashCode() {
  76. int result = 17;
  77. result = 37*result + kind.hashCode();
  78. result = 37*result + pattern.hashCode();
  79. result = 37*result + type.hashCode();
  80. return result;
  81. }
  82. public String toString() {
  83. return "?(" + pattern + ", " + type + ", " + kind + ")";
  84. }
  85. }
  86. }