1 | /*
|
---|
2 | * The Apache Software License, Version 1.1
|
---|
3 | *
|
---|
4 | * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
|
---|
5 | * reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * 1. Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | *
|
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
15 | * notice, this list of conditions and the following disclaimer in
|
---|
16 | * the documentation and/or other materials provided with the
|
---|
17 | * distribution.
|
---|
18 | *
|
---|
19 | * 3. The end-user documentation included with the redistribution, if
|
---|
20 | * any, must include the following acknowlegement:
|
---|
21 | * "This product includes software developed by the
|
---|
22 | * Apache Software Foundation (http://www.apache.org/)."
|
---|
23 | * Alternately, this acknowlegement may appear in the software itself,
|
---|
24 | * if and wherever such third-party acknowlegements normally appear.
|
---|
25 | *
|
---|
26 | * 4. The names "The Jakarta Project", "Ant", and "Apache Software
|
---|
27 | * Foundation" must not be used to endorse or promote products derived
|
---|
28 | * from this software without prior written permission. For written
|
---|
29 | * permission, please contact [email protected].
|
---|
30 | *
|
---|
31 | * 5. Products derived from this software may not be called "Apache"
|
---|
32 | * nor may "Apache" appear in their names without prior written
|
---|
33 | * permission of the Apache Group.
|
---|
34 | *
|
---|
35 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
---|
36 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
37 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
38 | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
---|
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
41 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
---|
42 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
43 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
44 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
---|
45 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
46 | * SUCH DAMAGE.
|
---|
47 | * ====================================================================
|
---|
48 | *
|
---|
49 | * This software consists of voluntary contributions made by many
|
---|
50 | * individuals on behalf of the Apache Software Foundation. For more
|
---|
51 | * information on the Apache Software Foundation, please see
|
---|
52 | * <http://www.apache.org/>.
|
---|
53 | */
|
---|
54 | package ise.antelope.tasks.condition;
|
---|
55 |
|
---|
56 | import java.text.*;
|
---|
57 | import java.util.*;
|
---|
58 | import org.apache.tools.ant.BuildException;
|
---|
59 | import org.apache.tools.ant.PropertyHelper;
|
---|
60 |
|
---|
61 | import org.apache.tools.ant.taskdefs.condition.*;
|
---|
62 | import ise.antelope.tasks.typedefs.TimeUnit;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Condition that validates the difference between two date/time stamps.
|
---|
66 | *
|
---|
67 | * @author Dale Anson
|
---|
68 | * @version $Revision: 1.1 $
|
---|
69 | */
|
---|
70 | public class DateTimeDifference implements Condition {
|
---|
71 |
|
---|
72 | private String date1, date2;
|
---|
73 |
|
---|
74 | private String format = null;
|
---|
75 | private boolean lenient = true;
|
---|
76 |
|
---|
77 | private int value = 0;
|
---|
78 | private String unit = null;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Sets a date/timestamp, required.
|
---|
82 | *
|
---|
83 | * @param date a string representing a date or time.
|
---|
84 | */
|
---|
85 | public void setDatetime1(String date) {
|
---|
86 | if (date == null)
|
---|
87 | return;
|
---|
88 | if (date1 == null)
|
---|
89 | date1 = date;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Sets the other date/time stamp, required.
|
---|
94 | *
|
---|
95 | * @param date a string representing a date or time.
|
---|
96 | */
|
---|
97 | public void setDatetime2(String date) {
|
---|
98 | if (date == null)
|
---|
99 | return;
|
---|
100 | if (date2 == null)
|
---|
101 | date2 = date;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Sets the format of the datetimes, this is a required attribute. See
|
---|
107 | * java.text.SimpleDateFormat for the format.
|
---|
108 | *
|
---|
109 | * @param format The new format value
|
---|
110 | */
|
---|
111 | public void setFormat(String format) {
|
---|
112 | this.format = format;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Sets whether the datetime parser should use lenient parsing. This is an
|
---|
118 | * optional setting, default is true, use lenient parsing.
|
---|
119 | *
|
---|
120 | * @param b The new lenient value
|
---|
121 | */
|
---|
122 | public void setLenient(boolean b) {
|
---|
123 | lenient = b;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Sets the expected difference between the 2 datetimes.
|
---|
128 | *
|
---|
129 | * @param value the expected difference.
|
---|
130 | */
|
---|
131 | public void setValue(int value) {
|
---|
132 | this.value = value;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Sets the unit for the difference between the 2 datetimes. For example,
|
---|
137 | * if <code>value</code> is 12 and <code>unit</code> is "hours", then this
|
---|
138 | * condition checks that the difference between the 2 datetimes is 12 hours.
|
---|
139 | *
|
---|
140 | * @param unit valid values are "millisecond", "second", "minute", "hour",
|
---|
141 | * "day", "week", "month", "year".
|
---|
142 | */
|
---|
143 | public void setUnit(String unit) {
|
---|
144 | if (unit == null)
|
---|
145 | return;
|
---|
146 | this.unit = unit;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @return true if the difference between the two dates
|
---|
151 | * or times is the same as the expected value.
|
---|
152 | * @exception BuildException if the attributes are not set correctly
|
---|
153 | */
|
---|
154 | public boolean eval() throws BuildException {
|
---|
155 | try {
|
---|
156 | Date d1;
|
---|
157 | Date d2;
|
---|
158 | if (format == null)
|
---|
159 | throw new BuildException("format is required");
|
---|
160 |
|
---|
161 | SimpleDateFormat df = new SimpleDateFormat(format);
|
---|
162 | df.setLenient(lenient);
|
---|
163 |
|
---|
164 | if (date1 != null && date2 != null) {
|
---|
165 | // handle date difference
|
---|
166 | d1 = df.parse(date1);
|
---|
167 | d2 = df.parse(date2);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | throw new BuildException("Both datetime1 and datetime2 must be set.");
|
---|
171 |
|
---|
172 | Calendar cal1 = Calendar.getInstance();
|
---|
173 | Calendar cal2 = Calendar.getInstance();
|
---|
174 | cal1.setTime(d1);
|
---|
175 | cal2.setTime(d2);
|
---|
176 |
|
---|
177 | Calendar before = cal1.before(cal2) ? cal1 : cal2;
|
---|
178 | Calendar after = cal1.before(cal2) ? cal2 : cal1;
|
---|
179 |
|
---|
180 | int cal_unit = Calendar.DATE;
|
---|
181 |
|
---|
182 | if (unit.equals(TimeUnit.SECOND)) {
|
---|
183 | cal_unit = Calendar.SECOND;
|
---|
184 | }
|
---|
185 | else if (unit.equals(TimeUnit.MILLISECOND)) {
|
---|
186 | cal_unit = Calendar.MILLISECOND;
|
---|
187 | }
|
---|
188 | else if (unit.equals(TimeUnit.MINUTE)) {
|
---|
189 | cal_unit = Calendar.MINUTE;
|
---|
190 | }
|
---|
191 | else if (unit.equals(TimeUnit.HOUR)) {
|
---|
192 | cal_unit = Calendar.HOUR;
|
---|
193 | }
|
---|
194 | else if (unit.equals(TimeUnit.DAY)) {
|
---|
195 | cal_unit = Calendar.DATE;
|
---|
196 | }
|
---|
197 | else if (unit.equals(TimeUnit.WEEK)) {
|
---|
198 | cal_unit = Calendar.WEEK_OF_YEAR;
|
---|
199 | }
|
---|
200 | else if (unit.equals(TimeUnit.MONTH)) {
|
---|
201 | cal_unit = Calendar.MONTH;
|
---|
202 | }
|
---|
203 | else if (unit.equals(TimeUnit.YEAR)) {
|
---|
204 | cal_unit = Calendar.YEAR;
|
---|
205 | }
|
---|
206 | else
|
---|
207 | throw new BuildException("Unknown unit: " + unit);
|
---|
208 |
|
---|
209 | int count = 0;
|
---|
210 | while (before.before(after)) {
|
---|
211 | before.add(cal_unit, 1);
|
---|
212 | ++count;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return count == value;
|
---|
216 | }
|
---|
217 | catch (Exception e) {
|
---|
218 | e.printStackTrace();
|
---|
219 | throw new BuildException(e.getMessage());
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|