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