View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.filter;
22  
23  import net.sf.jour.util.TimeUtil;
24  
25  import org.apache.log4j.Logger;
26  
27  
28  public class TimeMatchFilter extends MatchFilter {
29  	
30      protected static final Logger log = Logger.getLogger(TimeMatchFilter.class);
31      
32  	double timeFrom;
33  	
34  	double timeTo;
35  	
36  	TimeMatchFilter(String comp) {
37          double time[] = TimeUtil.string2TimeInterval(comp);
38          if (time != null) {
39              this.timeFrom = time[0];
40              this.timeTo = time[1];
41          }
42      }
43  	
44  	TimeMatchFilter(String sFrom, String sTo, String comp) {
45  		this.timeFrom = TimeUtil.string2TimeStamp(sFrom);
46  		this.timeTo = TimeUtil.string2TimeStamp(sTo);
47  		if (!isValid()) {
48  			double time[] = TimeUtil.string2TimeInterval(comp);
49  			if (time != null) {
50  				this.timeFrom = time[0];
51  				this.timeTo = time[1];
52  			}
53  		}
54  	}
55  	
56      public boolean isValid() {
57          return (this.timeFrom > 0) && (this.timeTo > 0) && (this.timeFrom <= this.timeTo);
58      }
59  	
60  	public void debug() {
61  		log.debug("time:" + TimeUtil.timeStamp2string(timeFrom) + " - " + TimeUtil.timeStamp2string(timeTo));
62  		log.debug("time:" + TimeUtil.timeStamp2dateString(timeFrom) + " - " + TimeUtil.timeStamp2dateString(timeTo));
63  	}
64  	
65  	public int matchState(Object obj) {
66  		double eventTime = ((Double)obj).doubleValue();
67  		if ((eventTime >= timeFrom) 
68  			&& (eventTime < timeTo)) {
69  			return MATCH_YES;
70  		} else {
71  			return MATCH_NO;
72  		}
73  	}
74  }