KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > mapping > MappingSetIteratorImpl


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.mapping;
24
25 import java.util.HashMap JavaDoc;
26
27 /**
28  * Iterator for iterating through a MappingSet performing memorization of the
29  * tables encoutered to allow doubles management.
30  */

31 public class MappingSetIteratorImpl implements MappingSetIterator
32 {
33     private int current = -1;
34     private int currentIndex = -1;
35     private int firstOccurenceIndex = -1;
36     private MappingInfo[] array;
37     private boolean reverse = false;
38     private HashMap JavaDoc tableFirstOccurences = new HashMap JavaDoc();
39
40     /**
41      * Constructor for MappingSetIterator.
42      */

43     public MappingSetIteratorImpl(MappingInfo[] tms, boolean reverse)
44     {
45         array = tms;
46         this.reverse = reverse;
47         if (tms != null)
48         {
49             if (reverse)
50                 current = array.length;
51             else
52                 current = -1;
53         }
54     }
55
56     public MappingSetIteratorImpl(MappingInfo[] tms)
57     {
58         this(tms, false);
59     }
60
61 // public MappingSetIterator(List tms, boolean reverse)
62
// {
63
// this((MappingInfo[])tms.toArray(new MappingInfo[tms.size()]), reverse);
64
// }
65
//
66
// public MappingSetIterator(List tms)
67
// {
68
// this((MappingInfo[])tms.toArray(new MappingInfo[tms.size()]), false);
69
// }
70
//
71

72     public boolean hasNext()
73     {
74         if (array == null)
75             return false;
76         if (reverse)
77             return current > 0;
78         else
79             return (current + 1) < array.length;
80     }
81
82     public MappingInfo next()
83     {
84         MappingInfo ret = null;
85         if (hasNext())
86         {
87             // update position
88
if (reverse)
89                 current--;
90             else
91                 current++;
92             registerTable();
93             ret = array[current];
94         }
95         return ret;
96     }
97     
98     public boolean isTableFirstOccurence()
99     {
100         return firstOccurenceIndex == currentIndex;
101     }
102     
103     public int getFirstOccurenceTableIndex()
104     {
105         return firstOccurenceIndex;
106     }
107     
108     private void registerTable()
109     {
110         if (hasNext())
111         {
112             TableMapping wTable = array[current].getTableMapping();
113             currentIndex = wTable.getTableIndex();
114             Integer JavaDoc firstOccurenceObject = (Integer JavaDoc)tableFirstOccurences.get(wTable.getTableName());
115             if (firstOccurenceObject == null) // first time encoutered
116
{
117                 tableFirstOccurences.put(wTable.getTableName(), new Integer JavaDoc(currentIndex));
118                 firstOccurenceIndex = currentIndex;
119             }
120             else
121                 firstOccurenceIndex = firstOccurenceObject.intValue();
122         }
123     }
124
125 }
126
Popular Tags