| 1 24 package org.archive.crawler.settings.refinements; 25 26 import java.text.DateFormat ; 27 import java.text.ParseException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.TimeZone ; 31 32 import org.archive.net.UURI; 33 34 46 public class TimespanCriteria implements Criteria { 47 48 private static DateFormat timeFormat; 49 static { 50 final TimeZone TZ = TimeZone.getTimeZone("GMT"); 51 timeFormat = new SimpleDateFormat ("HHmm"); 52 timeFormat.setTimeZone(TZ); 53 } 54 55 private Date from; 56 57 private Date to; 58 59 66 public TimespanCriteria(String from, String to) throws ParseException { 67 setFrom(from); 68 setTo(to); 69 } 70 71 public boolean isWithinRefinementBounds(UURI uri) { 72 try { 73 Date now = timeFormat.parse(timeFormat.format(new Date ())); 74 if (from.before(to)) { 75 if (now.getTime() >= from.getTime() 76 && now.getTime() <= to.getTime()) { 77 return true; 78 } 79 } else { 80 if (!(now.getTime() > to.getTime() && now.getTime() < from 81 .getTime())) { 82 return true; 83 } 84 } 85 } catch (ParseException e) { 86 e.printStackTrace(); 89 } 90 91 return false; 92 } 93 94 99 public String getFrom() { 100 return timeFormat.format(from); 101 } 102 103 109 public void setFrom(String from) throws ParseException { 110 this.from = timeFormat.parse(from); 111 } 112 113 118 public String getTo() { 119 return timeFormat.format(to); 120 } 121 122 128 public void setTo(String to) throws ParseException { 129 this.to = timeFormat.parse(to); 130 } 131 132 public boolean equals(Object o) { 133 if (o instanceof TimespanCriteria) { 134 TimespanCriteria other = (TimespanCriteria) o; 135 if (this.from.equals(other.from) && this.to.equals(other.to)) { 136 return true; 137 } 138 } 139 return false; 140 } 141 142 145 public String getName() { 146 return "Time of day criteria"; 147 } 148 149 152 public String getDescription() { 153 return "Accept any URIs between the hours of " + getFrom() + "(GMT) and " 154 + getTo() + "(GMT) each day."; 155 } 156 } | Popular Tags |