View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.facade.sql;
18  
19  import java.util.Iterator;
20  
21  import COM.FutureTense.Interfaces.IList;
22  
23  /**
24   * Wrapper for an IList that turns an <tt>IList</tt> into a <tt>Iterable</tt>.
25   * <p/>
26   * Sample usage:
27   * 
28   * <pre>
29   * SortedSet&lt;VirtualWebroot&gt; result = new TreeSet&lt;VirtualWebroot&gt;(new UrlInfoComparator());
30   * for (Row listRow : new IListIterable(ics.GetList(&quot;pr-out&quot;))) {
31   *     result.add(getVirtualWebroot(listRow.getLong(&quot;id&quot;)));
32   * }
33   * </pre>
34   * 
35   * 
36   * @author Dolf Dijkstra
37   * 
38   */
39  public class IListIterable implements Iterable<Row> {
40      private final IList list;
41  
42      private final int numRows;
43  
44      public IListIterable(final IList list) {
45          super();
46          this.list = list;
47          if (list != null) {
48              numRows = list.numRows();
49          } else {
50              numRows = 0;
51          }
52      }
53  
54      public Iterator<Row> iterator() {
55          if (list == null || !list.hasData()) {
56              return new Iterator<Row>() {
57  
58                  public boolean hasNext() {
59                      return false;
60                  }
61  
62                  public Row next() {
63                      return null;
64                  }
65  
66                  public void remove() {
67                      throw new RuntimeException("Can not remove");
68                  }
69  
70              };
71          }
72          return new Iterator<Row>() {
73              private int rowNum = 0;
74  
75              public boolean hasNext() {
76                  return rowNum < numRows;
77              }
78  
79              public Row next() {
80                  rowNum++;
81                  list.moveTo(rowNum);
82                  return new SingleRow(list);
83              }
84  
85              public void remove() {
86                  throw new RuntimeException("Can not remove");
87              }
88          };
89      }
90  
91      public int size() {
92          return numRows;
93      }
94  
95      public void flush() {
96          if (list != null) {
97              list.flush();
98          }
99      }
100 
101     public String getColumnName(final int i) {
102         if (list != null) {
103             return list.getColumnName(i);
104         }
105         return "";
106     }
107 
108     public String getIndirectColumnName(final int index) {
109         if (list != null) {
110             return list.getIndirectColumnName(index);
111         }
112         return "";
113     }
114 
115     public int numColumns() {
116         if (list != null) {
117             return list.numColumns();
118         }
119         return 0;
120     }
121 
122     public int numIndirectColumns() {
123         if (list != null) {
124             return list.numIndirectColumns();
125         }
126         return 0;
127     }
128 
129 }