// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the ForecastService.
ForecastServiceInterface forecastService =
user.getService(DfpService.V201211.FORECAST_SERVICE);
// Set the placement that the prospective line item will target.
long[] targetedPlacementIds = new long[] {Long.parseLong("INSERT_PLACEMENT_ID_HERE")};
// Create inventory targeting.
InventoryTargeting inventoryTargeting = new InventoryTargeting();
inventoryTargeting.setTargetedPlacementIds(targetedPlacementIds);
// Create targeting.
Targeting targeting = new Targeting();
targeting.setInventoryTargeting(inventoryTargeting);
// Set the end date time to have the line line item run till.
String endDateTime = "INSERT_END_DATE_TIME_HERE";
// Create prospective line item.
LineItem lineItem = new LineItem();
lineItem.setLineItemType(LineItemType.SPONSORSHIP);
lineItem.setTargeting(targeting);
// Create the creative placeholder.
CreativePlaceholder creativePlaceholder = new CreativePlaceholder();
creativePlaceholder.setSize(new Size(300, 250, false));
// Set the size of creatives that can be associated with this line item.
lineItem.setCreativePlaceholders(new CreativePlaceholder[] {creativePlaceholder});
// Set the line item's time to be now until the projected end date time.
lineItem.setStartDateTimeType(StartDateTimeType.IMMEDIATELY);
lineItem.setEndDateTime(DateTimeUtils.fromString(endDateTime));
// Set the line item to use 50% of the impressions.
lineItem.setUnitType(UnitType.IMPRESSIONS);
lineItem.setUnitsBought(50L);
// Set the cost type to match the unit type.
lineItem.setCostType(CostType.CPM);
// Get forecast for line item.
Forecast forecast = forecastService.getForecast(lineItem);
// Display results.
long matched = forecast.getMatchedUnits();
double availablePercent = (forecast.getAvailableUnits() / (matched * 1.0)) * 100;
String unitType = forecast.getUnitType().toString().toLowerCase();