001    // Copyright 2007, 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.upload.internal.services;
016    
017    import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newList;
018    
019    import java.util.List;
020    
021    /**
022     * Holds single or multivalued values.
023     */
024    public class ParameterValue
025    {
026        private final List<String> values = newList();
027    
028        public static final ParameterValue NULL = new ParameterValue()
029        {
030            @Override
031            public String single()
032            {
033                return null;
034            }
035    
036            @Override
037            public String[] multi()
038            {
039                return null;
040            }
041        };
042    
043        public ParameterValue(String value)
044        {
045            values.add(value);
046        }
047    
048        public ParameterValue(String... values)
049        {
050            for (String v : values)
051            {
052                add(v);
053            }
054        }
055    
056        /**
057         * @return Single value of parameter (or first value if there are multiple values)
058         */
059        public String single()
060        {
061            return values.get(0);
062        }
063    
064        /**
065         * @return All values of parameter
066         */
067        public String[] multi()
068        {
069            return values.toArray(new String[values.size()]);
070        }
071    
072        public void add(String value)
073        {
074            values.add(value);
075        }
076    
077        public boolean isMulti()
078        {
079            return values.size() > 1;
080        }
081    }