]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaAnnotationIterator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / javaeditor / JavaAnnotationIterator.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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 package org.eclipse.jdt.internal.ui.javaeditor;
12
13 import java.util.Iterator;
14
15 import org.eclipse.core.runtime.CoreException;
16
17 import org.eclipse.core.resources.IMarker;
18
19 import org.eclipse.jface.text.source.Annotation;
20
21 import org.eclipse.ui.texteditor.MarkerAnnotation;
22
23
24 /**
25  * Filters problems based on their types.
26  */
27 public class JavaAnnotationIterator implements Iterator<Annotation> {
28
29         private Iterator<Annotation> fIterator;
30         private Annotation fNext;
31         private boolean fReturnAllAnnotations;
32
33
34         /**
35          * Returns a new JavaAnnotationIterator.
36          * @param parent the parent iterator to iterate over annotations
37          * @param returnAllAnnotations whether to return all annotations or just problem annotations
38          */
39         public JavaAnnotationIterator(Iterator<Annotation> parent, boolean returnAllAnnotations) {
40                 fReturnAllAnnotations= returnAllAnnotations;
41                 fIterator= parent;
42                 skip();
43         }
44
45         private void skip() {
46                 while (fIterator.hasNext()) {
47                         Annotation next= fIterator.next();
48
49                         if (next.isMarkedDeleted())
50                                 continue;
51
52                         if (fReturnAllAnnotations || next instanceof IJavaAnnotation || isProblemMarkerAnnotation(next)) {
53                                 fNext= next;
54                                 return;
55                         }
56                 }
57                 fNext= null;
58         }
59
60         private static boolean isProblemMarkerAnnotation(Annotation annotation) {
61                 if (!(annotation instanceof MarkerAnnotation))
62                         return false;
63                 try {
64                         return(((MarkerAnnotation)annotation).getMarker().isSubtypeOf(IMarker.PROBLEM));
65                 } catch (CoreException e) {
66                         return false;
67                 }
68         }
69
70         /*
71          * @see Iterator#hasNext()
72          */
73         public boolean hasNext() {
74                 return fNext != null;
75         }
76
77         /*
78          * @see Iterator#next()
79          */
80         public Annotation next() {
81                 try {
82                         return fNext;
83                 } finally {
84                         skip();
85                 }
86         }
87
88         /*
89          * @see Iterator#remove()
90          */
91         public void remove() {
92                 throw new UnsupportedOperationException();
93         }
94 }