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 structure 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: {@value}.
035     *
036     * @see GenericKeyedObjectPool#getMaxTotalPerKey()
037     */
038    public static final int DEFAULT_MAX_TOTAL_PER_KEY = 8;
039
040    /**
041     * The default value for the {@code maxTotal} configuration attribute: {@value}.
042     *
043     * @see GenericKeyedObjectPool#getMaxTotal()
044     */
045    public static final int DEFAULT_MAX_TOTAL = -1;
046
047    /**
048     * The default value for the {@code minIdlePerKey} configuration attribute: {@value}.
049     *
050     * @see GenericKeyedObjectPool#getMinIdlePerKey()
051     */
052    public static final int DEFAULT_MIN_IDLE_PER_KEY = 0;
053
054    /**
055     * The default value for the {@code maxIdlePerKey} configuration attribute: {@value}.
056     *
057     * @see GenericKeyedObjectPool#getMaxIdlePerKey()
058     */
059    public static final int DEFAULT_MAX_IDLE_PER_KEY = 8;
060
061    private int minIdlePerKey = DEFAULT_MIN_IDLE_PER_KEY;
062
063    private int maxIdlePerKey = DEFAULT_MAX_IDLE_PER_KEY;
064
065    private int maxTotalPerKey = DEFAULT_MAX_TOTAL_PER_KEY;
066
067    private int maxTotal = DEFAULT_MAX_TOTAL;
068
069    /**
070     * Constructs a new configuration with default settings.
071     */
072    public GenericKeyedObjectPoolConfig() {
073    }
074
075    @SuppressWarnings("unchecked")
076    @Override
077    public GenericKeyedObjectPoolConfig<T> clone() {
078        try {
079            return (GenericKeyedObjectPoolConfig<T>) super.clone();
080        } catch (final CloneNotSupportedException e) {
081            throw new AssertionError(); // Can't happen
082        }
083    }
084
085    /**
086     * Gets the value for the {@code maxIdlePerKey} configuration attribute
087     * for pools created with this configuration instance.
088     *
089     * @return  The current setting of {@code maxIdlePerKey} for this
090     *          configuration instance
091     *
092     * @see GenericKeyedObjectPool#getMaxIdlePerKey()
093     */
094    public int getMaxIdlePerKey() {
095        return maxIdlePerKey;
096    }
097
098    /**
099     * Gets the value for the {@code maxTotal} configuration attribute
100     * for pools created with this configuration instance.
101     *
102     * @return  The current setting of {@code maxTotal} for this
103     *          configuration instance
104     *
105     * @see GenericKeyedObjectPool#getMaxTotal()
106     */
107    public int getMaxTotal() {
108        return maxTotal;
109    }
110
111    /**
112     * Gets the value for the {@code maxTotalPerKey} configuration attribute
113     * for pools created with this configuration instance.
114     *
115     * @return  The current setting of {@code maxTotalPerKey} for this
116     *          configuration instance
117     *
118     * @see GenericKeyedObjectPool#getMaxTotalPerKey()
119     */
120    public int getMaxTotalPerKey() {
121        return maxTotalPerKey;
122    }
123
124    /**
125     * Gets 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     * Sets the value for the {@code maxIdlePerKey} configuration attribute for
139     * pools created with this configuration instance.
140     *
141     * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
142     *        for this configuration instance
143     *
144     * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
145     */
146    public void setMaxIdlePerKey(final int maxIdlePerKey) {
147        this.maxIdlePerKey = maxIdlePerKey;
148    }
149
150    /**
151     * Sets the value for the {@code maxTotal} configuration attribute for
152     * pools created with this configuration instance.
153     *
154     * @param maxTotal The new setting of {@code maxTotal}
155     *        for this configuration instance
156     *
157     * @see GenericKeyedObjectPool#setMaxTotal(int)
158     */
159    public void setMaxTotal(final int maxTotal) {
160        this.maxTotal = maxTotal;
161    }
162
163    /**
164     * Sets the value for the {@code maxTotalPerKey} configuration attribute for
165     * pools created with this configuration instance.
166     *
167     * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
168     *        for this configuration instance
169     *
170     * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
171     */
172    public void setMaxTotalPerKey(final int maxTotalPerKey) {
173        this.maxTotalPerKey = maxTotalPerKey;
174    }
175
176    /**
177     * Sets the value for the {@code minIdlePerKey} configuration attribute for
178     * pools created with this configuration instance.
179     *
180     * @param minIdlePerKey The new setting of {@code minIdlePerKey}
181     *        for this configuration instance
182     *
183     * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
184     */
185    public void setMinIdlePerKey(final int minIdlePerKey) {
186        this.minIdlePerKey = minIdlePerKey;
187    }
188
189    @Override
190    protected void toStringAppendFields(final StringBuilder builder) {
191        super.toStringAppendFields(builder);
192        builder.append(", minIdlePerKey=");
193        builder.append(minIdlePerKey);
194        builder.append(", maxIdlePerKey=");
195        builder.append(maxIdlePerKey);
196        builder.append(", maxTotalPerKey=");
197        builder.append(maxTotalPerKey);
198        builder.append(", maxTotal=");
199        builder.append(maxTotal);
200    }
201}