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    /**
061     * Constructs a new instance.
062     */
063    public GenericObjectPoolConfig() {
064        // empty
065    }
066
067    @SuppressWarnings("unchecked")
068    @Override
069    public GenericObjectPoolConfig<T> clone() {
070        try {
071            return (GenericObjectPoolConfig<T>) super.clone();
072        } catch (final CloneNotSupportedException e) {
073            throw new AssertionError(); // Can't happen
074        }
075    }
076
077    /**
078     * Gets the value for the {@code maxIdle} configuration attribute
079     * for pools created with this configuration instance.
080     *
081     * @return  The current setting of {@code maxIdle} for this
082     *          configuration instance
083     *
084     * @see GenericObjectPool#getMaxIdle()
085     */
086    public int getMaxIdle() {
087        return maxIdle;
088    }
089
090    /**
091     * Gets the value for the {@code maxTotal} configuration attribute
092     * for pools created with this configuration instance.
093     *
094     * @return  The current setting of {@code maxTotal} for this
095     *          configuration instance
096     *
097     * @see GenericObjectPool#getMaxTotal()
098     */
099    public int getMaxTotal() {
100        return maxTotal;
101    }
102
103    /**
104     * Gets the value for the {@code minIdle} configuration attribute
105     * for pools created with this configuration instance.
106     *
107     * @return  The current setting of {@code minIdle} for this
108     *          configuration instance
109     *
110     * @see GenericObjectPool#getMinIdle()
111     */
112    public int getMinIdle() {
113        return minIdle;
114    }
115
116    /**
117     * Sets the value for the {@code maxIdle} configuration attribute for
118     * pools created with this configuration instance.
119     *
120     * @param maxIdle The new setting of {@code maxIdle}
121     *        for this configuration instance
122     *
123     * @see GenericObjectPool#setMaxIdle(int)
124     */
125    public void setMaxIdle(final int maxIdle) {
126        this.maxIdle = maxIdle;
127    }
128
129    /**
130     * Sets the value for the {@code maxTotal} configuration attribute for
131     * pools created with this configuration instance.
132     *
133     * @param maxTotal The new setting of {@code maxTotal}
134     *        for this configuration instance
135     *
136     * @see GenericObjectPool#setMaxTotal(int)
137     */
138    public void setMaxTotal(final int maxTotal) {
139        this.maxTotal = maxTotal;
140    }
141
142    /**
143     * Sets the value for the {@code minIdle} configuration attribute for
144     * pools created with this configuration instance.
145     *
146     * @param minIdle The new setting of {@code minIdle}
147     *        for this configuration instance
148     *
149     * @see GenericObjectPool#setMinIdle(int)
150     */
151    public void setMinIdle(final int minIdle) {
152        this.minIdle = minIdle;
153    }
154
155    @Override
156    protected void toStringAppendFields(final StringBuilder builder) {
157        super.toStringAppendFields(builder);
158        builder.append(", maxTotal=");
159        builder.append(maxTotal);
160        builder.append(", maxIdle=");
161        builder.append(maxIdle);
162        builder.append(", minIdle=");
163        builder.append(minIdle);
164    }
165}