1 | package de.ugoe.cs.eventbench;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.NoSuchElementException;
|
---|
6 |
|
---|
7 | import 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 | */
|
---|
19 | public class SequenceInstanceOf {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * <p>
|
---|
23 | * Private constructor to prevent initializing of the class.
|
---|
24 | * </p>
|
---|
25 | */
|
---|
26 | private SequenceInstanceOf() {
|
---|
27 |
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>
|
---|
32 | * Checks if an object is of type {@link Collection}<{@link List}<
|
---|
33 | * {@link Event}<?>>>.
|
---|
34 | * </p>
|
---|
35 | *
|
---|
36 | * @param obj
|
---|
37 | * object that is checked
|
---|
38 | * @return true, if the obj is of type {@link Collection}<{@link List}
|
---|
39 | * < {@link Event}<?>>>; false otherwise
|
---|
40 | */
|
---|
41 | public static boolean isCollectionOfSequences(Object obj) {
|
---|
42 | try {
|
---|
43 | if (obj instanceof Collection<?>) {
|
---|
44 | Object listObj = ((Collection<?>) obj).iterator().next();
|
---|
45 | if (listObj instanceof List<?>) {
|
---|
46 | if (((List<?>) listObj).iterator().next() instanceof Event<?>) {
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | } catch (NoSuchElementException e) {
|
---|
52 | }
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * <p>
|
---|
58 | * Checks if an object is of type {@link List}<{@link Event}
|
---|
59 | * <?>>.
|
---|
60 | * </p>
|
---|
61 | *
|
---|
62 | * @param obj
|
---|
63 | * object that is checked
|
---|
64 | * @return true, if obj is of type {@link List}<{@link Event}
|
---|
65 | * <?>>; false otherwise
|
---|
66 | */
|
---|
67 | public static boolean isEventSequence(Object obj) {
|
---|
68 | try {
|
---|
69 | if (obj instanceof List<?>) {
|
---|
70 | if (((List<?>) obj).iterator().next() instanceof Event<?>) {
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | } catch (NoSuchElementException e) {
|
---|
75 | }
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|