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    */
017    package org.apache.commons.inject.impl;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.commons.inject.api.IBinding;
024    import org.apache.commons.inject.api.IKey;
025    
026    public class ImmutableBindingSet extends AbstractBindingSet {
027            public ImmutableBindingSet(ResolvableBindingSet pResolvableBindings) {
028                    super(pResolvableBindings.map);
029            }
030    
031            <T> IBinding<T> getBinding(IKey<T> pKey) {
032                    final ReducedKey<T> rkey = new ReducedKey<T>(pKey.getType(), pKey.getName());
033                    final List<BindingAndKey<?>> list = map.get(rkey);
034                    if (list != null) {
035                            for (BindingAndKey<?> bak : list) {
036                                    if (isMatching(pKey, bak.getKey())) {
037                                            @SuppressWarnings("unchecked")
038                                            final IBinding<T> binding = (IBinding<T>) bak.getBinding();
039                                            return binding;
040                                    }
041                            }
042                    }
043                    return null;
044            }
045    
046            <T> void add(ReducedKey<T> pRKey, MappedKey<T> pMKey, IBinding<T> pBinding) {
047                    final List<BindingAndKey<?>> list = findOrCreateList(pRKey);
048                    final BindingAndKey<T> bak = new BindingAndKey<T>(pBinding, pMKey);
049                    list.add(bak);
050            }
051    
052            Iterable<IBinding<?>> getAllBindings() {
053                    final List<IBinding<?>> list = new ArrayList<IBinding<?>>();
054                    for (Map.Entry<ReducedKey<?>,List<BindingAndKey<?>>> en : map.entrySet()) {
055                            final List<BindingAndKey<?>> baklist = en.getValue();
056                            for (BindingAndKey<?> bak : baklist) {
057                                    list.add(bak.getBinding());
058                            }
059                    }
060                    return list;
061            }
062    }