Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package au.id.hazelwood.xmltvguidebuilder.postprocessor; import au.id.hazelwood.xmltvguidebuilder.model.ChannelDetail; import au.id.hazelwood.xmltvguidebuilder.model.ChannelListings; import au.id.hazelwood.xmltvguidebuilder.model.ProgrammeDetail; import org.joda.time.DateTime; import org.joda.time.Duration; import org.joda.time.Interval; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import static au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtils.formatDurationDHM; import static au.id.hazelwood.xmltvguidebuilder.utils.DateTimeUtils.toISODateTime; import static java.lang.Math.round; import static java.text.MessageFormat.format; import static org.apache.commons.lang.StringUtils.leftPad; import static org.apache.commons.lang.StringUtils.repeat; import static org.apache.commons.lang.StringUtils.rightPad; /** * @author Ricky Hazelwood * @version 1.0 */ public class ListingVerifier { private static final Logger LOGGER = LoggerFactory.getLogger(ListingVerifier.class); public void verifyListing(ChannelListings listings, DateTime from, DateTime to, DateTime subsetTo) { Duration listingDurationTotal = new Interval(from, to).toDuration(); Duration listingDurationSubset = new Interval(from, subsetTo).toDuration(); LOGGER.info(repeat("-", 100)); for (ChannelDetail channelDetail : listings.getChannels()) { Duration missingDurationTotal = Duration.ZERO; Duration missingDurationSubset = Duration.ZERO; StringBuilder allMissingIntervalDetails = new StringBuilder(); for (Interval missing : findMissingIntervals(listings, from, to, channelDetail.getId())) { missingDurationTotal = missingDurationTotal.plus(missing.toDuration()); if (missing.getStart().isBefore(subsetTo)) { if (missing.getEnd().isBefore(subsetTo)) { missingDurationSubset = missingDurationSubset.plus(missing.toDuration()); } else { missingDurationSubset = missingDurationSubset .plus(new Duration(missing.getStart(), subsetTo)); } } allMissingIntervalDetails.append(allMissingIntervalDetails.length() == 0 ? "missing " : ", "); allMissingIntervalDetails.append( format("{0}-{1}", toISODateTime(missing.getStart()), toISODateTime(missing.getEnd()))); } Duration availableDurationTotal = listingDurationTotal.minus(missingDurationTotal); Duration availableDurationSubset = listingDurationSubset.minus(missingDurationSubset); Integer availablePercentageTotal = getPercentage(availableDurationTotal, listingDurationTotal); Integer availablePercentageSubset = getPercentage(availableDurationSubset, listingDurationSubset); LOGGER.info("{} {} [{}|{}] {}", rightPad(channelDetail.getId() + " - " + channelDetail.getName(), 42), formatDurationDHM(availableDurationTotal.getMillis()), leftPad(availablePercentageSubset + "%", 4), leftPad(availablePercentageTotal + "%", 4), allMissingIntervalDetails.toString()); } LOGGER.info(repeat("-", 100)); } private List<Interval> findMissingIntervals(ChannelListings channelListings, DateTime from, DateTime to, Integer channelId) { List<Interval> missingIntervals = new ArrayList<>(); DateTime lastEndDate = from; for (ProgrammeDetail program : channelListings.getPrograms(channelId)) { DateTime startDate = program.getStartDate(); if (startDate.isAfter(lastEndDate)) { missingIntervals.add(new Interval(lastEndDate, startDate)); } lastEndDate = program.getEndDate(); } if (lastEndDate.isBefore(to)) { missingIntervals.add(new Interval(lastEndDate, to)); } return missingIntervals; } private int getPercentage(Duration available, Duration total) { return round(available.getMillis() * 100F / total.getMillis()); } }