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 */
017package org.apache.commons.pool2.impl;
018
019/**
020 * A simple "struct" encapsulating the configuration for a
021 * {@link GenericKeyedObjectPool}.
022 *
023 * <p>
024 * This class is not thread-safe; it is only intended to be used to provide
025 * attributes used when creating a pool.
026 * </p>
027 *
028 * @param <T> Type of element pooled.
029 * @since 2.0
030 */
031public class GenericKeyedObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
032
033    /**
034     * The default value for the {@code maxTotalPerKey} configuration attribute.
035     * @see GenericKeyedObjectPool#getMaxTotalPerKey()
036     */
037    public static final int DEFAULT_MAX_TOTAL_PER_KEY = 8;
038
039    /**
040     * The default value for the {@code maxTotal} configuration attribute.
041     * @see GenericKeyedObjectPool#getMaxTotal()
042     */
043    public static final int DEFAULT_MAX_TOTAL = -1;
044
045    /**
046     * The default value for the {@code minIdlePerKey} configuration attribute.
047     * @see GenericKeyedObjectPool#getMinIdlePerKey()
048     */
049    public static final int DEFAULT_MIN_IDLE_PER_KEY = 0;
050
051    /**
052     * The default value for the {@code maxIdlePerKey} configuration attribute.
053     * @see GenericKeyedObjectPool#getMaxIdlePerKey()
054     */
055    public static final int DEFAULT_MAX_IDLE_PER_KEY = 8;
056
057
058    private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;
059
060    private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;
061
062    private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;
063
064    private int maxTotal = DEFAULT_MAX_TOTAL;
065
066    /**
067     * Create a new configuration with default settings.
068     */
069    public GenericKeyedObjectPoolConfig() {
070    }
071
072    /**
073     * Get the value for the {@code maxTotal} configuration attribute
074     * for pools created with this configuration instance.
075     *
076     * @return  The current setting of {@code maxTotal} for this
077     *          configuration instance
078     *
079     * @see GenericKeyedObjectPool#getMaxTotal()
080     */
081    public int getMaxTotal() {
082        return maxTotal;
083    }
084
085    /**
086     * Set the value for the {@code maxTotal} configuration attribute for
087     * pools created with this configuration instance.
088     *
089     * @param maxTotal The new setting of {@code maxTotal}
090     *        for this configuration instance
091     *
092     * @see GenericKeyedObjectPool#setMaxTotal(int)
093     */
094    public void setMaxTotal(final int maxTotal) {
095        this.maxTotal = maxTotal;
096    }
097
098    /**
099     * Get the value for the {@code maxTotalPerKey} configuration attribute
100     * for pools created with this configuration instance.
101     *
102     * @return  The current setting of {@code maxTotalPerKey} for this
103     *          configuration instance
104     *
105     * @see GenericKeyedObjectPool#getMaxTotalPerKey()
106     */
107    public int getMaxTotalPerKey() {
108        return maxTotalPerKey;
109    }
110
111    /**
112     * Set the value for the {@code maxTotalPerKey} configuration attribute for
113     * pools created with this configuration instance.
114     *
115     * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
116     *        for this configuration instance
117     *
118     * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
119     */
120    public void setMaxTotalPerKey(final int maxTotalPerKey) {
121        this.maxTotalPerKey = maxTotalPerKey;
122    }
123
124    /**
125     * Get the value for the {@code minIdlePerKey} configuration attribute
126     * for pools created with this configuration instance.
127     *
128     * @return  The current setting of {@code minIdlePerKey} for this
129     *          configuration instance
130     *
131     * @see GenericKeyedObjectPool#getMinIdlePerKey()
132     */
133    public int getMinIdlePerKey() {
134        return minIdlePerKey;
135    }
136
137    /**
138     * Set the value for the {@code minIdlePerKey} configuration attribute for
139     * pools created with this configuration instance.
140     *
141     * @param minIdlePerKey The new setting of {@code minIdlePerKey}
142     *        for this configuration instance
143     *
144     * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
145     */
146    public void setMinIdlePerKey(final int minIdlePerKey) {
147        this.minIdlePerKey = minIdlePerKey;
148    }
149
150    /**
151     * Get the value for the {@code maxIdlePerKey} configuration attribute
152     * for pools created with this configuration instance.
153     *
154     * @return  The current setting of {@code maxIdlePerKey} for this
155     *          configuration instance
156     *
157     * @see GenericKeyedObjectPool#getMaxIdlePerKey()
158     */
159    public int getMaxIdlePerKey() {
160        return maxIdlePerKey;
161    }
162
163    /**
164     * Set the value for the {@code maxIdlePerKey} configuration attribute for
165     * pools created with this configuration instance.
166     *
167     * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
168     *        for this configuration instance
169     *
170     * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
171     */
172    public void setMaxIdlePerKey(final int maxIdlePerKey) {
173        this.maxIdlePerKey = maxIdlePerKey;
174    }
175
176    @SuppressWarnings("unchecked")
177    @Override
178    public GenericKeyedObjectPoolConfig<T> clone() {
179        try {
180            return (GenericKeyedObjectPoolConfig<T>) super.clone();
181        } catch (final CloneNotSupportedException e) {
182            throw new AssertionError(); // Can't happen
183        }
184    }
185
186    @Override
187    protected void toStringAppendFields(final StringBuilder builder) {
188        super.toStringAppendFields(builder);
189        builder.append(", minIdlePerKey=");
190        builder.append(minIdlePerKey);
191        builder.append(", maxIdlePerKey=");
192        builder.append(maxIdlePerKey);
193        builder.append(", maxTotalPerKey=");
194        builder.append(maxTotalPerKey);
195        builder.append(", maxTotal=");
196        builder.append(maxTotal);
197    }
198}