1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.merger.diff;
25 
26 import org.openoffice.xmerge.merger.Iterator;
27 
28 /**
29  *  <p>This is an implementation of the <code>Iterator</code> interface.
30  *  It is based upon a simple <code>Object</code> array.</p>
31  *
32  * <p>Note: this class is not thread safe for performance reasons.</p>
33  *
34  *  @author smak
35  */
36 public final class ObjectArrayIterator implements Iterator {
37 
38 
39     /**
40      *  The <code>Object</code> array.
41      */
42     private Object [] objArray;
43     private int currentPosition;
44 
45 
46     /**
47      *  Private default constructor.
48      */
ObjectArrayIterator()49     private ObjectArrayIterator() {
50         // do not allow user new a ObjectArrayIterator without argument
51     }
52 
53 
54     /**
55      *  Standard constructor.
56      *
57      *  @param  objArray  The <code>Object</code> array.
58      */
ObjectArrayIterator(Object [] objArray)59     public ObjectArrayIterator(Object [] objArray) {
60         if (objArray != null) {
61             this.objArray = new Object[objArray.length];
62             System.arraycopy(objArray, 0, this.objArray, 0, objArray.length);
63             currentPosition = 0;
64         } else {
65             this.objArray = new Object[0];
66         }
67     }
68 
69 
next()70     public Object next() {
71         if (currentPosition < objArray.length - 1) {
72             currentPosition++;
73             return currentElement();
74         } else {
75             return null;
76         }
77 
78     }
79 
80 
previous()81     public Object previous() {
82         if (currentPosition > 0) {
83             currentPosition--;
84             return currentElement();
85         } else {
86             return null;
87         }
88     }
89 
90 
start()91     public Object start() {
92         currentPosition = 0;
93         return currentElement();
94     }
95 
96 
end()97     public Object end() {
98         if (objArray.length > 0) {
99             currentPosition = objArray.length - 1;
100         }
101         return currentElement();
102     }
103 
104 
currentElement()105     public Object currentElement() {
106         if (objArray.length > 0) {
107             return objArray[currentPosition];
108         } else {
109             return null;
110         }
111     }
112 
113 
114     /**
115      *  Replace current <code>Object</code>.
116      *
117      *  @param  object  <code>Object</code> to replace.
118      */
replace(Object object)119     public void replace(Object object) {
120         objArray[currentPosition] = object;
121     }
122 
123 
124     /**
125      *  Insert <code>Object</code> after current <code>Object</code>.
126      *
127      *  @param  object  <code>Object</code> to insert.
128      */
insert(Object object)129     public void insert(Object object) {
130         Object [] objArray2 = new Object[objArray.length+1];
131 
132         // copy the array content up before the currentposition
133         if (currentPosition > 0) {
134             System.arraycopy(objArray, 0, objArray2, 0, currentPosition);
135         }
136 
137         objArray2[currentPosition] = object;
138 
139         // copy the array content up after the currentposition
140         System.arraycopy(objArray, currentPosition, objArray2,
141                    currentPosition + 1, objArray.length - currentPosition);
142 
143         objArray = objArray2;
144         currentPosition++;
145     }
146 
147     /**
148      *  Append <code>Object</code> after current <code>Object</code>.
149      *
150      *  @param  object  <code>Object</code> to append.
151      */
append(Object object)152     public void append(Object object) {
153         Object [] objArray2 = new Object[objArray.length + 1];
154 
155         int newPosition = currentPosition + 1;
156 
157         // copy the array content up to the currentposition
158         System.arraycopy(objArray, 0, objArray2, 0, newPosition);
159 
160         objArray2[newPosition] = object;
161 
162         // copy the array content up after the currentposition
163         if (currentPosition < objArray.length - 1) {
164             System.arraycopy(objArray, newPosition, objArray2,
165                    newPosition + 1, objArray.length - newPosition);
166         }
167 
168         objArray = objArray2;
169     }
170 
171     /**
172      *  Remove current <code>Object</code>.
173      */
remove()174     public void remove() {
175         Object [] objArray2 = new Object[objArray.length - 1];
176 
177         // copy the array content up before the currentposition
178         if (currentPosition > 0) {
179             System.arraycopy(objArray, 0, objArray2, 0, currentPosition);
180         }
181 
182         // copy the array content up after the currentposition
183         if (currentPosition < objArray.length - 1) {
184             System.arraycopy(objArray, currentPosition + 1, objArray2,
185                    currentPosition, objArray.length - currentPosition - 1);
186         }
187 
188         objArray = objArray2;
189 
190         if (currentPosition == objArray.length)
191             currentPosition--;
192     }
193 
elementCount()194     public int elementCount() {
195         return objArray.length;
196     }
197 
equivalent(Object obj1, Object obj2)198     public boolean equivalent(Object obj1, Object obj2) {
199         return obj1.equals(obj2);
200     }
201 
refresh()202     public void refresh() {
203         // do nothing
204     }
205 }
206 
207