blob: e0ae874b0783a5c9c23f93b9f892a7c653b0f6b0 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 2018 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.r.core.pkgmanager;
import java.util.List;
import org.eclipse.statet.r.core.pkgmanager.IRLibPaths.Entry;
import org.eclipse.statet.r.core.renv.IRLibraryLocation;
public class RPkgActionHelper {
private final boolean sameLocation;
private final IRLibraryLocation defaultLocation;
private final IRLibPaths rLibPaths;
public RPkgActionHelper(final boolean sameLocation, final IRLibraryLocation defaultLocation,
final IRLibPaths rLibPaths) {
this.sameLocation= sameLocation;
this.defaultLocation= defaultLocation;
this.rLibPaths= rLibPaths;
}
public void updateLocation(final RPkgAction.Install action) {
if (this.sameLocation) {
final IRPkgInfoAndData referencePkg= action.getReferencePkg();
if (referencePkg != null) {
final IRLibraryLocation location= referencePkg.getLibraryLocation();
final Entry entry= this.rLibPaths.getEntryByLocation(location);
if (entry != null && (entry.getAccess() & IRLibPaths.WRITABLE) == IRLibPaths.WRITABLE) {
action.setLibraryLocation(location);
}
return;
}
}
if (this.defaultLocation == null) {
throw new UnsupportedOperationException("missing default location");
}
action.setLibraryLocation(this.defaultLocation);
}
public void update(final List<? extends RPkgAction.Install> actions) {
for (final RPkgAction.Install action : actions) {
updateLocation(action);
}
}
@Override
public int hashCode() {
int h= (this.defaultLocation != null) ? this.defaultLocation.hashCode() : 0;
if (this.sameLocation) {
h++;
}
return h;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof RPkgActionHelper)) {
return false;
}
final RPkgActionHelper other= (RPkgActionHelper) obj;
return (((this.defaultLocation != null) ? this.defaultLocation.equals(other.defaultLocation) : null == other.defaultLocation)
&& (this.sameLocation == other.sameLocation) );
}
}