View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.pool2;
18  
19  /**
20   * Test pooled object class. Keeps track of how many times it has been validated, activated, passivated.
21   *
22   * @param <K> The key type.
23   */
24  public class VisitTracker<K> {
25  
26      private int validateCount;
27      private int activateCount;
28      private int passivateCount;
29      private boolean destroyed;
30      private int id;
31      private K key;
32  
33      public VisitTracker() {
34          reset();
35      }
36  
37      public VisitTracker(final int id) {
38          this.id = id;
39          reset();
40      }
41  
42      public VisitTracker(final int id, final K key) {
43          this.id = id;
44          this.key = key;
45          reset();
46      }
47  
48      public void activate() {
49          if (destroyed) {
50              fail("attempted to activate a destroyed object");
51          }
52          activateCount++;
53      }
54  
55      public void destroy() {
56          destroyed = true;
57      }
58  
59      private void fail(final String message) {
60          throw new IllegalStateException(message);
61      }
62  
63      public int getActivateCount() {
64          return activateCount;
65      }
66  
67      public int getId() {
68          return id;
69      }
70  
71      public K getKey() {
72          return key;
73      }
74  
75      public int getPassivateCount() {
76          return passivateCount;
77      }
78  
79      public int getValidateCount() {
80          return validateCount;
81      }
82  
83      public boolean isDestroyed() {
84          return destroyed;
85      }
86  
87      public void passivate() {
88          if (destroyed) {
89              fail("attempted to passivate a destroyed object");
90          }
91          passivateCount++;
92      }
93  
94      public void reset() {
95          validateCount = 0;
96          activateCount = 0;
97          passivateCount = 0;
98          destroyed = false;
99      }
100 
101     @Override
102     public String toString() {
103         return "Key: " + key + " id: " + id;
104     }
105 
106     public boolean validate() {
107         if (destroyed) {
108             fail("attempted to validate a destroyed object");
109         }
110         validateCount++;
111         return true;
112     }
113 }