source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/SequenceInstanceOf.java @ 208

Last change on this file since 208 was 208, checked in by sherbold, 13 years ago

+ added helper class de.ugoe.cs.eventbench.SequenceInstanceOf? to perform runtime type-checks with events sequences

  • Property svn:mime-type set to text/plain
File size: 1.7 KB
Line 
1package de.ugoe.cs.eventbench;
2
3import java.util.Collection;
4import java.util.List;
5import java.util.NoSuchElementException;
6
7import de.ugoe.cs.eventbench.data.Event;
8
9/**
10 * <p>
11 * Helper class that can be used to determine if an object is a sequence or a
12 * collection of sequences. {@code instanceof} does not work, because of
13 * the type erasure of generics.
14 * </p>
15 *
16 * @author Steffen Herbold
17 * @version 1.0
18 */
19public class SequenceInstanceOf {
20
21        /**
22         * <p>
23         * Checks if an object is of type {@link Collection}&lt;{@link List}&lt;
24         * {@link Event}&lt;?&gt;&gt;&gt;.
25         * </p>
26         *
27         * @param obj
28         *            object that is checked
29         * @return true, if the obj is of type {@link Collection}&lt;{@link List}
30         *         &lt; {@link Event}&lt;?&gt;&gt;&gt;; false otherwise
31         */
32        public static boolean isCollectionOfSequences(Object obj) {
33                try {
34                        if (obj instanceof Collection<?>) {
35                                Object listObj = ((Collection<?>) obj).iterator().next();
36                                if (listObj instanceof List<?>) {
37                                        if (((List<?>) listObj).iterator().next() instanceof Event<?>) {
38                                                return true;
39                                        }
40                                }
41                        }
42                } catch (NoSuchElementException e) {
43                }
44                return false;
45        }
46
47        /**
48         * <p>
49         * Checks if an object is of type {@link List}&lt;{@link Event}
50         * &lt;?&gt;&gt;.
51         * </p>
52         *
53         * @param obj
54         *            object that is checked
55         * @return true, if obj is of type {@link List}&lt;{@link Event}
56         *         &lt;?&gt;&gt;; false otherwise
57         */
58        public static boolean isEventSequence(Object obj) {
59                try {
60                        if (obj instanceof List<?>) {
61                                if (((List<?>) obj).iterator().next() instanceof Event<?>) {
62                                        return true;
63                                }
64                        }
65                } catch (NoSuchElementException e) {
66                }
67                return false;
68        }
69
70}
Note: See TracBrowser for help on using the repository browser.