1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.shared.trace;
20
21 import java.util.Iterator;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 /**
26 * Trace method calls to an iterator
27 *
28 * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
29 * @version $Revision: 1151677 $ $Date: 2011-07-27 19:03:59 -0500 (Wed, 27 Jul 2011) $
30 */
31 public class TracingIterator<E> implements Iterator<E>
32 {
33 private final Iterator<E> _iterator;
34
35 private final TracingSupport _tracer;
36
37 public TracingIterator(Iterator<E> iterator)
38 {
39 _iterator = iterator;
40 _tracer = new TracingSupport(iterator.getClass());
41 }
42
43 public static <E> TracingIterator<E> create(Iterator<E> iterator)
44 {
45 return new TracingIterator<E>(iterator);
46 }
47
48 /**
49 * @return the iterator
50 */
51 public Iterator<E> getIterator()
52 {
53 return _iterator;
54 }
55
56 public boolean hasNext()
57 {
58 return _tracer.trace("hasNext", new Closure<Boolean>()
59 {
60 public Boolean call()
61 {
62 return _iterator.hasNext();
63 }
64 });
65 }
66
67 public E next()
68 {
69 return _tracer.trace("next", new Closure<E>()
70 {
71 public E call()
72 {
73 return _iterator.next();
74 }
75 });
76 }
77
78 public void remove()
79 {
80 _tracer.trace("remove", new Closure<Object>()
81 {
82 public Object call()
83 {
84 _iterator.remove();
85 return Void.class;
86 }
87 });
88 }
89
90 /**
91 * @return
92 * @see org.apache.myfaces.shared.trace.TracingSupport#getLevel()
93 */
94 public Level getLevel()
95 {
96 return _tracer.getLevel();
97 }
98
99 /**
100 * @return
101 * @see org.apache.myfaces.shared.trace.TracingSupport#getLogger()
102 */
103 public Logger getLogger()
104 {
105 return _tracer.getLogger();
106 }
107
108 /**
109 * @return
110 * @see org.apache.myfaces.shared.trace.TracingSupport#getSourceClass()
111 */
112 public String getSourceClass()
113 {
114 return _tracer.getSourceClass();
115 }
116
117 /**
118 * @param level
119 * @see org.apache.myfaces.shared.trace.TracingSupport#setLevel(java.util.logging.Level)
120 */
121 public void setLevel(Level level)
122 {
123 _tracer.setLevel(level);
124 }
125
126 /**
127 * @param logger
128 * @see org.apache.myfaces.shared.trace.TracingSupport#setLogger(java.util.logging.Logger)
129 */
130 public void setLogger(Logger logger)
131 {
132 _tracer.setLogger(logger);
133 }
134
135 /**
136 * @param className
137 * @see org.apache.myfaces.shared.trace.TracingSupport#setSourceClass(java.lang.String)
138 */
139 public void setSourceClass(String className)
140 {
141 _tracer.setSourceClass(className);
142 }
143 }