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.chain.web;
018
019import java.util.Enumeration;
020import java.util.Objects;
021import java.util.function.Function;
022import java.util.function.Supplier;
023
024/**
025 * Implementation of {@code Map} for immutable parameter name-values with
026 * a parameter-provider.
027 *
028 * @param <P> the type of the parameter-provider
029 *
030 * @author Graff Stefan
031 * @since Chain 1.3
032 */
033public class ParamValuesMap<P> extends ParameterMap<P, String[]> {
034
035    /**
036     * The constructor for an immutable parameter-map.
037     *
038     * @param parameter the parameter-provider
039     * @param valueFunction Function to return the value of a parameter
040     * @param namesSupplier Supplier to return all names of the parameter
041     */
042    public ParamValuesMap(final P parameter, final Function<String, String[]> valueFunction,
043            final Supplier<Enumeration<String>> namesSupplier) {
044
045        super(parameter, valueFunction, namesSupplier);
046    }
047
048    /**
049     * Returns {@code true} if this parameter-map maps one or more keys
050     * to the specified value.
051     *
052     * @param value value whose presence in this parameter-map is to be
053     *        tested
054     *
055     * @return {@code true} if this parameter-map maps one or more keys
056     *         to the specified value
057     */
058    @Override
059    public boolean containsValue(Object value) {
060        if (!(value instanceof String[])) {
061            return false;
062        }
063
064        final Enumeration<String> keys = getNamesSupplier().get();
065        while (keys.hasMoreElements()) {
066            final String[] next = getValueFunction().apply(keys.nextElement());
067            if (Objects.deepEquals(value, next)) {
068                return true;
069            }
070        }
071        return false;
072    }
073}