Coverage Report - org.apache.commons.inject.api.Key
 
Classes in this File Line Coverage Branch Coverage Complexity
Key
88%
32/36
71%
10/14
2
 
 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.api;
 18  
 
 19  
 import java.lang.annotation.Annotation;
 20  
 import java.lang.reflect.Array;
 21  
 
 22  
 public class Key<T> implements IKey<T> {
 23  
         public static final String NO_NAME = "";
 24  1
         public static final Annotation[] NO_ANNOTATIONS = (Annotation[]) Array.newInstance(Annotation.class, 0);
 25  
         private final Class<T> type;
 26  
         private final String name;
 27  
         private final Annotation[] annotations;
 28  
 
 29  393
         public Key(Class<T> pType, String pName, Annotation[] pAnnotations) {
 30  393
                 if (pType == null) {
 31  0
                         throw new NullPointerException("The keys type must not be null.");
 32  
                 }
 33  393
                 this.type = pType;
 34  393
                 if (pName == null) {
 35  0
                         name = NO_NAME;
 36  
                 } else {
 37  393
                         name = pName;
 38  
                 }
 39  393
                 if (pAnnotations == null) {
 40  0
                         annotations = NO_ANNOTATIONS;
 41  
                 } else {
 42  393
                         annotations = pAnnotations;
 43  
                 }
 44  393
         }
 45  
 
 46  
         public Key(Class<T> pType, String pName) {
 47  193
                 this(pType, pName, NO_ANNOTATIONS);
 48  193
         }
 49  
 
 50  
         public Key(Class<T> pType) {
 51  16
                 this(pType, NO_NAME, NO_ANNOTATIONS);
 52  16
         }
 53  
 
 54  
         @Override
 55  
         public Class<T> getType() {
 56  396
                 return type;
 57  
         }
 58  
 
 59  
         @Override
 60  
         public String getName() {
 61  396
                 return name;
 62  
         }
 63  
 
 64  
         @Override
 65  
         public Annotation[] getAnnotations() {
 66  477
                 return annotations;
 67  
         }
 68  
 
 69  
         @Override
 70  
         public String toString() {
 71  3
                 return toString(this);
 72  
         }
 73  
 
 74  
         public static String toString(IKey<?> pKey) {
 75  3
                 final StringBuilder sb = new StringBuilder();
 76  3
                 sb.append("Type=");
 77  3
                 sb.append(pKey.getType().getName());
 78  3
                 final String name = pKey.getName();
 79  3
                 if (name.length() > 0) {
 80  2
                         sb.append(", Name=");
 81  2
                         sb.append(name);
 82  
                 }
 83  3
                 final Annotation[] annotations = pKey.getAnnotations();
 84  3
                 if (annotations.length > 0) {
 85  1
                         sb.append(", Annotations=[");
 86  2
                         for (int i = 0;  i < annotations.length;  i++) {
 87  1
                                 if (i > 0) {
 88  0
                                         sb.append(",");
 89  
                                 }
 90  1
                                 sb.append(annotations[i]);
 91  
                         }
 92  1
                         sb.append("]");
 93  
                 }
 94  3
                 return sb.toString();
 95  
         }
 96  
 }