use of org.apache.commons.lang3.ObjectUtils.defaultIfNull in project plugin-prov by ligoj.
the class ProvQuoteInstanceResource method lookup.
private QuoteInstanceLookup lookup(final ProvQuote configuration, final double cpu, final int ram, final Boolean constant, final VmOs osName, final String type, final boolean ephemeral, final String location, final String usageName) {
final String node = configuration.getSubscription().getNode().getId();
final int subscription = configuration.getSubscription().getId();
// Resolve
final VmOs os = Optional.ofNullable(osName).map(VmOs::toPricingOs).orElse(null);
// Resolve the location to use
final String locationR = location == null ? configuration.getLocation().getName() : location;
// Compute the rate to use
final ProvUsage usage = usageName == null ? ObjectUtils.defaultIfNull(configuration.getUsage(), USAGE_DEFAULT) : resource.findConfiguredByName(usageRepository, usageName, subscription);
final double rate = usage.getRate() / 100d;
final int duration = usage.getDuration();
// Resolve the required instance type
final Integer typeId = type == null ? null : assertFound(itRepository.findByName(subscription, type), type).getId();
// Return only the first matching instance
// Template instance
final QuoteInstanceLookup template = ipRepository.findLowestPrice(node, cpu, ram, constant, os, typeId, ephemeral, locationR, rate, duration, PageRequest.of(0, 1)).stream().findFirst().map(ip -> newPrice((ProvInstancePrice) ip[0], (double) ip[2])).orElse(null);
// Custom instance
final QuoteInstanceLookup custom = ipRepository.findLowestCustomPrice(node, Math.ceil(cpu), Math.ceil(ram / 1024), constant, os, locationR, PageRequest.of(0, 1)).stream().findFirst().map(ip -> newPrice((ProvInstancePrice) ip[0], rate * (double) ip[1])).orElse(null);
// Select the best price term
if (template == null) {
return custom;
}
if (custom == null) {
return template;
}
return custom.getCost() < template.getCost() ? custom : template;
}
use of org.apache.commons.lang3.ObjectUtils.defaultIfNull in project plugin-prov by ligoj.
the class ProvQuoteStorageResource method lookup.
private List<QuoteStorageLoopup> lookup(final ProvQuote configuration, final int size, final Rate latency, final Integer instance, final ProvStorageOptimized optimized, final String location) {
// Get the attached node and check the security on this subscription
final String node = configuration.getSubscription().getNode().getRefined().getId();
final ProvQuoteInstance qi = checkInstance(node, instance);
// The the right location from instance first, then the request one
String iLocation = Optional.ofNullable(qi).map(qiResource::getLocation).map(ProvLocation::getName).orElse(location);
iLocation = ObjectUtils.defaultIfNull(iLocation, configuration.getLocation().getName());
if (location != null && !location.equals(iLocation)) {
// Not compatible locations
return Collections.emptyList();
}
return spRepository.findLowestPrice(node, size, latency, instance, optimized, iLocation, PageRequest.of(0, 10)).stream().map(spx -> (ProvStoragePrice) spx[0]).map(sp -> newPrice(sp, size, getCost(sp, size))).collect(Collectors.toList());
}
use of org.apache.commons.lang3.ObjectUtils.defaultIfNull in project plugin-prov by ligoj.
the class ProvQuoteInstanceResource method saveOrUpdate.
/**
* Save or update the given entity from the {@link QuoteInstanceEditionVo}. The computed cost are recursively
* updated from the instance to the quote total cost.
*/
private UpdatedCost saveOrUpdate(final ProvQuoteInstance entity, final QuoteInstanceEditionVo vo) {
// Compute the unbound cost delta
final int deltaUnbound = BooleanUtils.toInteger(vo.getMaxQuantity() == null) - BooleanUtils.toInteger(entity.isUnboundCost());
// Check the associations and copy attributes to the entity
final ProvQuote configuration = getQuoteFromSubscription(vo.getSubscription());
final Subscription subscription = configuration.getSubscription();
final String providerId = subscription.getNode().getRefined().getId();
DescribedBean.copy(vo, entity);
entity.setConfiguration(configuration);
final ProvLocation oldLocation = getLocation(entity);
entity.setPrice(ipRepository.findOneExpected(vo.getPrice()));
entity.setLocation(resource.findLocation(providerId, vo.getLocation()));
entity.setUsage(Optional.ofNullable(vo.getUsage()).map(u -> resource.findConfiguredByName(usageRepository, u, subscription.getId())).orElse(null));
entity.setOs(ObjectUtils.defaultIfNull(vo.getOs(), entity.getPrice().getOs()));
entity.setRam(vo.getRam());
entity.setCpu(vo.getCpu());
entity.setConstant(vo.getConstant());
entity.setEphemeral(vo.isEphemeral());
entity.setInternet(vo.getInternet());
entity.setMaxVariableCost(vo.getMaxVariableCost());
entity.setMinQuantity(vo.getMinQuantity());
entity.setMaxQuantity(vo.getMaxQuantity());
resource.checkVisibility(entity.getPrice().getType(), providerId);
checkConstraints(entity);
checkOs(entity);
// Update the unbound increment of the global quote
configuration.setUnboundCostCounter(configuration.getUnboundCostCounter() + deltaUnbound);
// Save and update the costs
final UpdatedCost cost = newUpdateCost(entity);
final Map<Integer, FloatingCost> storagesCosts = new HashMap<>();
final boolean dirtyPrice = !oldLocation.equals(getLocation(entity));
CollectionUtils.emptyIfNull(entity.getStorages()).stream().peek(s -> {
if (dirtyPrice) {
// Location has changed, the available storage price is invalidated
storageResource.refresh(s);
storageResource.refreshCost(s);
}
}).forEach(s -> storagesCosts.put(s.getId(), addCost(s, storageResource::updateCost)));
cost.setRelatedCosts(storagesCosts);
cost.setTotalCost(toFloatingCost(entity.getConfiguration()));
return cost;
}
use of org.apache.commons.lang3.ObjectUtils.defaultIfNull in project midpoint by Evolveum.
the class AbstractOrgTabPanel method loadOrgRoots.
private List<PrismObject<OrgType>> loadOrgRoots() {
Task task = getPageBase().createSimpleTask(OPERATION_LOAD_ORG_UNIT);
OperationResult result = new OperationResult(OPERATION_LOAD_ORG_UNIT);
List<PrismObject<OrgType>> list = new ArrayList<>();
try {
ObjectQuery query = getPageBase().getPrismContext().queryFor(OrgType.class).isRoot().asc(OrgType.F_NAME).build();
ObjectFilter assignableItemsFilter = getAssignableItemsFilter();
if (assignableItemsFilter != null) {
query.addFilter(assignableItemsFilter);
}
list = getPageBase().getModelService().searchObjects(OrgType.class, query, null, task, result);
// Sort org roots by displayOrder, if not set push the org to the end
list.sort(new Comparator<PrismObject<OrgType>>() {
@Override
public int compare(PrismObject<OrgType> o1, PrismObject<OrgType> o2) {
Comparator<PrismObject<OrgType>> intComparator = Comparator.comparingInt(o -> ((ObjectUtils.defaultIfNull(o.getRealValue().getDisplayOrder(), Integer.MAX_VALUE))));
int compare = intComparator.compare(o1, o2);
if (compare == 0) {
String display1 = WebComponentUtil.getDisplayName(o1);
if (StringUtils.isBlank(display1)) {
display1 = WebComponentUtil.getTranslatedPolyString(o1.getName());
}
String display2 = WebComponentUtil.getDisplayName(o2);
if (StringUtils.isBlank(display2)) {
display2 = WebComponentUtil.getTranslatedPolyString(o2.getName());
}
if (StringUtils.isEmpty(display1) && StringUtils.isEmpty(display2)) {
return compare;
}
if (StringUtils.isEmpty(display1)) {
return 1;
}
if (StringUtils.isEmpty(display2)) {
return -1;
}
return String.CASE_INSENSITIVE_ORDER.compare(display1, display2);
}
return compare;
}
});
if (list.isEmpty() && isWarnMessageVisible()) {
warn(getString("PageOrgTree.message.noOrgStructDefined"));
}
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Unable to load org. unit", ex);
result.recordFatalError(getString("AbstractOrgTabPanel.message.loadOrgRoots.fatalError"), ex);
} finally {
result.computeStatus();
}
if (WebComponentUtil.showResultInPage(result)) {
getPageBase().showResult(result);
}
return list;
}
use of org.apache.commons.lang3.ObjectUtils.defaultIfNull in project commons-lang by apache.
the class ObjectUtilsTest method testIsNull.
// -----------------------------------------------------------------------
@Test
public void testIsNull() {
final Object o = FOO;
final Object dflt = BAR;
assertSame("dflt was not returned when o was null", dflt, ObjectUtils.defaultIfNull(null, dflt));
assertSame("dflt was returned when o was not null", o, ObjectUtils.defaultIfNull(o, dflt));
}
Aggregations