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.math4.legacy.ode;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.Collection;
022
023/** This abstract class provides boilerplate parameters list.
024 *
025 * @since 3.0
026 */
027
028public 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<>(Arrays.asList(names));
038    }
039
040    /** Simple constructor.
041     * @param names names of the supported parameters
042     */
043    protected AbstractParameterizable(final Collection<String> names) {
044        parametersNames = new ArrayList<>(names);
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public Collection<String> getParametersNames() {
050        return parametersNames;
051    }
052
053    /** {@inheritDoc} */
054    @Override
055    public boolean isSupported(final String name) {
056        for (final String supportedName : parametersNames) {
057            if (supportedName.equals(name)) {
058                return true;
059            }
060        }
061        return false;
062    }
063
064    /** Check if a parameter is supported and throw an IllegalArgumentException if not.
065     * @param name name of the parameter to check
066     * @exception UnknownParameterException if the parameter is not supported
067     * @see #isSupported(String)
068     */
069    public void complainIfNotSupported(final String name)
070        throws UnknownParameterException {
071        if (!isSupported(name)) {
072            throw new UnknownParameterException(name);
073        }
074    }
075}