001// Copyright 2006, 2007, 2008, 2011, 2012 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.commons.util;
016
017import java.util.Collection;
018import java.util.List;
019import java.util.Map;
020
021import org.apache.tapestry5.commons.internal.util.InheritanceSearch;
022
023/**
024 * A key component in implementing the "Gang of Four" Strategy pattern. A StrategyRegistry will match up a given input
025 * type with a registered strategy for that type.
026 *
027 * @param <A> the type of the strategy adapter
028 */
029public final class StrategyRegistry<A>
030{
031    private final Class<A> adapterType;
032
033    private final boolean allowNonMatch;
034
035    private final Map<Class, A> registrations = CollectionFactory.newMap();
036
037    private final Map<Class, A> cache = CollectionFactory.newConcurrentMap();
038
039    /**
040     * Used to identify types for which there is no matching adapter; we're using it as if it were a ConcurrentSet.
041     */
042    private final Map<Class, Boolean> unmatched = CollectionFactory.newConcurrentMap();
043
044    private StrategyRegistry(Class<A> adapterType, Map<Class, A> registrations, boolean allowNonMatch)
045    {
046        this.adapterType = adapterType;
047        this.allowNonMatch = allowNonMatch;
048
049        this.registrations.putAll(registrations);
050    }
051
052    /**
053     * Creates a strategy registry for the given adapter type. The registry will be configured to require matches.
054     *
055     * @param adapterType   the type of adapter retrieved from the registry
056     * @param registrations map of registrations (the contents of the map are copied)
057     */
058    public static <A> StrategyRegistry<A> newInstance(Class<A> adapterType,
059                                                      Map<Class, A> registrations)
060    {
061        return newInstance(adapterType, registrations, false);
062    }
063
064    /**
065     * Creates a strategy registry for the given adapter type.
066     *
067     * @param adapterType   the type of adapter retrieved from the registry
068     * @param registrations map of registrations (the contents of the map are copied)
069     * @param allowNonMatch if true, then the registry supports non-matches when retrieving an adapter
070     */
071    public static <A> StrategyRegistry<A> newInstance(
072            Class<A> adapterType,
073            Map<Class, A> registrations, boolean allowNonMatch)
074    {
075        return new StrategyRegistry<A>(adapterType, registrations, allowNonMatch);
076    }
077
078    public void clearCache()
079    {
080        cache.clear();
081        unmatched.clear();
082    }
083
084    public Class<A> getAdapterType()
085    {
086        return adapterType;
087    }
088
089    /**
090     * Gets an adapter for an object. Searches based on the value's class, unless the value is null, in which case, a
091     * search on class void is used.
092     *
093     * @param value for which an adapter is needed
094     * @return the adapter for the value or null if not found (and allowNonMatch is true)
095     * @throws IllegalArgumentException if no matching adapter may be found and allowNonMatch is false
096     */
097
098    public A getByInstance(Object value)
099    {
100        return get(value == null ? void.class : value.getClass());
101    }
102
103    /**
104     * Searches for an adapter corresponding to the given input type.
105     *
106     * @param type the type to search
107     * @return the adapter for the type or null if not found (and allowNonMatch is true)
108     * @throws IllegalArgumentException if no matching adapter may be found   and allowNonMatch is false
109     */
110    public A get(Class type)
111    {
112
113        A result = cache.get(type);
114
115        if (result != null) return result;
116
117        if (unmatched.containsKey(type)) return null;
118
119
120        result = findMatch(type);
121
122        // This may be null in the case that there is no match and we're allowing that to not
123        // be an error.  That's why we check via containsKey.
124
125        if (result != null)
126        {
127            cache.put(type, result);
128        } else
129        {
130            unmatched.put(type, true);
131        }
132
133        return result;
134    }
135
136    private A findMatch(Class type)
137    {
138        for (Class t : new InheritanceSearch(type))
139        {
140            A result = registrations.get(t);
141
142            if (result != null) return result;
143        }
144
145        if (allowNonMatch) return null;
146
147        // Report the error. These things really confused the hell out of people in Tap4, so we're
148        // going the extra mile on the exception message.
149
150        List<String> names = CollectionFactory.newList();
151        for (Class t : registrations.keySet())
152            names.add(t.getName());
153
154        throw new UnknownValueException(String.format("No adapter from type %s to type %s is available.", type.getName(), adapterType.getName()), null, null,
155                new AvailableValues("registered types", registrations));
156    }
157
158    /**
159     * Returns the registered types for which adapters are available.
160     */
161    public Collection<Class> getTypes()
162    {
163        return CollectionFactory.newList(registrations.keySet());
164    }
165
166    @Override
167    public String toString()
168    {
169        return String.format("StrategyRegistry[%s]", adapterType.getName());
170    }
171}