/testability-explorer/src/main/java/com/google/test/metric/example/MutableGlobalState/FinalGlobalExample.java

http://testability-explorer.googlecode.com/ · Java · 90 lines · 35 code · 15 blank · 40 comment · 0 complexity · 9896eefa14e699c9775cfae443d16960 MD5 · raw file

  1. /*
  2. * Copyright 2007 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. package com.google.test.metric.example.MutableGlobalState;
  17. /**
  18. * This class illustrates global state through using another class'
  19. * {@code public static final} instance. (That "other class" is
  20. * {@code FinalGlobal} below.
  21. *
  22. * <p>The purpose of testing this is to illustrate how global mutable state is
  23. * transitive, for non-fields.
  24. */
  25. public class FinalGlobalExample {
  26. public static class Gadget {
  27. /**
  28. * This is important: it is final and is exposed through {@code getId}.
  29. * When it is accessed by {@code getGlobalId} below, observe that there is
  30. * not a Global State cost, since it is final. */
  31. private final String id;
  32. /**
  33. * Note that this is not final, and it is exposed through {@code getCount}.
  34. * When it is accessed through {@code getGlobalCount} below, there is a
  35. * Global State cost, since it is mutable global state. (Made global through
  36. * the transitive nature of the {@code FinalGlobal} class having a static
  37. * {@code Gadget} field.*/
  38. private int count;
  39. public Gadget(String id, int count) {
  40. this.id = id;
  41. this.count = count;
  42. }
  43. public String getId() {
  44. return id;
  45. }
  46. public int getCount() {
  47. return count;
  48. }
  49. public int increment() {
  50. return ++count;
  51. }
  52. }
  53. /** This class has reference to global state */
  54. public static class FinalGlobal {
  55. /** Holds a <em>final</em> instance to global state. This does not count as global
  56. * mutable state, but observe how what it references can be counted as globally
  57. * mutable state.
  58. */
  59. public static final Gadget finalInstance = new Gadget("Global", 1);
  60. }
  61. /* The rest are instance methods on {@code FinalGlobalExample} */
  62. public Gadget getInstance() {
  63. return FinalGlobal.finalInstance;
  64. }
  65. /** The field {@code id} in Gadget is final, so accessing it is not counted as a cost. */
  66. public String getGlobalId() {
  67. return FinalGlobal.finalInstance.getId();
  68. }
  69. public int getGlobalCount() {
  70. return FinalGlobal.finalInstance.getCount();
  71. }
  72. public int globalIncrement() {
  73. return FinalGlobal.finalInstance.increment();
  74. }
  75. }