use of jetbrains.buildServer.util.filters.Filter in project teamcity-powershell by JetBrains.
the class PowerShellInfoProvider method selectTool.
@Nullable
public PowerShellInfo selectTool(@Nullable final PowerShellBitness bit, @Nullable final String version, @Nullable final PowerShellEdition edition) {
// filter by edition
List<PowerShellInfo> availableShells = myHolder.getShells();
if (edition != null) {
availableShells = CollectionsUtil.filterCollection(availableShells, new Filter<PowerShellInfo>() {
@Override
public boolean accept(@NotNull PowerShellInfo data) {
return edition.equals(data.getEdition());
}
});
}
// filter by version
if (version != null) {
availableShells = CollectionsUtil.filterCollection(availableShells, new Filter<PowerShellInfo>() {
@Override
public boolean accept(@NotNull PowerShellInfo data) {
return VersionComparatorUtil.compare(data.getVersion(), version) >= 0;
}
});
}
// filter by bitness
if (bit != null) {
availableShells = CollectionsUtil.filterCollection(availableShells, new Filter<PowerShellInfo>() {
@Override
public boolean accept(@NotNull PowerShellInfo data) {
return data.getBitness().equals(bit);
}
});
}
if (availableShells.isEmpty()) {
return null;
}
if (availableShells.size() == 1) {
return availableShells.get(0);
}
// prefer desktop over core
if (edition == null) {
Map<PowerShellEdition, List<PowerShellInfo>> byEdition = new HashMap<PowerShellEdition, List<PowerShellInfo>>();
for (PowerShellInfo info : availableShells) {
if (!byEdition.containsKey(info.getEdition())) {
byEdition.put(info.getEdition(), new ArrayList<PowerShellInfo>());
}
byEdition.get(info.getEdition()).add(info);
}
if (byEdition.get(PowerShellEdition.DESKTOP) != null) {
availableShells = byEdition.get(PowerShellEdition.DESKTOP);
} else {
availableShells = byEdition.get(PowerShellEdition.CORE);
}
}
if (availableShells.size() == 1) {
return availableShells.get(0);
}
// prefer 64bit over 32bit
if (bit == null) {
Map<PowerShellBitness, List<PowerShellInfo>> byBits = new HashMap<PowerShellBitness, List<PowerShellInfo>>();
for (PowerShellInfo info : availableShells) {
if (!byBits.containsKey(info.getBitness())) {
byBits.put(info.getBitness(), new ArrayList<PowerShellInfo>());
}
byBits.get(info.getBitness()).add(info);
}
if (byBits.containsKey(PowerShellBitness.x64)) {
availableShells = byBits.get(PowerShellBitness.x64);
} else {
availableShells = byBits.get(PowerShellBitness.x86);
}
}
if (availableShells.size() == 1) {
return availableShells.get(0);
}
// select max available version
Collections.sort(availableShells, new Comparator<PowerShellInfo>() {
@Override
public int compare(PowerShellInfo info1, PowerShellInfo info2) {
return VersionComparatorUtil.compare(info1.getVersion(), info2.getVersion());
}
});
return availableShells.get(0);
}
Aggregations