Coverage Report - org.owasp.dependencycheck.dependency.Evidence
 
Classes in this File Line Coverage Branch Coverage Complexity
Evidence
67%
39/58
61%
21/34
2.389
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.dependency;
 19  
 
 20  
 import org.apache.commons.lang.ObjectUtils;
 21  
 import org.apache.commons.lang.StringUtils;
 22  
 
 23  
 import java.io.Serializable;
 24  
 
 25  
 /**
 26  
  * Evidence is a piece of information about a Dependency.
 27  
  *
 28  
  * @author Jeremy Long
 29  
  */
 30  306480
 public class Evidence implements Serializable, Comparable<Evidence> {
 31  
 
 32  
     /**
 33  
      * Used as starting point for generating the value in {@link #hashCode()}.
 34  
      */
 35  
     private static final int MAGIC_HASH_INIT_VALUE = 3;
 36  
 
 37  
     /**
 38  
      * Used as a multiplier for generating the value in {@link #hashCode()}.
 39  
      */
 40  
     private static final int MAGIC_HASH_MULTIPLIER = 67;
 41  
 
 42  
     /**
 43  
      * Creates a new Evidence object.
 44  
      */
 45  0
     public Evidence() {
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Creates a new Evidence objects.
 50  
      *
 51  
      * @param source     the source of the evidence.
 52  
      * @param name       the name of the evidence.
 53  
      * @param value      the value of the evidence.
 54  
      * @param confidence the confidence of the evidence.
 55  
      */
 56  117192
     public Evidence(String source, String name, String value, Confidence confidence) {
 57  117192
         this.source = source;
 58  117192
         this.name = name;
 59  117192
         this.value = value;
 60  117192
         this.confidence = confidence;
 61  117192
     }
 62  
 
 63  
     /**
 64  
      * The name of the evidence.
 65  
      */
 66  
     private String name;
 67  
 
 68  
     /**
 69  
      * Get the value of name.
 70  
      *
 71  
      * @return the value of name
 72  
      */
 73  
     public String getName() {
 74  192
         return name;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Set the value of name.
 79  
      *
 80  
      * @param name new value of name
 81  
      */
 82  
     public void setName(String name) {
 83  0
         this.name = name;
 84  0
     }
 85  
 
 86  
     /**
 87  
      * The source of the evidence.
 88  
      */
 89  
     private String source;
 90  
 
 91  
     /**
 92  
      * Get the value of source.
 93  
      *
 94  
      * @return the value of source
 95  
      */
 96  
     public String getSource() {
 97  88
         return source;
 98  
     }
 99  
 
 100  
     /**
 101  
      * Set the value of source.
 102  
      *
 103  
      * @param source new value of source
 104  
      */
 105  
     public void setSource(String source) {
 106  0
         this.source = source;
 107  0
     }
 108  
 
 109  
     /**
 110  
      * The value of the evidence.
 111  
      */
 112  
     private String value;
 113  
 
 114  
     /**
 115  
      * Get the value of value.
 116  
      *
 117  
      * @return the value of value
 118  
      */
 119  
     public String getValue() {
 120  6200
         used = true;
 121  6200
         return value;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Get the value of value. If setUsed is set to false this call to get will not mark the evidence as used.
 126  
      *
 127  
      * @param setUsed whether or not this call to getValue should cause the used flag to be updated
 128  
      * @return the value of value
 129  
      */
 130  
     public String getValue(Boolean setUsed) {
 131  400
         used = used || setUsed;
 132  400
         return value;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Set the value of value.
 137  
      *
 138  
      * @param value new value of value
 139  
      */
 140  
     public void setValue(String value) {
 141  0
         this.value = value;
 142  0
     }
 143  
 
 144  
     /**
 145  
      * A value indicating if the Evidence has been "used" (aka read).
 146  
      */
 147  
     private boolean used;
 148  
 
 149  
     /**
 150  
      * Get the value of used.
 151  
      *
 152  
      * @return the value of used
 153  
      */
 154  
     public boolean isUsed() {
 155  8656
         return used;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Set the value of used.
 160  
      *
 161  
      * @param used new value of used
 162  
      */
 163  
     public void setUsed(boolean used) {
 164  0
         this.used = used;
 165  0
     }
 166  
 
 167  
     /**
 168  
      * The confidence level for the evidence.
 169  
      */
 170  
     private Confidence confidence;
 171  
 
 172  
     /**
 173  
      * Get the value of confidence.
 174  
      *
 175  
      * @return the value of confidence
 176  
      */
 177  
     public Confidence getConfidence() {
 178  1904
         return confidence;
 179  
     }
 180  
 
 181  
     /**
 182  
      * Set the value of confidence.
 183  
      *
 184  
      * @param confidence new value of confidence
 185  
      */
 186  
     public void setConfidence(Confidence confidence) {
 187  0
         this.confidence = confidence;
 188  0
     }
 189  
 
 190  
     /**
 191  
      * Implements the hashCode for Evidence.
 192  
      *
 193  
      * @return hash code.
 194  
      */
 195  
     @Override
 196  
     public int hashCode() {
 197  56
         int hash = MAGIC_HASH_INIT_VALUE;
 198  56
         hash = MAGIC_HASH_MULTIPLIER * hash + ObjectUtils.hashCode(StringUtils.lowerCase(this.name));
 199  56
         hash = MAGIC_HASH_MULTIPLIER * hash + ObjectUtils.hashCode(StringUtils.lowerCase(this.source));
 200  56
         hash = MAGIC_HASH_MULTIPLIER * hash + ObjectUtils.hashCode(StringUtils.lowerCase(this.value));
 201  56
         hash = MAGIC_HASH_MULTIPLIER * hash + ObjectUtils.hashCode(this.confidence);
 202  56
         return hash;
 203  
     }
 204  
 
 205  
     /**
 206  
      * Implements equals for Evidence.
 207  
      *
 208  
      * @param that an object to check the equality of.
 209  
      * @return whether the two objects are equal.
 210  
      */
 211  
     @Override
 212  
     public boolean equals(Object that) {
 213  80
         if (this == that) {
 214  0
             return true;
 215  
         }
 216  80
         if (!(that instanceof Evidence)) {
 217  0
             return false;
 218  
         }
 219  80
         final Evidence e = (Evidence) that;
 220  
 
 221  80
         return StringUtils.equalsIgnoreCase(name, e.name)
 222  
                 && StringUtils.equalsIgnoreCase(source, e.source)
 223  
                 && StringUtils.equalsIgnoreCase(value, e.value)
 224  
                 && ObjectUtils.equals(confidence, e.confidence);
 225  
     }
 226  
 
 227  
     /**
 228  
      * Implementation of the comparable interface.
 229  
      *
 230  
      * @param o the evidence being compared
 231  
      * @return an integer indicating the ordering of the two objects
 232  
      */
 233  
     public int compareTo(Evidence o) {
 234  306560
         if (o == null) {
 235  0
             return 1;
 236  
         }
 237  306560
         if (StringUtils.equalsIgnoreCase(source, o.source)) {
 238  188912
             if (StringUtils.equalsIgnoreCase(name, o.name)) {
 239  187768
                 if (StringUtils.equalsIgnoreCase(value, o.value)) {
 240  116296
                     if (ObjectUtils.equals(confidence, o.confidence)) {
 241  116184
                         return 0; //they are equal
 242  
                     } else {
 243  112
                         return ObjectUtils.compare(confidence, o.confidence);
 244  
                     }
 245  
                 } else {
 246  71472
                     return compareToIgnoreCaseWithNullCheck(value, o.value);
 247  
                 }
 248  
             } else {
 249  1144
                 return compareToIgnoreCaseWithNullCheck(name, o.name);
 250  
             }
 251  
         } else {
 252  117648
             return compareToIgnoreCaseWithNullCheck(source, o.source);
 253  
         }
 254  
     }
 255  
 
 256  
     /**
 257  
      * Wrapper around {@link java.lang.String#compareToIgnoreCase(java.lang.String) String.compareToIgnoreCase} with an
 258  
      * exhaustive, possibly duplicative, check against nulls.
 259  
      *
 260  
      * @param me    the value to be compared
 261  
      * @param other the other value to be compared
 262  
      * @return true if the values are equal; otherwise false
 263  
      */
 264  
     private int compareToIgnoreCaseWithNullCheck(String me, String other) {
 265  190264
         if (me == null && other == null) {
 266  0
             return 0;
 267  190264
         } else if (me == null) {
 268  0
             return -1; //the other string is greater then me
 269  190264
         } else if (other == null) {
 270  0
             return 1; //me is greater then the other string
 271  
         }
 272  190264
         return me.compareToIgnoreCase(other);
 273  
     }
 274  
 
 275  
     /**
 276  
      * Standard toString() implementation.
 277  
      *
 278  
      * @return the string representation of the object
 279  
      */
 280  
     @Override
 281  
     public String toString() {
 282  0
         return "Evidence{" + "name=" + name + ", source=" + source + ", value=" + value + ", confidence=" + confidence + '}';
 283  
     }
 284  
 }