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.
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     * Constructs a new configuration with default settings.
068     */
069    public GenericKeyedObjectPoolConfig() {
070    }
071
072    @SuppressWarnings("unchecked")
073    @Override
074    public GenericKeyedObjectPoolConfig<T> clone() {
075        try {
076            return (GenericKeyedObjectPoolConfig<T>) super.clone();
077        } catch (final CloneNotSupportedException e) {
078            throw new AssertionError(); // Can't happen
079        }
080    }
081
082    /**
083     * Get the value for the {@code maxIdlePerKey} configuration attribute
084     * for pools created with this configuration instance.
085     *
086     * @return  The current setting of {@code maxIdlePerKey} for this
087     *          configuration instance
088     *
089     * @see GenericKeyedObjectPool#getMaxIdlePerKey()
090     */
091    public int getMaxIdlePerKey() {
092        return maxIdlePerKey;
093    }
094
095    /**
096     * Get the value for the {@code maxTotal} configuration attribute
097     * for pools created with this configuration instance.
098     *
099     * @return  The current setting of {@code maxTotal} for this
100     *          configuration instance
101     *
102     * @see GenericKeyedObjectPool#getMaxTotal()
103     */
104    public int getMaxTotal() {
105        return maxTotal;
106    }
107
108    /**
109     * Get the value for the {@code maxTotalPerKey} configuration attribute
110     * for pools created with this configuration instance.
111     *
112     * @return  The current setting of {@code maxTotalPerKey} for this
113     *          configuration instance
114     *
115     * @see GenericKeyedObjectPool#getMaxTotalPerKey()
116     */
117    public int getMaxTotalPerKey() {
118        return maxTotalPerKey;
119    }
120
121    /**
122     * Get the value for the {@code minIdlePerKey} configuration attribute
123     * for pools created with this configuration instance.
124     *
125     * @return  The current setting of {@code minIdlePerKey} for this
126     *          configuration instance
127     *
128     * @see GenericKeyedObjectPool#getMinIdlePerKey()
129     */
130    public int getMinIdlePerKey() {
131        return minIdlePerKey;
132    }
133
134    /**
135     * Set the value for the {@code maxIdlePerKey} configuration attribute for
136     * pools created with this configuration instance.
137     *
138     * @param maxIdlePerKey The new setting of {@code maxIdlePerKey}
139     *        for this configuration instance
140     *
141     * @see GenericKeyedObjectPool#setMaxIdlePerKey(int)
142     */
143    public void setMaxIdlePerKey(final int maxIdlePerKey) {
144        this.maxIdlePerKey = maxIdlePerKey;
145    }
146
147    /**
148     * Set the value for the {@code maxTotal} configuration attribute for
149     * pools created with this configuration instance.
150     *
151     * @param maxTotal The new setting of {@code maxTotal}
152     *        for this configuration instance
153     *
154     * @see GenericKeyedObjectPool#setMaxTotal(int)
155     */
156    public void setMaxTotal(final int maxTotal) {
157        this.maxTotal = maxTotal;
158    }
159
160    /**
161     * Set the value for the {@code maxTotalPerKey} configuration attribute for
162     * pools created with this configuration instance.
163     *
164     * @param maxTotalPerKey The new setting of {@code maxTotalPerKey}
165     *        for this configuration instance
166     *
167     * @see GenericKeyedObjectPool#setMaxTotalPerKey(int)
168     */
169    public void setMaxTotalPerKey(final int maxTotalPerKey) {
170        this.maxTotalPerKey = maxTotalPerKey;
171    }
172
173    /**
174     * Set the value for the {@code minIdlePerKey} configuration attribute for
175     * pools created with this configuration instance.
176     *
177     * @param minIdlePerKey The new setting of {@code minIdlePerKey}
178     *        for this configuration instance
179     *
180     * @see GenericKeyedObjectPool#setMinIdlePerKey(int)
181     */
182    public void setMinIdlePerKey(final int minIdlePerKey) {
183        this.minIdlePerKey = minIdlePerKey;
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}