/testability-explorer/src/main/java/com/google/test/metric/VariableState.java

http://testability-explorer.googlecode.com/ · Java · 83 lines · 58 code · 9 blank · 16 comment · 14 complexity · bcaf709de73984bdb5228871a14b9de3 MD5 · raw file

  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. // Copyright 2008 Google Inc. All Rights Reserved.
  17. package com.google.test.metric;
  18. import java.util.HashSet;
  19. import java.util.Set;
  20. public class VariableState {
  21. private final Set<Variable> injectables = new HashSet<Variable>();
  22. private final Set<Variable> globals = new HashSet<Variable>();
  23. boolean isGlobal(Variable var) {
  24. if (var == null) {
  25. return false;
  26. }
  27. if (var.isGlobal()) {
  28. return true;
  29. }
  30. if (globals.contains(var)) {
  31. return true;
  32. }
  33. if (var instanceof LocalField) {
  34. LocalField field = (LocalField) var;
  35. return globals.contains(field.getInstance()) || globals.contains(field.getField());
  36. }
  37. return false;
  38. }
  39. boolean isInjectable(Variable var) {
  40. if (var == null) {
  41. return false;
  42. }
  43. if (injectables.contains(var)) {
  44. return true;
  45. } else {
  46. if (var instanceof LocalField) {
  47. return injectables.contains(((LocalField) var).getField());
  48. } else {
  49. return false;
  50. }
  51. }
  52. }
  53. void setGlobal(Variable var) {
  54. globals.add(var);
  55. }
  56. void setInjectable(Variable var) {
  57. injectables.add(var);
  58. }
  59. @Override
  60. public String toString() {
  61. StringBuilder buf = new StringBuilder();
  62. buf.append("\nInjectables:");
  63. for (Variable var : injectables) {
  64. buf.append("\n ");
  65. buf.append(var);
  66. }
  67. buf.append("\nGlobals:");
  68. for (Variable var : globals) {
  69. buf.append("\n ");
  70. buf.append(var);
  71. }
  72. return buf.toString();
  73. }
  74. }