Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.equinox.internal.provisional.p2.artifact.repository; |
13 |
|
14 |
import java.util.*; |
15 |
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey; |
16 |
|
17 |
/** |
18 |
* An iterator over an IArtifactRepository |
19 |
* Returns IArtifactKey's and IArtifactDescriptor's. First a key is returned, |
20 |
* then that key's descriptors, and again for the next key. |
21 |
*/ |
22 |
public class ArtifactIterator implements Iterator { |
23 |
private final IArtifactRepository artifactRepository; |
24 |
private Iterator keys = null; |
25 |
private Iterator descriptors = null; |
26 |
private boolean returnKeys = false; |
27 |
private boolean returnDescriptors = false; |
28 |
|
29 |
/** |
30 |
* @param artifactRepository - the artifact repository |
31 |
* @param returnKeys - whether or not to include IArtifactKey's in the iteration |
32 |
* @param returnDescriptors - whether or not to include IArtifactDescriptor's in the iteration |
33 |
*/ |
34 |
public ArtifactIterator(IArtifactRepository artifactRepository, boolean returnKeys, boolean returnDescriptors) { |
35 |
this.artifactRepository = artifactRepository; |
36 |
this.returnDescriptors = returnDescriptors; |
37 |
this.returnKeys = returnKeys; |
38 |
|
39 |
//initialize the key interator |
40 |
keys = Arrays.asList(artifactRepository.getArtifactKeys()).iterator(); |
41 |
} |
42 |
|
43 |
/** |
44 |
* See {@link Iterator#hasNext()} |
45 |
*/ |
46 |
public boolean hasNext() { |
47 |
if (!returnDescriptors && !returnKeys) |
48 |
return false; //perhaps this is an exception? |
49 |
|
50 |
if (!returnDescriptors) |
51 |
return keys.hasNext(); //keys only |
52 |
|
53 |
if (returnKeys) //keys and descriptors |
54 |
return (descriptors != null && descriptors.hasNext()) || keys.hasNext(); |
55 |
|
56 |
//descriptors only |
57 |
if (descriptors != null && descriptors.hasNext()) |
58 |
return true; |
59 |
while (keys.hasNext()) { |
60 |
IArtifactKey key = (IArtifactKey) keys.next(); |
61 |
descriptors = Arrays.asList(artifactRepository.getArtifactDescriptors(key)).iterator(); |
62 |
if (descriptors.hasNext()) |
63 |
return true; |
64 |
} |
65 |
return false; |
66 |
} |
67 |
|
68 |
/** |
69 |
* See {@link Iterator#next()} |
70 |
*/ |
71 |
public Object next() { |
72 |
if (!returnDescriptors && !returnKeys) |
73 |
throw new NoSuchElementException(); |
74 |
|
75 |
if (!returnDescriptors) |
76 |
return keys.next(); //keys only |
77 |
|
78 |
if (returnKeys) { |
79 |
//keys and descriptors |
80 |
if (descriptors != null && descriptors.hasNext()) |
81 |
return descriptors.next(); |
82 |
|
83 |
IArtifactKey nextKey = (IArtifactKey) keys.next(); |
84 |
descriptors = Arrays.asList(artifactRepository.getArtifactDescriptors(nextKey)).iterator(); |
85 |
return nextKey; |
86 |
} |
87 |
|
88 |
//descriptors only |
89 |
if (descriptors != null && descriptors.hasNext()) |
90 |
return descriptors.next(); |
91 |
while (keys.hasNext()) { |
92 |
IArtifactKey key = (IArtifactKey) keys.next(); |
93 |
descriptors = Arrays.asList(artifactRepository.getArtifactDescriptors(key)).iterator(); |
94 |
if (!descriptors.hasNext()) |
95 |
continue; |
96 |
return descriptors.next(); |
97 |
} |
98 |
throw new NoSuchElementException(); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Returns true if the iteration contains more IArtifactKeys |
103 |
* @return boolean |
104 |
*/ |
105 |
public boolean hasNextKey() { |
106 |
if (!returnKeys) |
107 |
return false; |
108 |
return keys.hasNext(); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Get the next IArtifactKey skipping over any IArtifactDescriptor's |
113 |
* @return IArtifactKey |
114 |
* @throws IllegalStateException if this iterator is not returning keys |
115 |
*/ |
116 |
public IArtifactKey nextKey() { |
117 |
if (!returnKeys) |
118 |
throw new IllegalStateException(); |
119 |
IArtifactKey next = (IArtifactKey) keys.next(); |
120 |
if (returnDescriptors) |
121 |
descriptors = Arrays.asList(artifactRepository.getArtifactDescriptors(next)).iterator(); |
122 |
return next; |
123 |
} |
124 |
|
125 |
/** |
126 |
* See {@link Iterator#remove()} |
127 |
* This operation is not supported |
128 |
* @throws UnsupportedOperationException |
129 |
*/ |
130 |
public void remove() { |
131 |
throw new UnsupportedOperationException(); |
132 |
} |
133 |
} |