Skip to content

Commit

Permalink
Using lambda wherever possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
kelemen committed Jul 5, 2017
1 parent 240c5e6 commit 6bb6338
Show file tree
Hide file tree
Showing 168 changed files with 1,608 additions and 3,691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ public void updateSettingsFile(NbGradleModel model) {
ExceptionHelper.checkNotNullArgument(model, "model");
setAllSettingsDef(model, getStamp());

settingsDefPersistor.execute(new Runnable() {
@Override
public void run() {
persistSettingsDefsNow();
}
});
settingsDefPersistor.execute(this::persistSettingsDefsNow);
}

private void persistSettingsDefsNow() {
Expand All @@ -146,33 +141,30 @@ private void persistSettingsDefsNow0() throws IOException {
outstandingDefsLock.unlock();
}

getLocker().doWrite(new IoTask<Void>() {
@Override
public Void run() throws IOException {
MessageDigest hashCalculator = getNameHasher();
for (SettingsDef def: toSave) {
Path savePath = tryGetProjectSaveFile(def.projectDir, hashCalculator);
if (savePath == null) {
LOGGER.log(Level.WARNING, "Cannot save settings.gradle location for projects.");
break;
}

Path settingsGradle = def.settingsGradleDef.getSettingsGradle();

Properties output = new Properties();
output.put("projectDir", def.projectDir.toString());
output.put("rootProjectDir", def.rootProjectDir.toString());
output.put("maySearchUpwards", Boolean.toString(def.settingsGradleDef.isMaySearchUpwards()));
output.put("settingsGradle", settingsGradle != null ? settingsGradle.toString() : "");
output.put("stamp", def.stamp);

Files.createDirectories(savePath.getParent());
try (OutputStream outputStream = Files.newOutputStream(savePath)) {
output.store(outputStream, null);
}
getLocker().doWrite(() -> {
MessageDigest hashCalculator = getNameHasher();
for (SettingsDef def: toSave) {
Path savePath = tryGetProjectSaveFile(def.projectDir, hashCalculator);
if (savePath == null) {
LOGGER.log(Level.WARNING, "Cannot save settings.gradle location for projects.");
break;
}

Path settingsGradle = def.settingsGradleDef.getSettingsGradle();

Properties output = new Properties();
output.put("projectDir", def.projectDir.toString());
output.put("rootProjectDir", def.rootProjectDir.toString());
output.put("maySearchUpwards", Boolean.toString(def.settingsGradleDef.isMaySearchUpwards()));
output.put("settingsGradle", settingsGradle != null ? settingsGradle.toString() : "");
output.put("stamp", def.stamp);

Files.createDirectories(savePath.getParent());
try (OutputStream outputStream = Files.newOutputStream(savePath)) {
output.store(outputStream, null);
}
return null;
}
return null;
});

outstandingDefsLock.lock();
Expand Down Expand Up @@ -222,25 +214,22 @@ private SettingsDef tryGetStoredSettingsDef(File projectDir) {
}

private SettingsDef tryGetStoredSettingsDef0(final File projectDir) throws IOException {
return getLocker().doRead(new IoTask<SettingsDef>() {
@Override
public SettingsDef run() throws IOException {
SettingsDef result = tryGetStoredSettingsDefUnsafe(projectDir);
if (result == null) {
return null;
}

if (Objects.equals(projectDir, result.projectDir)) {
return result;
}
return getLocker().doRead(() -> {
SettingsDef result = tryGetStoredSettingsDefUnsafe(projectDir);
if (result == null) {
return null;
}

SettingsDef rootDef = tryGetStoredSettingsDefUnsafe(projectDir);
if (rootDef == null) {
return null;
}
if (Objects.equals(projectDir, result.projectDir)) {
return result;
}

return Objects.equals(result.stamp, rootDef.stamp) ? result : null;
SettingsDef rootDef = tryGetStoredSettingsDefUnsafe(projectDir);
if (rootDef == null) {
return null;
}

return Objects.equals(result.stamp, rootDef.stamp) ? result : null;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ private static PropertySource<GradleLocationDef> gradleLocation() {

public static void requireGradlePaths() {
if (USING_GLOBAL_PATHS.compareAndSet(false, true)) {
gradleLocation().addChangeListener(new Runnable() {
@Override
public void run() {
updateGradleHome();
}
});

gradleLocation().addChangeListener(GradleHomeRegistry::updateGradleHome);
updateGradleHome();
}
}
Expand All @@ -91,31 +85,25 @@ private static void updateGradleHome() {
private static void setGradleHome(final FileObject gradleHome) {
ExceptionHelper.checkNotNullArgument(gradleHome, "gradleHome");

GRADLE_HOME_UPDATER.execute(new Runnable() {
@Override
public void run() {
URL[] urls = GradleHomeClassPathProvider.getAllGradleLibs(gradleHome);
if (urls.length == 0) {
// Keep the previous classpaths if there are non found.
return;
}

if (gradleHome.equals(GRADLE_HOME_BINARIES.get().getHomePath())) {
return;
}

GRADLE_HOME_BINARIES.set(new GradleHomePaths(gradleHome, urls));

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CHANGES.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null);
}
});

if (REGISTERED_GLOBAL_PATH.compareAndSet(false, true)) {
doRegisterGlobalClassPath();
}
GRADLE_HOME_UPDATER.execute(() -> {
URL[] urls = GradleHomeClassPathProvider.getAllGradleLibs(gradleHome);
if (urls.length == 0) {
// Keep the previous classpaths if there are non found.
return;
}

if (gradleHome.equals(GRADLE_HOME_BINARIES.get().getHomePath())) {
return;
}

GRADLE_HOME_BINARIES.set(new GradleHomePaths(gradleHome, urls));

SwingUtilities.invokeLater(() -> {
CHANGES.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null);
});

if (REGISTERED_GLOBAL_PATH.compareAndSet(false, true)) {
doRegisterGlobalClassPath();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
import org.netbeans.gradle.project.util.NbTaskExecutors;

public final class ModelCacheSizeAutoUpdater implements ProjectModelChangeListener {
private static final ModelCacheSizeAutoUpdater DEFAULT = new ModelCacheSizeAutoUpdater(NbTaskExecutors.newDefaultUpdateExecutor(), new NbConsumer<Integer>() {
@Override
public void accept(Integer requiredCacheSize) {
DefaultGradleModelLoader.ensureCacheSize(requiredCacheSize);
}
});
private static final ModelCacheSizeAutoUpdater DEFAULT = new ModelCacheSizeAutoUpdater(
NbTaskExecutors.newDefaultUpdateExecutor(),
DefaultGradleModelLoader::ensureCacheSize);

private final UpdateTaskExecutor cacheSizeCheckExecutor;
private final NbConsumer<? super Integer> cacheSizeUpdater;
Expand All @@ -47,12 +44,7 @@ public static ModelCacheSizeAutoUpdater getDefault() {

@Override
public void onModelChanged() {
cacheSizeCheckExecutor.execute(new Runnable() {
@Override
public void run() {
checkCacheSize();
}
});
cacheSizeCheckExecutor.execute(this::checkCacheSize);
}

private void checkCacheSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.netbeans.gradle.project.license.LicenseSource;
import org.netbeans.gradle.project.lookups.LookupsEx;
import org.netbeans.gradle.project.model.DefaultGradleModelLoader;
import org.netbeans.gradle.project.model.ModelRetrievedListener;
import org.netbeans.gradle.project.model.NbGradleModel;
import org.netbeans.gradle.project.model.SettingsGradleDef;
import org.netbeans.gradle.project.model.issue.ModelLoadIssue;
Expand Down Expand Up @@ -325,17 +324,12 @@ public ServiceObjects(NbGradleProject project, ProjectState state) {

this.modelManager = new ProjectModelManager(project,
DefaultGradleModelLoader.createEmptyModel(projectDir, scriptFileProvider));
ModelRetrievedListener<NbGradleModel> modelUpdateListener = new ModelRetrievedListener<NbGradleModel>() {
@Override
public void updateModel(NbGradleModel model, Throwable error) {
modelManager.updateModel(model, error);
if (model != null && error == null) {
SETTINGS_FILE_MANAGER.updateSettingsFile(model);
}
this.modelUpdater = new ProjectModelUpdater<>(createModelLoader(project), (model, error) -> {
modelManager.updateModel(model, error);
if (model != null && error == null) {
SETTINGS_FILE_MANAGER.updateSettingsFile(model);
}
};

this.modelUpdater = new ProjectModelUpdater<>(createModelLoader(project), modelUpdateListener);
});
this.settingsFileManager = new SettingsFileManager(projectDirAsFile, SETTINGS_FILE_MANAGER);
this.projectDisplayInfo = new ProjectDisplayInfo(
modelManager.currentModel(),
Expand Down Expand Up @@ -411,12 +405,7 @@ private static ProjectOpenedHook createOpenHook(
ROOT_PROJECT_REGISTRY.forProject(currentModel)
);

return GenericOpenHook.create(actionProperties, new Runnable() {
@Override
public void run() {
modelUpdater.reloadProjectMayUseCache();
}
});
return GenericOpenHook.create(actionProperties, modelUpdater::reloadProjectMayUseCache);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ private static List<LookupProvider> moveToLookupProvider(List<Lookup> lookups) {

private static LookupProvider moveToLookupProvider(final Lookup lookup) {
ExceptionHelper.checkNotNullArgument(lookup, "lookup");
return new LookupProvider() {
@Override
public Lookup createAdditionalLookup(Lookup baseContext) {
return lookup;
}
};
return (baseContext) -> lookup;
}

private static List<Lookup> extractLookupsFromProviders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import org.jtrim.property.PropertyFactory;
import org.jtrim.property.PropertySource;
import org.jtrim.property.ValueConverter;
import org.jtrim.utils.ExceptionHelper;
import org.netbeans.gradle.project.model.NbGradleModel;
import org.netbeans.gradle.project.properties.NbProperties;
import org.netbeans.gradle.project.util.NbBiFunction;

public final class ProjectDisplayInfo {
private final PropertySource<String> displayName;
Expand All @@ -18,18 +16,8 @@ public ProjectDisplayInfo(
ExceptionHelper.checkNotNullArgument(currentModel, "currentModel");
ExceptionHelper.checkNotNullArgument(displayNamePattern, "displayNamePattern");

this.displayName = NbProperties.combine(currentModel, displayNamePattern, new NbBiFunction<NbGradleModel, String, String>() {
@Override
public String apply(NbGradleModel model, String pattern) {
return model.getDisplayName(pattern);
}
});
this.description = PropertyFactory.convert(currentModel, new ValueConverter<NbGradleModel, String>() {
@Override
public String convert(NbGradleModel input) {
return input.getDescription();
}
});
this.displayName = NbProperties.combine(currentModel, displayNamePattern, NbGradleModel::getDisplayName);
this.description = PropertyFactory.convert(currentModel, NbGradleModel::getDescription);
}

public PropertySource<String> displayName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.netbeans.gradle.project.query.GradleCacheBinaryForSourceQuery;
import org.netbeans.gradle.project.query.GradleCacheByBinaryLookup;
import org.netbeans.gradle.project.util.LazyValue;
import org.netbeans.gradle.project.util.NbSupplier;

final class ProjectModelManager implements ModelRetrievedListener<NbGradleModel> {
private static final Logger LOGGER = Logger.getLogger(ProjectModelManager.class.getName());
Expand All @@ -45,18 +44,8 @@ public ProjectModelManager(final NbGradleProject project, final NbGradleModel in
this.currentModelRef = new AtomicReference<>(initialModel);
this.currentModel = NbProperties.atomicValueView(currentModelRef, modelChangeListeners);
this.modelUpdater = new SwingUpdateTaskExecutor(true);
this.modelUpdateDispatcher = new Runnable() {
@Override
public void run() {
onModelChange();
}
};
this.loadErrorRef = new LazyValue<>(new NbSupplier<ProjectIssueRef>() {
@Override
public ProjectIssueRef get() {
return project.getProjectIssueManager().createIssueRef();
}
});
this.modelUpdateDispatcher = this::onModelChange;
this.loadErrorRef = new LazyValue<>(() -> project.getProjectIssueManager().createIssueRef());
}

private void onModelChange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@ public ProjectModelUpdater(
ExceptionHelper.checkNotNullArgument(modelUpdater, "modelUpdater");

this.modelLoader = modelLoader;
this.modelUpdaterWrapper = new ModelRetrievedListener<M>() {
@Override
public void updateModel(M model, Throwable error) {
try {
modelUpdater.updateModel(model, error);
} finally {
loadedAtLeastOnceSignal.signal();
}
this.loadedAtLeastOnceSignal = new WaitableSignal();

this.modelUpdaterWrapper = (M model, Throwable error) -> {
try {
modelUpdater.updateModel(model, error);
} finally {
loadedAtLeastOnceSignal.signal();
}
};

this.hasModelBeenLoaded = new AtomicBoolean(false);
this.loadedAtLeastOnceSignal = new WaitableSignal();
this.lastInProgressRef = new AtomicReference<>(null);
}

Expand Down Expand Up @@ -82,11 +80,8 @@ private void loadProject(final boolean onlyIfNotLoaded, final boolean mayUseCach
lastInProgressRef.set(progressRef);
}

modelLoader.fetchModel(mayUseCache, modelUpdaterWrapper, new Runnable() {
@Override
public void run() {
lastInProgressRef.compareAndSet(progressRef, null);
}
modelLoader.fetchModel(mayUseCache, modelUpdaterWrapper, () -> {
lastInProgressRef.compareAndSet(progressRef, null);
});
}

Expand Down
Loading

0 comments on commit 6bb6338

Please sign in to comment.