Fasten version item construction

This commit is contained in:
huangyuhui
2018-06-08 12:45:44 +08:00
parent 72871e6d9a
commit 118a6cf0d1
10 changed files with 174 additions and 110 deletions

View File

@@ -30,6 +30,7 @@ import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Stream;
/**
* An implementation of classic Minecraft game repository.
@@ -170,57 +171,50 @@ public class DefaultGameRepository implements GameRepository {
File[] files = new File(getBaseDirectory(), "versions").listFiles();
if (files != null)
for (File dir : files)
if (dir.isDirectory()) {
if (Thread.interrupted()) {
this.versions = new HashMap<>();
loaded = false;
return;
}
Arrays.stream(files).parallel().filter(File::isDirectory).flatMap(dir -> {
String id = dir.getName();
File json = new File(dir, id + ".json");
String id = dir.getName();
File json = new File(dir, id + ".json");
// If user renamed the json file by mistake or created the json file in a wrong name,
// we will find the only json and rename it to correct name.
if (!json.exists()) {
List<File> jsons = FileUtils.listFilesByExtension(dir, "json");
if (jsons.size() == 1)
if (!jsons.get(0).renameTo(json)) {
Logging.LOG.warning("Cannot rename json file " + jsons.get(0) + " to " + json + ", ignoring version " + id);
return Stream.empty();
}
}
// If user renamed the json file by mistake or created the json file in a wrong name,
// we will find the only json and rename it to correct name.
if (!json.exists()) {
List<File> jsons = FileUtils.listFilesByExtension(dir, "json");
if (jsons.size() == 1)
if (!jsons.get(0).renameTo(json)) {
Logging.LOG.warning("Cannot rename json file " + jsons.get(0) + " to " + json + ", ignoring version " + id);
continue;
}
}
Version version;
try {
version = Objects.requireNonNull(readVersionJson(json));
} catch (Exception e) {
// JsonSyntaxException or IOException or NullPointerException(!!)
if (EventBus.EVENT_BUS.fireEvent(new GameJsonParseFailedEvent(this, json, id)) != Event.Result.ALLOW)
return Stream.empty();
Version version;
try {
version = Objects.requireNonNull(readVersionJson(json));
} catch (Exception e) {
// JsonSyntaxException or IOException or NullPointerException(!!)
if (EventBus.EVENT_BUS.fireEvent(new GameJsonParseFailedEvent(this, json, id)) != Event.Result.ALLOW)
continue;
try {
version = Objects.requireNonNull(readVersionJson(json));
} catch (Exception e2) {
Logging.LOG.log(Level.SEVERE, "User corrected version json is still malformed");
continue;
}
} catch (Exception e2) {
Logging.LOG.log(Level.SEVERE, "User corrected version json is still malformed");
return Stream.empty();
}
if (!id.equals(version.getId())) {
version = version.setId(id);
try {
FileUtils.writeText(json, Constants.GSON.toJson(version));
} catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it.");
continue;
}
}
provider.addVersion(version);
}
if (!id.equals(version.getId())) {
version = version.setId(id);
try {
FileUtils.writeText(json, Constants.GSON.toJson(version));
} catch (Exception e) {
Logging.LOG.log(Level.WARNING, "Ignoring version " + id + " because wrong id " + version.getId() + " is set and cannot correct it.");
return Stream.empty();
}
}
return Stream.of(version);
}).forEachOrdered(provider::addVersion);
for (Version version : provider.getVersionMap().values()) {
try {
Version resolved = version.resolve(provider);

View File

@@ -28,7 +28,7 @@ import java.util.Collection;
*/
public final class ParallelTask extends Task {
private final Task[] tasks;
private final Collection<Task> tasks;
/**
* Constructor.
@@ -36,6 +36,11 @@ public final class ParallelTask extends Task {
* @param tasks the tasks that can be executed parallelly.
*/
public ParallelTask(Task... tasks) {
this.tasks = Arrays.asList(tasks);
setSignificance(TaskSignificance.MINOR);
}
public ParallelTask(Collection<Task> tasks) {
this.tasks = tasks;
setSignificance(TaskSignificance.MINOR);
}
@@ -46,7 +51,7 @@ public final class ParallelTask extends Task {
@Override
public Collection<Task> getDependents() {
return Arrays.asList(tasks);
return tasks;
}
}