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  
18  package org.apache.commons.pool2;
19  
20  import org.apache.commons.pool2.impl.DefaultPooledObject;
21  
22  /**
23   * Factory that creates VisitTracker instances. Used to test Evictor runs.
24   *
25   * @param <K> The VisitTracker key type.
26   */
27  public class VisitTrackerFactory<K>
28          implements PooledObjectFactory<VisitTracker<K>>, KeyedPooledObjectFactory<K, VisitTracker<K>> {
29  
30      private int nextId;
31  
32      @Override
33      public void activateObject(final K key, final PooledObject<VisitTracker<K>> ref) {
34          ref.getObject().activate();
35      }
36  
37      @Override
38      public void activateObject(final PooledObject<VisitTracker<K>> ref) {
39          ref.getObject().activate();
40      }
41  
42      @Override
43      public void destroyObject(final K key, final PooledObject<VisitTracker<K>> ref) {
44          ref.getObject().destroy();
45      }
46  
47      @Override
48      public void destroyObject(final PooledObject<VisitTracker<K>> ref) {
49          ref.getObject().destroy();
50      }
51  
52      @Override
53      public PooledObject<VisitTracker<K>> makeObject() {
54          return new DefaultPooledObject<>(new VisitTracker<>(nextId++));
55      }
56  
57      @Override
58      public PooledObject<VisitTracker<K>> makeObject(final K key) {
59          return new DefaultPooledObject<>(new VisitTracker<>(nextId++, key));
60      }
61  
62      @Override
63      public void passivateObject(final K key, final PooledObject<VisitTracker<K>> ref) {
64          ref.getObject().passivate();
65      }
66  
67      @Override
68      public void passivateObject(final PooledObject<VisitTracker<K>> ref) {
69          ref.getObject().passivate();
70      }
71  
72      public void resetId() {
73          nextId = 0;
74      }
75  
76      @Override
77      public boolean validateObject(final K key, final PooledObject<VisitTracker<K>> ref) {
78          return ref.getObject().validate();
79      }
80  
81      @Override
82      public boolean validateObject(final PooledObject<VisitTracker<K>> ref) {
83          return ref.getObject().validate();
84      }
85  }