001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.pool;
018    
019    /**
020     * Test pooled object class.  Keeps track of how many times it has been
021     * validated, activated, passivated.
022     *
023     */
024    public class VisitTracker {
025        private int validateCount = 0;
026        private int activateCount = 0;
027        private int passivateCount = 0;
028        private boolean destroyed = false;
029        private int id = 0;
030        private Object key = null;
031        
032        public VisitTracker() {
033            super();
034            reset();
035        }
036        
037        public VisitTracker(int id) {
038            super();
039            this.id = id;
040            reset();
041        }
042        
043        public VisitTracker(int id, Object key) {
044            super();
045            this.id = id;
046            this.key = key;
047            reset();
048        }
049        
050        public boolean validate() {
051            if (destroyed) {
052                fail("attempted to validate a destroyed object");
053            }
054            validateCount++;
055            return true;
056        }
057        public void activate() {
058            if (destroyed) {
059                fail("attempted to activate a destroyed object");
060            }
061            activateCount++;
062        }
063        public void passivate() {
064            if (destroyed) {
065                fail("attempted to passivate a destroyed object");
066            }
067            passivateCount++;
068        }
069        public void reset() {
070            validateCount = 0;
071            activateCount = 0;
072            passivateCount = 0;
073            destroyed = false;
074        }
075        public void destroy() {
076            destroyed = true;
077        }
078        public int getValidateCount() {
079            return validateCount;
080        }
081        public int getActivateCount() {
082            return activateCount;
083        }
084        public int getPassivateCount() {
085            return passivateCount;
086        }
087        public boolean isDestroyed() {
088            return destroyed;
089        }
090        public int getId() {
091            return id;
092        }
093        public Object getKey() {
094            return key;
095        }
096        public String toString() {
097            return "Key: " + key + " id: " + id;
098        }
099        
100        private void fail(String message) {
101            throw new IllegalStateException(message);
102        }
103    }