001// Copyright 2010 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
015package org.apache.tapestry5.ioc.util;
016
017import java.util.Collection;
018import java.util.Collections;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
023import org.apache.tapestry5.ioc.internal.util.InternalUtils;
024
025/**
026 * Used (as part of a {@link UnknownValueException} to identify what available values
027 * are present.
028 * 
029 * @since 5.2.0
030 */
031public class AvailableValues
032{
033    private final String valueType;
034
035    private final List<String> values;
036
037    /**
038     * @param valueType
039     *            a word or phrase that describes what the values are such as "component types" or "service ids"
040     *@param values
041     *            a set of objects defining the values; the values will be converted to strings and sorted into
042     *            ascending order
043     */
044    public AvailableValues(String valueType, Collection<?> values)
045    {
046        this.valueType = valueType;
047        this.values = sortValues(values);
048    }
049
050    public AvailableValues(String valueType, Map<?, ?> map)
051    {
052        this(valueType, map.keySet());
053    }
054
055    private static List<String> sortValues(Collection<?> values)
056    {
057        List<String> result = CollectionFactory.newList();
058
059        for (Object v : values)
060        {
061            result.add(String.valueOf(v));
062        }
063
064        Collections.sort(result);
065
066        return Collections.unmodifiableList(result);
067    }
068
069    /** The type of value, i.e., "component types" or "service ids". */
070    public String getValueType()
071    {
072        return valueType;
073    }
074
075    /** The values, as strings, in sorted order. */
076    public List<String> getValues()
077    {
078        return values;
079    }
080
081    @Override
082    public String toString()
083    {
084        return String.format("AvailableValues[%s: %s]", valueType, InternalUtils.join(values));
085    }
086
087}