1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.jsp;
23
24
25 /***
26 * The iterator tag can export an IteratorStatus object so that
27 * one can get information about the status of the iteration, such as:
28 * <ul>
29 * <li>index: current iteration index, starts on 0 and increments in one on every iteration</li>
30 * <li>count: iterations so far, starts on 1. count is always index + 1</li>
31 * <li>first: true if index == 0</li>
32 * <li>even: true if (index + 1) % 2 == 0</li>
33 * <li>last: true if current iteration is the last iteration</li>
34 * <li>odd: true if (index + 1) % 2 == 1</li>
35 * </ul>
36 * <p>Example</p>
37 * <pre>
38 * <s:iterator status="status" value='%{0, 1}'>
39 * Index: <s:property value="%{#status.index}" /> <br />
40 * Count: <s:property value="%{#status.count}" /> <br />
41 * </s:iterator>
42 * </pre>
43 *
44 * <p>will print</p>
45 * <pre>
46 * Index: 0
47 * Count: 1
48 * Index: 1
49 * Count: 2
50 * </pre>
51 */
52 public class IteratorStatus {
53 protected StatusState state;
54
55 public IteratorStatus(StatusState aState) {
56 state = aState;
57 }
58
59 public int getCount() {
60 return state.index + 1;
61 }
62
63 public boolean isEven() {
64 return ((state.index + 1) % 2) == 0;
65 }
66
67 public boolean isFirst() {
68 return state.index == 0;
69 }
70
71 public int getIndex() {
72 return state.index;
73 }
74
75 public boolean isLast() {
76 return state.last;
77 }
78
79 public boolean isOdd() {
80 return ((state.index + 1) % 2) != 0;
81 }
82
83 public int modulus(int operand) {
84 return (state.index + 1) % operand;
85 }
86
87 public static class StatusState {
88 boolean last = false;
89 int index = 0;
90
91 public void setLast(boolean isLast) {
92 last = isLast;
93 }
94
95 public void next() {
96 index++;
97 }
98 }
99 }