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 tools.gsf.facade.sql;
18  
19  import COM.FutureTense.Interfaces.IList;
20  
21  import java.util.Iterator;
22  
23  /**
24   * Wrapper for an IList that turns an <tt>IList</tt> into a <tt>Iterable</tt>.
25   * <p>
26   * Sample usage:
27   * <pre>
28   * SortedSet&lt;VirtualWebroot&gt; result = new TreeSet&lt;VirtualWebroot&gt;(new UrlInfoComparator());
29   * for (Row listRow : new IListIterable(ics.GetList(&quot;pr-out&quot;))) {
30   *     result.add(getVirtualWebroot(listRow.getLong(&quot;id&quot;)));
31   * }
32   * </pre>
33   *
34   * @author Dolf Dijkstra
35   */
36  public class IListIterable implements Iterable<Row> {
37      private final IList list;
38  
39      private final int numRows;
40  
41      public IListIterable(final IList list) {
42          super();
43          this.list = list;
44          if (list != null) {
45              numRows = list.numRows();
46          } else {
47              numRows = 0;
48          }
49      }
50  
51      public Iterator<Row> iterator() {
52          if (list == null || !list.hasData()) {
53              return new Iterator<Row>() {
54  
55                  public boolean hasNext() {
56                      return false;
57                  }
58  
59                  public Row next() {
60                      return null;
61                  }
62  
63                  public void remove() {
64                      throw new RuntimeException("Can not remove");
65                  }
66  
67              };
68          }
69          return new Iterator<Row>() {
70              private int rowNum = 0;
71  
72              public boolean hasNext() {
73                  return rowNum < numRows;
74              }
75  
76              public Row next() {
77                  rowNum++;
78                  list.moveTo(rowNum);
79                  return new SingleRow(list);
80              }
81  
82              public void remove() {
83                  throw new RuntimeException("Can not remove");
84              }
85          };
86      }
87  
88      public int size() {
89          return numRows;
90      }
91  
92      public void flush() {
93          if (list != null) {
94              list.flush();
95          }
96      }
97  
98      public String getColumnName(final int i) {
99          if (list != null) {
100             return list.getColumnName(i);
101         }
102         return "";
103     }
104 
105     public String getIndirectColumnName(final int index) {
106         if (list != null) {
107             return list.getIndirectColumnName(index);
108         }
109         return "";
110     }
111 
112     public int numColumns() {
113         if (list != null) {
114             return list.numColumns();
115         }
116         return 0;
117     }
118 
119     public int numIndirectColumns() {
120         if (list != null) {
121             return list.numIndirectColumns();
122         }
123         return 0;
124     }
125 
126 }