Subversion Repositories SmartDukaan

Rev

Rev 5827 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5827 amar.kumar 1
/*
2
 *	Copyright 2005 stat4j.org
3
 *
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
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
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.
15
 */
16
 
17
package net.sourceforge.stat4j;
18
 
19
import java.util.Date;
20
 
21
 
22
 
23
/**
24
 * Name:		Threshold.java
25
 * Date:		Sep 6, 2004
26
 * Description:
27
 * 
28
 * A preset limit that may be applied to a statstic so
29
 * as to trigger an alert.
30
 * 
31
 * 
32
 * @see Alert
33
 * 
34
 * @author Lara D'Abreo
35
 */
36
public final class Threshold {
37
 
38
	//Threshold operators
39
	public interface Operators {
40
 
41
		public final static String LESS_THAN_OR_EQUAL = "<=";
42
		public final static String GREATER_THAN_OR_EQUAL = ">=";
43
		public final static String EQUAL_TO = "==";
44
		public final static String NOT_EQUAL_TO = "!=";
45
		public final static String GREATER_THAN = ">";
46
		public final static String LESS_THAN = "<";
47
 
48
 
49
		public final static String[] ALL_OPS =new String[] 
50
			{
51
				LESS_THAN_OR_EQUAL,
52
				GREATER_THAN_OR_EQUAL,
53
				EQUAL_TO,
54
				NOT_EQUAL_TO,
55
				GREATER_THAN,
56
				LESS_THAN,
57
				};
58
 
59
	}
60
 
61
 
62
	protected String operator;
63
	protected double limit;
64
 
65
 
66
	public Threshold() {
67
	}
68
 
69
	/**
70
	 * Evaluate if rule is triggered
71
	 * @param reading
72
	 * @return
73
	 */
74
	public boolean isTriggered(double value) {
75
 
76
		if (operator == null)
77
			return false;
78
 
79
		if (operator.equals(Operators.EQUAL_TO)) {
80
			return value == limit;
81
		} else if (operator.equals(Operators.NOT_EQUAL_TO)) {
82
			return value != limit;
83
		} else if (operator.equals(Operators.GREATER_THAN)) {
84
			return value > limit;
85
		} else if (operator.equals(Operators.GREATER_THAN_OR_EQUAL)) {
86
			Date currentDate = new Date();
87
			if(currentDate.getHours()>1 && currentDate.getHours()<5) {
88
				return value >= limit/5;
5848 amar.kumar 89
			} else if(currentDate.getHours()<=23 && currentDate.getHours()>6) {
5827 amar.kumar 90
				return value >= limit;
91
			} else {
92
				return value >= limit/2;
93
			}
94
		} else if (operator.equals(Operators.LESS_THAN)) {
95
			return value < limit;
96
		} else if (operator.equals(Operators.LESS_THAN_OR_EQUAL)) {
97
			return value <= limit;
98
		}
99
		return false;
100
	}
101
 
102
 
103
 
104
 
105
	public static Threshold toThreshold(String str) {
106
		if (str == null)
107
			return null;
108
		if (str.length() == 0)
109
			return null;
110
 
111
		 try {
112
		 		int idx = -1;String op= null;
113
		 		for (int i = 0; i < Operators.ALL_OPS.length; ++i) {
114
		 			String opstr = Operators.ALL_OPS[i];
115
		 			idx= str.indexOf(opstr);
116
					if (idx >= 0) {
117
						op = opstr;
118
						break;			 			
119
					} //fi
120
		 		}//rof
121
 
122
		 		// mo operator found
123
		 		if (idx == -1) return null;
124
 
125
				// value
126
				String valueStr = str.substring(idx+op.length());
127
				double value = Double.parseDouble(valueStr);
128
 
129
				Threshold rule = new Threshold();
130
				rule.setOperator(op);
131
				rule.setValue(value);
132
 
133
				return rule;
134
 
135
			} catch (Exception e) {
136
				return null;
137
			}
138
 
139
	}
140
 
141
 
142
	/**
143
	 * @param string
144
	 */
145
	public void setOperator(String string) {
146
		operator = string;
147
	}
148
 
149
	/**
150
	 * @param d
151
	 */
152
	public void setValue(double d) {
153
		limit = d;
154
	}
155
 
156
	public String toString() {
157
		return operator + limit;
158
	}
159
 
160
}