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.util;
25
26 import java.util.Collection;
27 import java.util.concurrent.atomic.AtomicInteger;
28
29 import net.sf.jasperreports.engine.JRPrintElement;
30 import net.sf.jasperreports.engine.JRPrintFrame;
31
32 /**
33  * Print element visitor that counts deep elements by recursively visiting
34  * {@link JRPrintFrame} containers.
35  * 
36  * @author Lucian Chirita (lucianc@users.sourceforge.net)
37  */

38 //we need a mutable integer argument, using AtomicInteger
39 public class DeepPrintElementCounter extends UniformPrintElementVisitor<AtomicInteger>
40 {
41
42     private static final DeepPrintElementCounter INSTANCE = new DeepPrintElementCounter();
43     
44     /**
45      * Calculates the deep element count of an element.
46      * 
47      * @param element
48      * @return the deep element count of the element
49      */

50     public static int count(JRPrintElement element)
51     {
52         return count(element, INSTANCE);
53     }
54     
55     protected static int count(JRPrintElement element, UniformPrintElementVisitor<AtomicInteger> counter)
56     {
57         if (element == null)
58         {
59             return 0;
60         }
61         
62         AtomicInteger count = new AtomicInteger(0);
63         element.accept(counter, count);
64         return count.get();
65     }
66     
67     public static int count(Collection<? extends JRPrintElement> elements)
68     {
69         return count(elements, INSTANCE);
70     }
71     
72     protected static int count(Collection<? extends JRPrintElement> elements, 
73             UniformPrintElementVisitor<AtomicInteger> counter)
74     {
75         if (elements == null || elements.isEmpty())
76         {
77             return 0;
78         }
79         
80         AtomicInteger count = new AtomicInteger(0);
81         for (JRPrintElement element : elements)
82         {
83             element.accept(counter, count);
84         }
85         return count.get();
86     }
87     
88     protected DeepPrintElementCounter()
89     {
90         super(true);
91     }
92
93     @Override
94     protected void visitElement(JRPrintElement element, AtomicInteger count)
95     {
96         count.incrementAndGet();        
97     }
98     
99 }
100