2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.crawler.kiss.main;
19 import java.util.HashSet;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.wamblee.crawler.kiss.guide.Program;
28 import org.wamblee.crawler.kiss.guide.TimeInterval;
29 import org.wamblee.crawler.kiss.guide.Program.RecordingResult;
32 * Provides execution of actions for programs. Actions use this class to tell
33 * the executor what to do. The executor then decides on exactly what to do and
34 * in what order and makes decisions in case of conflicts.
36 public class ProgramActionExecutor {
38 private static final Log LOG = LogFactory
39 .getLog(ProgramActionExecutor.class);
42 * Map of priority to set of programs.
44 private Map<Integer, Set<Program>> _showsToRecord;
49 private Report _report;
52 * Constructs the program action executor.
55 public ProgramActionExecutor(Report aReport) {
56 _showsToRecord = new TreeMap<Integer, Set<Program>>();
61 * Called by an action to indicate the desire to record a program.
64 * Priority of the program. Used to resolve conflicts.
68 public void recordProgram(int aPriority, Program aProgram) {
69 LOG.info("priority = " + aPriority + ", program: " + aProgram);
70 // Putting -priority into the set makes sure that iteration order
71 // over the priorities will go from higher priority to lower priority.
72 Set<Program> programs = _showsToRecord.get(-aPriority);
73 if (programs == null) {
74 programs = new TreeSet<Program>(new Program.TimeComparator());
75 _showsToRecord.put(-aPriority, programs);
77 programs.add(aProgram);
81 * Called by an action to indicate that a program is interesting.
84 * Category of the program.
88 public void interestingProgram(String aCategory, Program aProgram) {
89 LOG.info("category = '" + aCategory + "', program: " + aProgram);
90 _report.interestingProgram(aCategory, aProgram);
94 * Makes sure that the actions are performed.
96 public void commit() {
97 Set<TimeInterval> previouslyRecorded = new HashSet<TimeInterval>();
98 for (Integer priority : _showsToRecord.keySet()) {
99 for (Program program : _showsToRecord.get(priority)) {
100 TimeInterval interval = program.getInterval();
101 if (recordingConflictExists(previouslyRecorded, interval)) {
102 _report.setRecordingResult(RecordingResult.CONFLICT, program);
104 RecordingResult result = program.record();
105 _report.setRecordingResult(result, program);
106 previouslyRecorded.add(interval);
113 * Checks an interval for overlap with a previously recorded program.
115 * @param aPreviouslyRecorded
116 * Previously recorded programs.
119 * @return True iff there is a recording conflict.
121 private boolean recordingConflictExists(
122 Set<TimeInterval> aPreviouslyRecorded, TimeInterval aInterval) {
123 for (TimeInterval recordedInterval : aPreviouslyRecorded) {
124 if (aInterval.overlap(recordedInterval)) {