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     */
017    package org.apache.commons.math3.ode;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    
022    /** This abstract class provides boilerplate parameters list.
023     *
024     * @version $Id: AbstractParameterizable.java 1416643 2012-12-03 19:37:14Z tn $
025     * @since 3.0
026     */
027    
028    public abstract class AbstractParameterizable implements Parameterizable {
029    
030       /** List of the parameters names. */
031        private final Collection<String> parametersNames;
032    
033        /** Simple constructor.
034         * @param names names of the supported parameters
035         */
036        protected AbstractParameterizable(final String ... names) {
037            parametersNames = new ArrayList<String>();
038            for (final String name : names) {
039                parametersNames.add(name);
040            }
041        }
042    
043        /** Simple constructor.
044         * @param names names of the supported parameters
045         */
046        protected AbstractParameterizable(final Collection<String> names) {
047            parametersNames = new ArrayList<String>();
048            parametersNames.addAll(names);
049        }
050    
051        /** {@inheritDoc} */
052        public Collection<String> getParametersNames() {
053            return parametersNames;
054        }
055    
056        /** {@inheritDoc} */
057        public boolean isSupported(final String name) {
058            for (final String supportedName : parametersNames) {
059                if (supportedName.equals(name)) {
060                    return true;
061                }
062            }
063            return false;
064        }
065    
066        /** Check if a parameter is supported and throw an IllegalArgumentException if not.
067         * @param name name of the parameter to check
068         * @exception UnknownParameterException if the parameter is not supported
069         * @see #isSupported(String)
070         */
071        public void complainIfNotSupported(final String name)
072            throws UnknownParameterException {
073            if (!isSupported(name)) {
074                throw new UnknownParameterException(name);
075            }
076        }
077    
078    }