Skip to content

Commit

Permalink
Use more modern Java constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
ottlinger committed Dec 16, 2024
1 parent 1133e13 commit 78f1d02
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 59 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/apache/creadur/tentacles/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public class Archive {
private final File file;
private final Map<URI, URI> map;

private final Set<License> licenses = new HashSet<License>();
private final Set<Notice> notices = new HashSet<Notice>();
private final Set<License> licenses = new HashSet<>();
private final Set<Notice> notices = new HashSet<>();

private final Set<License> declaredLicenses = new HashSet<License>();
private final Set<Notice> declaredNotices = new HashSet<Notice>();
private final Set<License> declaredLicenses = new HashSet<>();
private final Set<Notice> declaredNotices = new HashSet<>();

private final Set<License> otherLicenses = new HashSet<License>();
private final Set<Notice> otherNotices = new HashSet<Notice>();
private final Set<License> otherLicenses = new HashSet<>();
private final Set<Notice> otherNotices = new HashSet<>();
private Map<URI, URI> others;

public Archive(final File file, final FileSystem fileSystem,
Expand Down Expand Up @@ -109,7 +109,7 @@ private Map<URI, URI> mapOther() {

private Map<URI, URI> buildMapFrom(final File jarContents,
final List<File> legal) {
final Map<URI, URI> map = new LinkedHashMap<URI, URI>();
final Map<URI, URI> map = new LinkedHashMap<>();
for (final File file : legal) {
final URI name = jarContents.toURI().relativize(file.toURI());
final URI link =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void main(final String[] args) throws Exception {
"At least one directory must be specified");
}

final List<File> dirs = new ArrayList<File>();
final List<File> dirs = new ArrayList<>();

// Check the input args upfront
for (final String arg : args) {
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/apache/creadur/tentacles/FileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ public List<File> collect(final File dir, final String regex) {
}

private List<File> collect(final File dir, final Pattern pattern) {
return collect(dir, new FileFilter() {
@Override
public boolean accept(final File file) {
return pattern.matcher(file.getAbsolutePath()).matches();
}
});
return collect(dir, file -> pattern.matcher(file.getAbsolutePath()).matches());
}

public List<File> collect(final File dir, final FileFilter filter) {
final List<File> accepted = new ArrayList<File>();
final List<File> accepted = new ArrayList<>();
if (filter.accept(dir)) {
accepted.add(dir);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/creadur/tentacles/IOSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public void close(final Closeable closeable) throws IOException {
((Flushable) closeable).flush();
}
} catch (final IOException e) {
LOG.trace("Error when trying to flush before closing " + closeable, e);
LOG.trace("Error when trying to flush before closing {}", closeable, e);
}
try {
closeable.close();
} catch (final IOException e) {
LOG.trace("Error when trying to close " + closeable, e);
LOG.trace("Error when trying to close {}", closeable, e);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/creadur/tentacles/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public class License {
private final String text;
private final String key;
private final Set<Archive> archives = new HashSet<Archive>();
private final List<File> locations = new ArrayList<File>();
private final Set<Archive> archives = new HashSet<>();
private final List<File> locations = new ArrayList<>();

public License(final String key, final String text) {
this.text = text;
Expand All @@ -54,7 +54,7 @@ public List<File> getLocations() {

public Set<URI> locations(final Archive archive) {
final URI contents = archive.contentsURI();
final Set<URI> locations = new HashSet<URI>();
final Set<URI> locations = new HashSet<>();
for (final File file : this.locations) {
final URI uri = file.toURI();
final URI relativize = contents.relativize(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum LicenseType {
public static Licenses loadLicensesFrom(final Platform platform)
throws IOException {
final Map<String, String> licenses =
new ConcurrentHashMap<String, String>();
new ConcurrentHashMap<>();
for (final LicenseType type : values()) {
type.putTextInto(licenses, platform.getTentaclesResources());
}
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/org/apache/creadur/tentacles/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ public Main(final Configuration configuration, final Platform platform,

this.reports = new Reports();

log.info("Remote repository: "
+ this.configuration.getStagingRepositoryURI());
log.info("Local root directory: " + this.layout.getLocalRootDirectory());
log.info("Remote repository: {}", this.configuration.getStagingRepositoryURI());
log.info("Local root directory: {}", this.layout.getLocalRootDirectory());

this.tentaclesResources.copyTo("legal/style.css",
new File(this.layout.getOutputDirectory(), "style.css"));
Expand Down Expand Up @@ -116,7 +115,7 @@ private void main() throws Exception {
private List<Archive> archivesIn(final File repository) {
final List<File> jars = this.fileSystem.documentsFrom(repository);

final List<Archive> archives = new ArrayList<Archive>();
final List<Archive> archives = new ArrayList<>();
for (final File file : jars) {
final Archive archive =
new Archive(file, this.fileSystem, this.layout);
Expand Down Expand Up @@ -152,7 +151,7 @@ private void reportLicenses(final List<Archive> archives)
}

private void initLicenses(final List<Archive> archives) throws IOException {
final Map<License, License> licenses = new HashMap<License, License>();
final Map<License, License> licenses = new HashMap<>();

for (final Archive archive : archives) {
final List<File> files =
Expand All @@ -174,7 +173,7 @@ private void initLicenses(final List<Archive> archives) throws IOException {
}

private Collection<License> getLicenses(final List<Archive> archives) {
final Set<License> licenses = new LinkedHashSet<License>();
final Set<License> licenses = new LinkedHashSet<>();
for (final Archive archive : archives) {
licenses.addAll(archive.getLicenses());
}
Expand Down Expand Up @@ -202,7 +201,7 @@ private void reportDeclaredLicenses(final List<Archive> archives)

private void classifyLicenses(final Archive archive) throws IOException {
final Set<License> undeclared =
new HashSet<License>(archive.getLicenses());
new HashSet<>(archive.getLicenses());

final File contents = archive.contentsDirectory();
final List<File> files = this.fileSystem.licensesDeclaredIn(contents);
Expand All @@ -216,7 +215,7 @@ private void classifyLicenses(final Archive archive) throws IOException {
archive.getOtherLicenses().addAll(undeclared);

final Set<License> declared =
new HashSet<License>(archive.getLicenses());
new HashSet<>(archive.getLicenses());
declared.removeAll(undeclared);
archive.getDeclaredLicenses().addAll(declared);

Expand All @@ -236,7 +235,7 @@ private void reportDeclaredNotices(final List<Archive> archives)
for (final Archive archive : archives) {

final Set<Notice> undeclared =
new HashSet<Notice>(archive.getNotices());
new HashSet<>(archive.getNotices());

final File contents = archive.contentsDirectory();
final List<File> files =
Expand All @@ -252,7 +251,7 @@ private void reportDeclaredNotices(final List<Archive> archives)
archive.getOtherNotices().addAll(undeclared);

final Set<Notice> declared =
new HashSet<Notice>(archive.getNotices());
new HashSet<>(archive.getNotices());
declared.removeAll(undeclared);
archive.getDeclaredNotices().addAll(declared);

Expand All @@ -275,7 +274,7 @@ private void reportDeclaredNotices(final List<Archive> archives)
}

private void reportNotices(final List<Archive> archives) throws IOException {
final Map<Notice, Notice> notices = new HashMap<Notice, Notice>();
final Map<Notice, Notice> notices = new HashMap<>();

for (final Archive archive : archives) {
final List<File> noticeDocuments =
Expand Down Expand Up @@ -311,7 +310,7 @@ private void unpackContents(final Set<File> files) throws IOException {

private Set<File> mirrorRepositoryFrom(final Configuration configuration)
throws IOException {
final Set<File> files = new HashSet<File>();
final Set<File> files = new HashSet<>();
if (HTTP.isRepositoryFor(configuration)) {
final NexusClient client = new NexusClient(this.platform);
final Set<URI> resources =
Expand All @@ -337,7 +336,7 @@ private Set<File> mirrorRepositoryFrom(final Configuration configuration)
}

private void unpack(final File archive) throws IOException {
log.info("Unpack " + archive);
log.info("Unpack {}", archive);

try {
final ZipInputStream zip = this.ioSystem.unzip(archive);
Expand Down Expand Up @@ -373,7 +372,7 @@ private void unpack(final File archive) throws IOException {
this.ioSystem.close(zip);
}
} catch (final IOException e) {
log.error("Not a zip " + archive);
log.error("Not a zip {}", archive);
}
}

Expand All @@ -382,7 +381,7 @@ private File copyToMirror(final File src) throws IOException {

final File file = mirroredFrom(uri);

log.info("Copy " + uri);
log.info("Copy {}", uri);

this.fileSystem.mkparent(file);

Expand Down
24 changes: 7 additions & 17 deletions src/main/java/org/apache/creadur/tentacles/NexusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,20 @@ public File download(final URI uri, final File file) throws IOException {
final long length = getContentLength(uri);

if (file.length() == length) {
log.info("Exists " + uri);
log.info("Exists {}", uri);
return file;
} else {
log.info("Incomplete " + uri);
log.info("Incomplete {}", uri);
}
}

log.info("Download " + uri);
log.info("Download {}", uri);

final CloseableHttpResponse response = get(uri);

InputStream content = null;
try {
content = response.getEntity().getContent();
try (CloseableHttpResponse response = get(uri); InputStream content = response.getEntity().getContent()) {

this.fileSystem.mkparent(file);

this.ioSystem.copy(content, file);
} finally {
if (content != null) {
content.close();
}

response.close();
}

return file;
Expand Down Expand Up @@ -135,15 +125,15 @@ private CloseableHttpResponse get(final HttpUriRequest request, int tries) throw
}

public Set<URI> crawl(final URI index) throws IOException {
log.info("Crawl " + index);
final Set<URI> resources = new LinkedHashSet<URI>();
log.info("Crawl {}", index);
final Set<URI> resources = new LinkedHashSet<>();

final CloseableHttpResponse response = get(index);

final InputStream content = response.getEntity().getContent();
final StreamLexer lexer = new StreamLexer(content);

final Set<URI> crawl = new LinkedHashSet<URI>();
final Set<URI> crawl = new LinkedHashSet<>();

// <a
// href="https://repository.apache.org/content/repositories/orgapacheopenejb-094/archetype-catalog.xml">archetype-catalog.xml</a>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/creadur/tentacles/Notice.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public class Notice {
private final String text;
private final String key;
private final Set<Archive> archives = new HashSet<Archive>();
private final List<File> locations = new ArrayList<File>();
private final Set<Archive> archives = new HashSet<>();
private final List<File> locations = new ArrayList<>();

public Notice(final String text) {
this.text = text.intern();
Expand All @@ -54,7 +54,7 @@ public List<File> getLocations() {

public Set<URI> locations(final Archive archive) {
final URI contents = archive.contentsURI();
final Set<URI> locations = new HashSet<URI>();
final Set<URI> locations = new HashSet<>();
for (final File file : this.locations) {
final URI uri = file.toURI();
final URI relativize = contents.relativize(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TemplateBuilder {
private final IOSystem ioSystem;
private final String templateName;
private final Map<String, Object> templateContextMap =
new ConcurrentHashMap<String, Object>();
new ConcurrentHashMap<>();

public TemplateBuilder(final String template, final IOSystem ioSystem,
final VelocityEngine engine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

final class AndFilter implements FileFilter {

List<FileFilter> filters = new ArrayList<FileFilter>();
List<FileFilter> filters = new ArrayList<>();

AndFilter(final FileFilter... filters) {
for (final FileFilter filter : filters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@XmlRootElement
public class Archives {

private final List<Item> archives = new ArrayList<Item>();
private final List<Item> archives = new ArrayList<>();

/**
* Required for JAXB
Expand Down

0 comments on commit 78f1d02

Please sign in to comment.