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.pool;
18  
19  /**
20   * Test pooled object class.  Keeps track of how many times it has been
21   * validated, activated, passivated.
22   *
23   */
24  public class VisitTracker {
25      private int validateCount = 0;
26      private int activateCount = 0;
27      private int passivateCount = 0;
28      private boolean destroyed = false;
29      private int id = 0;
30      private Object key = null;
31      
32      public VisitTracker() {
33          super();
34          reset();
35      }
36      
37      public VisitTracker(int id) {
38          super();
39          this.id = id;
40          reset();
41      }
42      
43      public VisitTracker(int id, Object key) {
44          super();
45          this.id = id;
46          this.key = key;
47          reset();
48      }
49      
50      public boolean validate() {
51          if (destroyed) {
52              fail("attempted to validate a destroyed object");
53          }
54          validateCount++;
55          return true;
56      }
57      public void activate() {
58          if (destroyed) {
59              fail("attempted to activate a destroyed object");
60          }
61          activateCount++;
62      }
63      public void passivate() {
64          if (destroyed) {
65              fail("attempted to passivate a destroyed object");
66          }
67          passivateCount++;
68      }
69      public void reset() {
70          validateCount = 0;
71          activateCount = 0;
72          passivateCount = 0;
73          destroyed = false;
74      }
75      public void destroy() {
76          destroyed = true;
77      }
78      public int getValidateCount() {
79          return validateCount;
80      }
81      public int getActivateCount() {
82          return activateCount;
83      }
84      public int getPassivateCount() {
85          return passivateCount;
86      }
87      public boolean isDestroyed() {
88          return destroyed;
89      }
90      public int getId() {
91          return id;
92      }
93      public Object getKey() {
94          return key;
95      }
96      public String toString() {
97          return "Key: " + key + " id: " + id;
98      }
99      
100     private void fail(String message) {
101         throw new IllegalStateException(message);
102     }
103 }