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 GenericObjectPool}.
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 GenericObjectPoolConfig<T> extends BaseObjectPoolConfig<T> {
032
033    /**
034     * The default value for the {@code maxTotal} configuration attribute: {@value}.
035     *
036     * @see GenericObjectPool#getMaxTotal()
037     */
038    public static final int DEFAULT_MAX_TOTAL = 8;
039
040    /**
041     * The default value for the {@code maxIdle} configuration attribute: {@value}.
042     *
043     * @see GenericObjectPool#getMaxIdle()
044     */
045    public static final int DEFAULT_MAX_IDLE = 8;
046
047    /**
048     * The default value for the {@code minIdle} configuration attribute: {@value}.
049     *
050     * @see GenericObjectPool#getMinIdle()
051     */
052    public static final int DEFAULT_MIN_IDLE = 0;
053
054    private int maxTotal = DEFAULT_MAX_TOTAL;
055
056    private int maxIdle = DEFAULT_MAX_IDLE;
057
058    private int minIdle = DEFAULT_MIN_IDLE;
059
060    @SuppressWarnings("unchecked")
061    @Override
062    public GenericObjectPoolConfig<T> clone() {
063        try {
064            return (GenericObjectPoolConfig<T>) super.clone();
065        } catch (final CloneNotSupportedException e) {
066            throw new AssertionError(); // Can't happen
067        }
068    }
069
070    /**
071     * Gets the value for the {@code maxIdle} configuration attribute
072     * for pools created with this configuration instance.
073     *
074     * @return  The current setting of {@code maxIdle} for this
075     *          configuration instance
076     *
077     * @see GenericObjectPool#getMaxIdle()
078     */
079    public int getMaxIdle() {
080        return maxIdle;
081    }
082
083    /**
084     * Gets the value for the {@code maxTotal} configuration attribute
085     * for pools created with this configuration instance.
086     *
087     * @return  The current setting of {@code maxTotal} for this
088     *          configuration instance
089     *
090     * @see GenericObjectPool#getMaxTotal()
091     */
092    public int getMaxTotal() {
093        return maxTotal;
094    }
095
096    /**
097     * Gets the value for the {@code minIdle} configuration attribute
098     * for pools created with this configuration instance.
099     *
100     * @return  The current setting of {@code minIdle} for this
101     *          configuration instance
102     *
103     * @see GenericObjectPool#getMinIdle()
104     */
105    public int getMinIdle() {
106        return minIdle;
107    }
108
109    /**
110     * Sets the value for the {@code maxIdle} configuration attribute for
111     * pools created with this configuration instance.
112     *
113     * @param maxIdle The new setting of {@code maxIdle}
114     *        for this configuration instance
115     *
116     * @see GenericObjectPool#setMaxIdle(int)
117     */
118    public void setMaxIdle(final int maxIdle) {
119        this.maxIdle = maxIdle;
120    }
121
122    /**
123     * Sets the value for the {@code maxTotal} configuration attribute for
124     * pools created with this configuration instance.
125     *
126     * @param maxTotal The new setting of {@code maxTotal}
127     *        for this configuration instance
128     *
129     * @see GenericObjectPool#setMaxTotal(int)
130     */
131    public void setMaxTotal(final int maxTotal) {
132        this.maxTotal = maxTotal;
133    }
134
135    /**
136     * Sets the value for the {@code minIdle} configuration attribute for
137     * pools created with this configuration instance.
138     *
139     * @param minIdle The new setting of {@code minIdle}
140     *        for this configuration instance
141     *
142     * @see GenericObjectPool#setMinIdle(int)
143     */
144    public void setMinIdle(final int minIdle) {
145        this.minIdle = minIdle;
146    }
147
148    @Override
149    protected void toStringAppendFields(final StringBuilder builder) {
150        super.toStringAppendFields(builder);
151        builder.append(", maxTotal=");
152        builder.append(maxTotal);
153        builder.append(", maxIdle=");
154        builder.append(maxIdle);
155        builder.append(", minIdle=");
156        builder.append(minIdle);
157    }
158}