1717 */
1818package org .owasp .dependencycheck .xml .suppression ;
1919
20+ import com .google .common .base .Suppliers ;
2021import org .apache .commons .lang3 .builder .EqualsBuilder ;
2122import org .apache .commons .lang3 .builder .HashCodeBuilder ;
2223
23- import java .util .regex .Pattern ;
2424import javax .annotation .concurrent .ThreadSafe ;
25+ import java .util .function .Supplier ;
26+ import java .util .regex .Pattern ;
2527
2628/**
2729 * A simple PropertyType used to represent a string value that could be used as
@@ -37,32 +39,53 @@ public class PropertyType {
3739 /**
3840 * The value.
3941 */
40- private String value ;
42+ private final String value ;
4143 /**
4244 * Whether or not the expression is a regex.
4345 */
44- private boolean regex = false ;
46+ private final boolean regex ;
4547 /**
4648 * Indicates case sensitivity.
4749 */
48- private boolean caseSensitive = false ;
50+ private final boolean caseSensitive ;
51+
52+ private final Supplier <Pattern > compiledRegex = Suppliers
53+ .memoize (() -> isRegex () ? Pattern .compile (getValue (), isCaseSensitive () ? 0 : Pattern .CASE_INSENSITIVE ) : null );
4954
5055 /**
51- * Gets the value of the value property.
52- *
53- * @return the value of the value property
56+ * @param value the value of the value property
57+ * @param regex whether the value is a regex
58+ * @param caseSensitive whether the value is case-sensitive
5459 */
55- public String getValue () {
56- return value ;
60+ public PropertyType (String value , boolean regex , boolean caseSensitive ) {
61+ this .value = value ;
62+ this .regex = regex ;
63+ this .caseSensitive = caseSensitive ;
64+ }
65+
66+ public static PropertyType of (String value ) {
67+ return new PropertyType (value , false , false );
68+ }
69+
70+ public static PropertyType regex (String value ) {
71+ return new PropertyType (value , true , false );
72+ }
73+
74+ public static PropertyType caseSensitive (String value ) {
75+ return new PropertyType (value , false , true );
76+ }
77+
78+ public static PropertyType regexCaseSensitive (String value ) {
79+ return new PropertyType (value , true , true );
5780 }
5881
5982 /**
60- * Sets the value of the value property.
83+ * Gets the value of the value property.
6184 *
62- * @param value the value of the value property
85+ * @return the value of the value property
6386 */
64- public void setValue ( String value ) {
65- this . value = value ;
87+ public String getValue ( ) {
88+ return value ;
6689 }
6790
6891 /**
@@ -74,32 +97,14 @@ public boolean isRegex() {
7497 return regex ;
7598 }
7699
77- /**
78- * Sets whether the value property is a regex.
79- *
80- * @param value true if the value is a regex, otherwise false
81- */
82- public void setRegex (boolean value ) {
83- this .regex = value ;
84- }
85-
86100 /**
87101 * Gets the value of the caseSensitive property.
88102 *
89- * @return true if the value is case sensitive
103+ * @return true if the value is case- sensitive
90104 */
91105 public boolean isCaseSensitive () {
92106 return caseSensitive ;
93107 }
94-
95- /**
96- * Sets the value of the caseSensitive property.
97- *
98- * @param value whether the value is case sensitive
99- */
100- public void setCaseSensitive (boolean value ) {
101- this .caseSensitive = value ;
102- }
103108 //</editor-fold>
104109
105110 /**
@@ -114,13 +119,7 @@ public boolean matches(String text) {
114119 return false ;
115120 }
116121 if (this .regex ) {
117- final Pattern rx ;
118- if (this .caseSensitive ) {
119- rx = Pattern .compile (this .value );
120- } else {
121- rx = Pattern .compile (this .value , Pattern .CASE_INSENSITIVE );
122- }
123- return rx .matcher (text ).matches ();
122+ return compiledRegex .get ().matcher (text ).matches ();
124123 } else {
125124 if (this .caseSensitive ) {
126125 return value .equals (text );
0 commit comments