View Javadoc
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) 2018 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.dependency;
19  
20  import java.io.Serializable;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  import org.owasp.dependencycheck.data.cwe.CweDB;
28  
29  /**
30   * Collection of CWEs with a pretty print implemented in the toString().
31   *
32   * @author Jeremy Long
33   */
34  public class CweSet implements Serializable {
35  
36      /**
37       * Serial version UID.
38       */
39      private static final long serialVersionUID = 7884812602736995362L;
40  
41      /**
42       * The set of CWE entries.
43       */
44      private final Set<String> cwes = new HashSet<>();
45  
46      /**
47       * Get the value of CWEs.
48       *
49       * @return the value of CWEs
50       */
51      public Set<String> getEntries() {
52          return cwes;
53      }
54  
55      /**
56       * Adds a CWE to the set.
57       *
58       * @param cwe new CWE to add
59       */
60      public void addCwe(String cwe) {
61          if (cwe != null) {
62              this.cwes.add(cwe);
63          }
64      }
65  
66      @Override
67      public String toString() {
68          if (cwes.isEmpty()) {
69              return "";
70          }
71          return cwes.stream().map(CweDB::getFullName).collect(Collectors.joining(", "));
72      }
73  
74      /**
75       * Streams the CWEs.
76       *
77       * @return the stream of CWE
78       */
79      public Stream<String> stream() {
80          return cwes.stream();
81      }
82  
83      /**
84       * Returns a map of CWE-ID and title.
85       *
86       * @return a map of CWE-ID and title.
87       */
88      public Map<String, String> getFullCwes() {
89          final Map<String, String> map = new HashMap<>();
90          cwes.forEach((cwe) -> map.put(cwe, CweDB.getName(cwe)));
91          return map;
92      }
93  
94      /**
95       * Returns <code>true</code> if there are no CWEs; otherwise
96       * <code>false</code>.
97       *
98       * @return  <code>true</code> if there are no CWEs; otherwise
99       * <code>false</code>
100      */
101     public boolean isEmpty() {
102         return cwes.isEmpty();
103     }
104 }