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.wicket.request.parameter;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.LinkedHashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.wicket.request.IRequestParameters;
026import org.apache.wicket.util.lang.Args;
027import org.apache.wicket.util.string.StringValue;
028
029/**
030 * {@link IRequestParameters} implementation that combines other {@link IRequestParameters}s.
031 * 
032 * @author Matej Knopp
033 */
034public class CombinedRequestParametersAdapter implements IRequestParameters
035{
036        private final IRequestParameters parameters[];
037
038        /**
039         * Construct.
040         * 
041         * @param parameters
042         */
043        public CombinedRequestParametersAdapter(final IRequestParameters... parameters)
044        {
045                this.parameters = Args.notNull(parameters, "parameters");
046        }
047
048        /**
049         * @see org.apache.wicket.request.IRequestParameters#getParameterNames()
050         */
051        @Override
052        public Set<String> getParameterNames()
053        {
054                Set<String> result = new LinkedHashSet<>();
055                for (IRequestParameters p : parameters)
056                {
057                        result.addAll(p.getParameterNames());
058                }
059                return Collections.unmodifiableSet(result);
060        }
061
062        /**
063         * @see org.apache.wicket.request.IRequestParameters#getParameterValue(java.lang.String)
064         */
065        @Override
066        public StringValue getParameterValue(final String name)
067        {
068                for (IRequestParameters p : parameters)
069                {
070                        StringValue value = p.getParameterValue(name);
071                        if (!value.isNull())
072                        {
073                                return value;
074                        }
075                }
076                return StringValue.valueOf((String)null);
077        }
078
079        /**
080         * @see org.apache.wicket.request.IRequestParameters#getParameterValues(java.lang.String)
081         */
082        @Override
083        public List<StringValue> getParameterValues(final String name)
084        {
085                List<StringValue> result = new ArrayList<>();
086                for (IRequestParameters p : parameters)
087                {
088                        List<StringValue> values = p.getParameterValues(name);
089                        if (values != null)
090                        {
091                                for (StringValue v : values)
092                                {
093                                        result.add(v);
094                                }
095                        }
096                }
097
098                if (result.isEmpty())
099                {
100                        return null;
101                }
102                else
103                {
104                        return Collections.unmodifiableList(result);
105                }
106        }
107}