PageRenderTime 44ms CodeModel.GetById 34ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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