|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
BaseDurationFactory.java | 0% | 0% | 0% | 0% |
|
1 | package org.drools.semantics.base; | |
2 | ||
3 | /* | |
4 | * $Id: BaseDurationFactory.java,v 1.11 2005/02/04 02:13:36 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2003-2004 (C) The Werken Company. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use of this software and associated documentation | |
9 | * ("Software"), with or without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import org.drools.rule.Rule; | |
45 | import org.drools.smf.Configuration; | |
46 | import org.drools.smf.DurationFactory; | |
47 | import org.drools.spi.Duration; | |
48 | import org.drools.spi.RuleBaseContext; | |
49 | ||
50 | /** | |
51 | * An implementation of the <code>DurationFactory</code> interface. | |
52 | */ | |
53 | public class BaseDurationFactory implements DurationFactory | |
54 | { | |
55 | /** The number of seconds in one minute (60). */ | |
56 | private static final int SECONDS_IN_A_MINUTE = 60; | |
57 | ||
58 | /** The number of seconds in one hour (60 * 60). */ | |
59 | private static final int SECONDS_IN_AN_HOUR = 60 * SECONDS_IN_A_MINUTE; | |
60 | ||
61 | /** The number of seconds in one day (60 * 60 * 24). */ | |
62 | private static final int SECONDS_IN_A_DAY = 24 * SECONDS_IN_AN_HOUR; | |
63 | ||
64 | /** | |
65 | * Returns a new <code>Duration</code> object configured | |
66 | * using the given <code>Configuration</code>. | |
67 | * | |
68 | * @param config a <code>Configuration</code> object containing "days", | |
69 | * "hours", "minutes", and/or "seconds" attributes. All attributes | |
70 | * are optional and the given <code>Configuration</code> object | |
71 | * may be <code>null</code>. | |
72 | * | |
73 | * @return a new <code>Duration</code> object. | |
74 | */ | |
75 | 0 | public Duration newDuration( Rule rule, |
76 | RuleBaseContext context, | |
77 | Configuration config ) | |
78 | { | |
79 | 0 | long seconds = 0; |
80 | ||
81 | 0 | if ( null != config ) |
82 | { | |
83 | 0 | String daysStr = config.getAttribute( "days" ); |
84 | 0 | String hoursStr = config.getAttribute( "hours" ); |
85 | 0 | String minutesStr = config.getAttribute( "minutes" ); |
86 | 0 | String secondsStr = config.getAttribute( "seconds" ); |
87 | ||
88 | 0 | if ( daysStr != null && !daysStr.trim( ).equals( "" ) ) |
89 | { | |
90 | 0 | seconds += Integer.parseInt( daysStr.trim( ) ) * SECONDS_IN_A_DAY; |
91 | } | |
92 | ||
93 | 0 | if ( hoursStr != null && !hoursStr.trim( ).equals( "" ) ) |
94 | { | |
95 | 0 | seconds += Integer.parseInt( hoursStr.trim( ) ) * SECONDS_IN_AN_HOUR; |
96 | } | |
97 | ||
98 | 0 | if ( minutesStr != null && !minutesStr.trim( ).equals( "" ) ) |
99 | { | |
100 | 0 | seconds += Integer.parseInt( minutesStr.trim( ) ) * SECONDS_IN_A_MINUTE; |
101 | } | |
102 | ||
103 | 0 | if ( secondsStr != null && !secondsStr.trim( ).equals( "" ) ) |
104 | { | |
105 | 0 | seconds += Integer.parseInt( secondsStr.trim( ) ); |
106 | } | |
107 | } | |
108 | ||
109 | 0 | return new BaseDuration( rule, seconds ); |
110 | } | |
111 | } |
|