1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.fill;
25
26 import java.io.Serializable;
27
28 import net.sf.jasperreports.engine.JRConstants;
29 import net.sf.jasperreports.engine.JRGroup;
30 import net.sf.jasperreports.engine.type.EvaluationTimeEnum;
31
32
33 /**
34  * An evaluation time during the report fill process.
35  * 
36  * @author Lucian Chirita (lucianc@users.sourceforge.net)
37  */

38 public final class JREvaluationTime implements Serializable
39 {
40     /**
41      *
42      */

43     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
44
45
46     /**
47      * Evaluation time corresponding to {@link EvaluationTimeEnum#REPORT EvaluationTimeEnum.REPORT}.
48      */

49     public static final JREvaluationTime EVALUATION_TIME_REPORT = new JREvaluationTime(EvaluationTimeEnum.REPORT, nullnull);
50     /**
51      * Evaluation time corresponding to {@link EvaluationTimeEnum#PAGE EvaluationTimeEnum.PAGE}.
52      */

53     public static final JREvaluationTime EVALUATION_TIME_PAGE = new JREvaluationTime(EvaluationTimeEnum.PAGE, nullnull);
54     /**
55      * Evaluation time corresponding to {@link EvaluationTimeEnum#COLUMN EvaluationTimeEnum.COLUMN}.
56      */

57     public static final JREvaluationTime EVALUATION_TIME_COLUMN = new JREvaluationTime(EvaluationTimeEnum.COLUMN, nullnull);
58     /**
59      * Evaluation time corresponding to {@link EvaluationTimeEnum#NOW EvaluationTimeEnum.NOW}.
60      */

61     public static final JREvaluationTime EVALUATION_TIME_NOW = new JREvaluationTime(EvaluationTimeEnum.NOW, nullnull);
62     
63
64     /**
65      * Evaluation time corresponding to {@link EvaluationTimeEnum#MASTER EvaluationTimeEnum.MASTER}.
66      */

67     public static final JREvaluationTime EVALUATION_TIME_MASTER = new JREvaluationTime(EvaluationTimeEnum.MASTER, nullnull);
68     
69     /**
70      * Returns the evaluation time corresponding to
71      * {@link EvaluationTimeEnum#GROUP EvaluationTimeEnum.GROUP} for a specific group.
72      * 
73      * @param groupName the group name
74      * @return corresponding group evaluation time
75      */

76     public static JREvaluationTime getGroupEvaluationTime(String groupName)
77     {
78         return new JREvaluationTime(EvaluationTimeEnum.GROUP, groupName, null);
79     }
80     
81     /**
82      * Returns the evaluation time corresponding to
83      * {@link EvaluationTimeEnum#BAND EvaluationTimeEnum.BAND} for a specific band.
84      * 
85      * @param band the band
86      * @return corresponding band evaluation time
87      */

88     public static JREvaluationTime getBandEvaluationTime(JRFillBand band)
89     {
90         return new JREvaluationTime(EvaluationTimeEnum.BAND, null, band);
91     }
92     
93     public static JREvaluationTime getBandEvaluationTime(int bandId)
94     {
95         return new JREvaluationTime(EvaluationTimeEnum.BAND, null, bandId);
96     }
97     
98     
99     /**
100      * Returns the evaluation time corresponding to an evaluation time type.
101      * 
102      * @param type the evaluation time type
103      * @param group the group used for {@link EvaluationTimeEnum#GROUP EvaluationTimeEnum.GROUP}
104      *     evaluation time type
105      * @param band the band used for {@link EvaluationTimeEnum#BAND EvaluationTimeEnum.BAND}
106      *     evaluation time type
107      * @return the evaluation time corresponding to an evaluation time type
108      */

109     public static JREvaluationTime getEvaluationTime(EvaluationTimeEnum type, JRGroup group, JRFillBand band)
110     {
111         JREvaluationTime evaluationTime;
112         
113         switch (type)
114         {
115             case REPORT:
116                 evaluationTime = EVALUATION_TIME_REPORT;
117                 break;
118             case MASTER:
119                 evaluationTime = EVALUATION_TIME_MASTER;
120                 break;
121             case PAGE:
122                 evaluationTime = EVALUATION_TIME_PAGE;
123                 break;
124             case COLUMN:
125                 evaluationTime = EVALUATION_TIME_COLUMN;
126                 break;
127             case GROUP:
128                 evaluationTime = getGroupEvaluationTime(group.getName());
129                 break;
130             case BAND:
131                 evaluationTime = getBandEvaluationTime(band);
132                 break;
133             default:
134                 evaluationTime = null;
135                 break;
136         }
137         
138         return evaluationTime;
139     }
140     
141     private final EvaluationTimeEnum type;
142     private final String groupName;
143     private final int bandId;
144     private final int hash;
145     
146     
147     private JREvaluationTime(EvaluationTimeEnum type, String groupName, JRFillBand band)
148     {
149         this(type, groupName, band == null ? 0 : band.getId());
150     }
151     
152     private JREvaluationTime(EvaluationTimeEnum type, String groupName, int bandId)
153     {
154         this.type = type;
155         this.groupName = groupName;
156         this.bandId = bandId;
157         
158         this.hash = computeHash();
159     }
160
161
162     private int computeHash()
163     {
164         int hashCode = type.ordinal();
165         hashCode = 31*hashCode + (groupName == null ? 0 : groupName.hashCode());
166         hashCode = 31*hashCode + bandId;
167         return hashCode;
168     }
169
170
171     @Override
172     public boolean equals(Object obj)
173     {
174         if (obj == this)
175         {
176             return true;
177         }
178         
179         JREvaluationTime e = (JREvaluationTime) obj;
180         
181         boolean eq = e.type == type;
182         
183         if (eq)
184         {
185             switch (type)
186             {
187                 case GROUP:
188                     eq = groupName.equals(e.groupName);
189                     break;
190                 case BAND:
191                     eq = bandId == e.bandId;
192                     break;
193             }
194         }
195         
196         return eq;
197     }
198
199
200     @Override
201     public int hashCode()
202     {
203         return hash;
204     }
205
206     @Override
207     public String toString()
208     {
209         return "{type: " + type
210                 + ", group: " + groupName
211                 + ", band: " + bandId
212                 + "}";
213     }
214
215     public EvaluationTimeEnum getType()
216     {
217         return type;
218     }
219
220     public String getGroupName()
221     {
222         return groupName;
223     }
224
225     public int getBandId()
226     {
227         return bandId;
228     }
229     
230 }
231