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.
54 * @param aReport Report to use.
56 public ProgramActionExecutor(Report aReport) {
57 _showsToRecord = new TreeMap<Integer, Set<Program>>();
62 * Called by an action to indicate the desire to record a program.
65 * Priority of the program. Used to resolve conflicts.
69 public void recordProgram(int aPriority, Program aProgram) {
70 LOG.info("priority = " + aPriority + ", program: " + aProgram);
71 // Putting -priority into the set makes sure that iteration order
72 // over the priorities will go from higher priority to lower priority.
73 Set<Program> programs = _showsToRecord.get(-aPriority);
74 if (programs == null) {
75 programs = new TreeSet<Program>(new Program.TimeComparator());
76 _showsToRecord.put(-aPriority, programs);
78 programs.add(aProgram);
82 * Called by an action to indicate that a program is interesting.
85 * Category of the program.
89 public void interestingProgram(String aCategory, Program aProgram) {
90 LOG.info("category = '" + aCategory + "', program: " + aProgram);
91 _report.interestingProgram(aCategory, aProgram);
95 * Makes sure that the actions are performed.
97 public void commit() {
98 Set<TimeInterval> previouslyRecorded = new HashSet<TimeInterval>();
99 for (Integer priority : _showsToRecord.keySet()) {
100 for (Program program : _showsToRecord.get(priority)) {
101 TimeInterval interval = program.getInterval();
102 if (recordingConflictExists(previouslyRecorded, interval)) {
103 _report.setRecordingResult(RecordingResult.CONFLICT, program);
105 RecordingResult result = program.record();
106 _report.setRecordingResult(result, program);
107 previouslyRecorded.add(interval);
114 * Checks an interval for overlap with a previously recorded program.
116 * @param aPreviouslyRecorded
117 * Previously recorded programs.
120 * @return True iff there is a recording conflict.
122 private boolean recordingConflictExists(
123 Set<TimeInterval> aPreviouslyRecorded, TimeInterval aInterval) {
124 for (TimeInterval recordedInterval : aPreviouslyRecorded) {
125 if (aInterval.overlap(recordedInterval)) {