-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1043 from jqno/rethink-forpackage-api
Rethink forpackage api
- Loading branch information
Showing
14 changed files
with
443 additions
and
95 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
59 changes: 59 additions & 0 deletions
59
equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ScanOption.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,59 @@ | ||
package nl.jqno.equalsverifier; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Contains a number of options that can be set in {@link EqualsVerifier#forPackage(String, ScanOption...)}. These | ||
* options affect the way in which EqualsVerifier scans the given package. | ||
*/ | ||
public interface ScanOption { | ||
|
||
/** | ||
* Signals that not just the given package should be scanned, but also all of its sub-packages. | ||
* | ||
* @return The 'recursive' flag. | ||
*/ | ||
public static ScanOption recursive() { | ||
return ScanOptions.O.RECURSIVE; | ||
} | ||
|
||
/** | ||
* Signals that packages from external jars, which can't be scanned, will be ignored rather than throw an exception. | ||
* | ||
* @return The 'ignore external jars' flag. | ||
*/ | ||
public static ScanOption ignoreExternalJars() { | ||
return ScanOptions.O.IGNORE_EXTERNAL_JARS; | ||
} | ||
|
||
/** | ||
* Finds only classes that extend or implement the given type. | ||
* | ||
* @param type The type that all classes must extend or implement. | ||
* @return The 'mustExtend' flag with the associated type. | ||
*/ | ||
public static ScanOption mustExtend(Class<?> type) { | ||
return new ScanOptions.MustExtend(type); | ||
} | ||
|
||
/** | ||
* Removes the given type or types from the list of types to verify. | ||
* | ||
* @param type A type to remove from the list of types to verify. | ||
* @param more More types to remove from the list of types to verify. | ||
* @return The 'except' flag with the associated types. | ||
*/ | ||
public static ScanOption except(Class<?> type, Class<?>... more) { | ||
return new ScanOptions.ExceptClasses(type, more); | ||
} | ||
|
||
/** | ||
* Removes all types matching the given Predicate. | ||
* | ||
* @param exclusionPredicate A Predicate matching classes to remove from the list of types to verify. | ||
* @return The 'except' flag with the associated Predicate. | ||
*/ | ||
public static ScanOption except(Predicate<Class<?>> exclusionPredicate) { | ||
return new ScanOptions.ExclusionPredicate(exclusionPredicate); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ScanOptions.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,66 @@ | ||
package nl.jqno.equalsverifier; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
|
||
import nl.jqno.equalsverifier.internal.reflection.PackageScanOptions; | ||
|
||
final class ScanOptions { | ||
private ScanOptions() {} | ||
|
||
enum O implements ScanOption { | ||
RECURSIVE, IGNORE_EXTERNAL_JARS; | ||
} | ||
|
||
static class MustExtend implements ScanOption { | ||
final Class<?> type; | ||
|
||
MustExtend(Class<?> type) { | ||
Objects.requireNonNull(type); | ||
this.type = type; | ||
} | ||
} | ||
|
||
static class ExceptClasses implements ScanOption { | ||
final Set<Class<?>> types; | ||
|
||
ExceptClasses(Class<?> type, Class<?>... more) { | ||
this.types = new HashSet<>(); | ||
this.types.add(type); | ||
this.types.addAll(Arrays.asList(more)); | ||
} | ||
} | ||
|
||
static class ExclusionPredicate implements ScanOption { | ||
final Predicate<Class<?>> exclusionPredicate; | ||
|
||
ExclusionPredicate(Predicate<Class<?>> exclusionPredicate) { | ||
this.exclusionPredicate = exclusionPredicate; | ||
} | ||
} | ||
|
||
public static PackageScanOptions process(ScanOption... options) { | ||
PackageScanOptions result = new PackageScanOptions(); | ||
for (ScanOption option : options) { | ||
if (option.equals(O.RECURSIVE)) { | ||
result.scanRecursively = true; | ||
} | ||
if (option.equals(O.IGNORE_EXTERNAL_JARS)) { | ||
result.ignoreExternalJars = true; | ||
} | ||
if (option instanceof MustExtend) { | ||
MustExtend me = (MustExtend) option; | ||
result.mustExtend = me.type; | ||
} | ||
if (option instanceof ExceptClasses) { | ||
ExceptClasses ec = (ExceptClasses) option; | ||
result.exceptClasses.addAll(ec.types); | ||
} | ||
if (option instanceof ExclusionPredicate) { | ||
ExclusionPredicate ep = (ExclusionPredicate) option; | ||
result.exclusionPredicate = ep.exclusionPredicate; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
...ier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/PackageScanOptions.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,14 @@ | ||
package nl.jqno.equalsverifier.internal.reflection; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
public class PackageScanOptions { | ||
|
||
public boolean scanRecursively = false; | ||
public boolean ignoreExternalJars = false; | ||
public Class<?> mustExtend = null; | ||
public Set<Class<?>> exceptClasses = new HashSet<>(); | ||
public Predicate<Class<?>> exclusionPredicate = c -> false; | ||
} |
Oops, something went wrong.