View Javadoc

1   /*
2    * $Id: IteratorStatus.java 768855 2009-04-27 02:09:35Z wesw $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   *   &lt;s:iterator status="status" value='%{0, 1}'&gt;
39   *      Index: &lt;s:property value="%{#status.index}" /&gt; &lt;br /&gt;
40   *      Count: &lt;s:property value="%{#status.count}" /&gt; &lt;br /&gt;  
41   *   &lt;/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  }