Coverage Report - org.apache.commons.inject.impl.AbstractBindingSet
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractBindingSet
68%
22/32
41%
10/24
2,294
AbstractBindingSet$BindingAndKey
75%
6/8
N/A
2,294
AbstractBindingSet$MappedKey
100%
4/4
N/A
2,294
AbstractBindingSet$ReducedKey
80%
12/15
50%
5/10
2,294
 
 1  
 /**
 2  
  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  contributor license agreements.  See the NOTICE file distributed with
 4  
  this work for additional information regarding copyright ownership.
 5  
  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  (the "License"); you may not use this file except in compliance with
 7  
  the License.  You may obtain a copy of the License at
 8  
 
 9  
       http://www.apache.org/licenses/LICENSE-2.0
 10  
 
 11  
  Unless required by applicable law or agreed to in writing, software
 12  
  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  See the License for the specific language governing permissions and
 15  
  limitations under the License.
 16  
 */
 17  
 package org.apache.commons.inject.impl;
 18  
 
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.util.ArrayList;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.commons.inject.api.IBinding;
 26  
 import org.apache.commons.inject.api.IKey;
 27  
 import org.apache.commons.inject.api.Key;
 28  
 
 29  
 /**
 30  
  * A set of bindings, which are being collected to create, or implement
 31  
  * an {@link IInjector}.
 32  
  */
 33  
 
 34  
 public class AbstractBindingSet {
 35  
         /**
 36  
          * The internal map of bindings uses this key.
 37  
          */
 38  
         public static class MappedKey<T> extends Key<T> {
 39  
                 private final Class<? extends Annotation> annotationType;
 40  
                 public MappedKey(Class<T> pType, String pName,
 41  
                                          Annotation[] pAnnotations,
 42  
                                          Class<? extends Annotation> pAnnotationType) {
 43  183
                         super(pType, pName, pAnnotations);
 44  183
                         annotationType = pAnnotationType;
 45  183
                 }
 46  
 
 47  
                 public Class<? extends Annotation> getAnnotationType() {
 48  162
                         return annotationType;
 49  
                 }
 50  
         }
 51  
         /**
 52  
          * The internal map of bindings uses this value.
 53  
          */
 54  
         protected static class BindingAndKey<T> {
 55  
                 private IBinding<T> binding;
 56  
                 private final MappedKey<T> key;
 57  
 
 58  59
                 BindingAndKey(IBinding<T> pBinding, MappedKey<T> pKey) {
 59  59
                         binding = pBinding;
 60  59
                         key = pKey;
 61  59
                 }
 62  
 
 63  
                 public IBinding<T> getBinding() {
 64  280
                         return binding;
 65  
                 }
 66  
 
 67  
                 public void setBinding(IBinding<T> pBinding) {
 68  0
                         binding = pBinding;
 69  0
                 }
 70  
 
 71  
                 public MappedKey<T> getKey() {
 72  205
                         return key;
 73  
                 }
 74  
         }
 75  
         protected static class ReducedKey<T> {
 76  
                 private final Class<T> type;
 77  
                 private final String name;
 78  210
                 ReducedKey(Class<T> pType, String pName) {
 79  210
                         type = pType;
 80  210
                         name = pName;
 81  210
                 }
 82  
                 public Class<T> getType() {
 83  326
                         return type;
 84  
                 }
 85  
                 public String getName() {
 86  326
                         return name;
 87  
                 }
 88  
                 @Override
 89  
                 public int hashCode() {
 90  255
                         return 31 * (31 + name.hashCode()) + type.hashCode();
 91  
                 }
 92  
 
 93  
                 @Override
 94  
                 public boolean equals(Object obj) {
 95  163
                         if (this == obj)
 96  0
                                 return true;
 97  163
                         if (obj == null)
 98  0
                                 return false;
 99  163
                         if (getClass() != obj.getClass())
 100  0
                                 return false;
 101  163
                         ReducedKey<?> other = (ReducedKey<?>) obj;
 102  163
                         return getType() == other.getType() &&  getName().equals(other.getName());
 103  
                 }
 104  
 
 105  
                 
 106  
         }
 107  
 
 108  
         protected final Map<ReducedKey<?>, List<BindingAndKey<?>>> map;
 109  
 
 110  27
         protected AbstractBindingSet(Map<ReducedKey<?>, List<BindingAndKey<?>>> pMap) {
 111  27
                 map = pMap;
 112  27
         }
 113  
 
 114  
         protected AbstractBindingSet() {
 115  9
                 this(new HashMap<ReducedKey<?>, List<BindingAndKey<?>>>());
 116  9
         }
 117  
 
 118  
 
 119  
         protected <T> ReducedKey<T> newReducedKey(IKey<T> pKey) {
 120  183
                 return new ReducedKey<T>(pKey.getType(), pKey.getName());
 121  
         }
 122  
 
 123  
         protected List<BindingAndKey<?>> findOrCreateList(ReducedKey<?> pKey) {
 124  183
                 List<BindingAndKey<?>> list = map.get(pKey);
 125  183
                 if (list == null) {
 126  45
                         list = new ArrayList<BindingAndKey<?>>();
 127  45
                         map.put(pKey, list);
 128  
                 }
 129  183
                 return list;
 130  
         }
 131  
 
 132  
         protected boolean isMatching(IKey<?> pSearchKey, MappedKey<?> pMapKey) {
 133  
                 // No need to compare type and name. They are matching, because
 134  
                 // we did a lookup with a ReducedKey to find the list of
 135  
                 // bindings and keys, from which pMapKey was taken.
 136  163
                 if (!hasAnnotations(pMapKey.getAnnotations(), pSearchKey)
 137  
                         ||  !hasAnnotations(pSearchKey.getAnnotations(), pMapKey)) {
 138  1
                         return false;
 139  
                 }
 140  162
                 final Class<? extends Annotation> mappedAnnotationType = pMapKey.getAnnotationType();
 141  162
                 if (mappedAnnotationType != null) {
 142  0
                         boolean found = false;
 143  0
                         for (Annotation searchAnnotation : pSearchKey.getAnnotations()) {
 144  0
                                 if (searchAnnotation != null  &&  mappedAnnotationType == searchAnnotation.getClass()) {
 145  0
                                         found = true;
 146  0
                                         break;
 147  
                                 }
 148  
                         }
 149  0
                         if (!found) {
 150  0
                                 return false;
 151  
                         }
 152  
                         
 153  
                 }
 154  162
                 return true;
 155  
         }
 156  
 
 157  
         private boolean hasAnnotations(Annotation[] pAnnotations, IKey<?> pKey) {
 158  326
                 for (Annotation annotation : pAnnotations) {
 159  1
                         boolean found = false;
 160  1
                         for (Annotation ann : pKey.getAnnotations()) {
 161  0
                                 if (annotation.equals(ann)) {
 162  0
                                         found = true;
 163  0
                                         break;
 164  
                                 }
 165  
                         }
 166  1
                         if (!found) {
 167  1
                                 return false;
 168  
                         }
 169  
                 }
 170  325
                 return true;
 171  
         }
 172  
 }