-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab65555
commit e5730a1
Showing
13 changed files
with
864 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
...om/github/alexisjehan/mvncheck/core/component/filter/artifact/WildcardArtifactFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022-2024 Alexis Jehan | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.github.alexisjehan.mvncheck.core.component.filter.artifact; | ||
|
||
import com.github.alexisjehan.javanilla.misc.quality.Ensure; | ||
import com.github.alexisjehan.mvncheck.core.component.artifact.Artifact; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* <p>Class that describes an artifact filter based on expressions that can contain wildcard characters.</p> | ||
* @since 1.7.0 | ||
*/ | ||
public final class WildcardArtifactFilter implements ArtifactFilter { | ||
|
||
/** | ||
* <p>Single character wildcard.</p> | ||
* @since 1.7.0 | ||
*/ | ||
private static final char WILDCARD_SINGLE = '?'; | ||
|
||
/** | ||
* <p>Any characters wildcard.</p> | ||
* @since 1.7.0 | ||
*/ | ||
private static final char WILDCARD_ANY = '*'; | ||
|
||
/** | ||
* <p>{@link Pattern} of the group identifier.</p> | ||
* @since 1.7.0 | ||
*/ | ||
private final Pattern groupIdPattern; | ||
|
||
/** | ||
* <p>{@link Pattern} of the artifact identifier or {@code null}.</p> | ||
* @since 1.7.0 | ||
*/ | ||
private final Pattern artifactIdPattern; | ||
|
||
/** | ||
* <p>{@link Pattern} of the update version or {@code null}.</p> | ||
* @since 1.7.0 | ||
*/ | ||
private final Pattern updateVersionPattern; | ||
|
||
/** | ||
* <p>Constructor with a group identifier expression.</p> | ||
* @param groupIdExpression a group identifier expression | ||
* @throws NullPointerException if the group identifier expression is {@code null} | ||
* @throws IllegalArgumentException if the group identifier expression is empty | ||
* @since 1.7.0 | ||
*/ | ||
public WildcardArtifactFilter( | ||
final String groupIdExpression | ||
) { | ||
this(groupIdExpression, null); | ||
} | ||
|
||
/** | ||
* <p>Constructor with a group identifier expression and an artifact identifier expression.</p> | ||
* @param groupIdExpression a group identifier expression | ||
* @param artifactIdExpression an artifact identifier expression or {@code null} | ||
* @throws NullPointerException if the group identifier expression is {@code null} | ||
* @throws IllegalArgumentException if the group identifier expression or the artifact identifier expression is | ||
* empty | ||
* @since 1.7.0 | ||
*/ | ||
public WildcardArtifactFilter( | ||
final String groupIdExpression, | ||
final String artifactIdExpression | ||
) { | ||
this(groupIdExpression, artifactIdExpression, null); | ||
} | ||
|
||
/** | ||
* <p>Constructor with a group identifier expression, an artifact identifier expression and an update version | ||
* expression.</p> | ||
* @param groupIdExpression a group identifier expression | ||
* @param artifactIdExpression an artifact identifier expression or {@code null} | ||
* @param updateVersionExpression an update version expression or {@code null} | ||
* @throws NullPointerException if the group identifier expression is {@code null} | ||
* @throws IllegalArgumentException if the group identifier expression, the artifact identifier expression or the | ||
* update version expression is empty | ||
* @since 1.7.0 | ||
*/ | ||
public WildcardArtifactFilter( | ||
final String groupIdExpression, | ||
final String artifactIdExpression, | ||
final String updateVersionExpression | ||
) { | ||
Ensure.notNullAndNotEmpty("groupIdExpression", groupIdExpression); | ||
groupIdPattern = createPattern(groupIdExpression); | ||
if (null != artifactIdExpression) { | ||
Ensure.notNullAndNotEmpty("artifactIdExpression", artifactIdExpression); | ||
artifactIdPattern = createPattern(artifactIdExpression); | ||
} else { | ||
artifactIdPattern = null; | ||
} | ||
if (null != updateVersionExpression) { | ||
Ensure.notNullAndNotEmpty("updateVersionExpression", updateVersionExpression); | ||
updateVersionPattern = createPattern(updateVersionExpression); | ||
} else { | ||
updateVersionPattern = null; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* @throws NullPointerException if the artifact is {@code null} | ||
* @since 1.7.0 | ||
*/ | ||
@Override | ||
public boolean accept(final Artifact<?> artifact) { | ||
Ensure.notNull("artifact", artifact); | ||
final var artifactIdentifier = artifact.getIdentifier(); | ||
final var groupId = artifactIdentifier.getGroupId(); | ||
final var artifactId = artifactIdentifier.getArtifactId(); | ||
return groupIdPattern.matcher(groupId).matches() | ||
&& (null == artifactIdPattern || artifactIdPattern.matcher(artifactId).matches()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* @throws NullPointerException if the artifact or the update version is {@code null} | ||
* @throws IllegalArgumentException if the update version is empty | ||
* @since 1.7.0 | ||
*/ | ||
@Override | ||
public boolean accept(final Artifact<?> artifact, final String updateVersion) { | ||
Ensure.notNull("artifact", artifact); | ||
Ensure.notNullAndNotEmpty("updateVersion", updateVersion); | ||
return accept(artifact) | ||
&& (null == updateVersionPattern || updateVersionPattern.matcher(updateVersion).matches()); | ||
} | ||
|
||
/** | ||
* <p>Create a {@link Pattern} from an expression.</p> | ||
* @param expression an expression | ||
* @return the {@link Pattern} | ||
* @since 1.7.0 | ||
*/ | ||
private static Pattern createPattern(final String expression) { | ||
return Pattern.compile( | ||
"^" + Pattern.quote(expression) | ||
.replace(String.valueOf(WILDCARD_SINGLE), "\\E.\\Q") | ||
.replace(String.valueOf(WILDCARD_ANY), "\\E.*\\Q") + "$", | ||
Pattern.CASE_INSENSITIVE | ||
); | ||
} | ||
} |
Oops, something went wrong.