1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest;
|
---|
16 |
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.NoSuchElementException;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * <p>
|
---|
25 | * Helper class that can be used to determine if an object is a sequence or a
|
---|
26 | * collection of sequences. {@code instanceof} does not work, because of
|
---|
27 | * the type erasure of generics.
|
---|
28 | * </p>
|
---|
29 | *
|
---|
30 | * @author Steffen Herbold
|
---|
31 | * @version 1.0
|
---|
32 | */
|
---|
33 | public class SequenceInstanceOf {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * <p>
|
---|
37 | * Private constructor to prevent initializing of the class.
|
---|
38 | * </p>
|
---|
39 | */
|
---|
40 | private SequenceInstanceOf() {
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * <p>
|
---|
46 | * Checks if an object is of type {@link Collection}<{@link List}<
|
---|
47 | * {@link Event}<?>>>.
|
---|
48 | * </p>
|
---|
49 | *
|
---|
50 | * @param obj
|
---|
51 | * object that is checked
|
---|
52 | * @return true, if the obj is of type {@link Collection}<{@link List}
|
---|
53 | * < {@link Event}<?>>>; false otherwise
|
---|
54 | */
|
---|
55 | public static boolean isCollectionOfSequences(Object obj) {
|
---|
56 | try {
|
---|
57 | if (obj instanceof Collection<?>) {
|
---|
58 | Object listObj = ((Collection<?>) obj).iterator().next();
|
---|
59 | if (listObj instanceof List<?>) {
|
---|
60 | if (((List<?>) listObj).iterator().next() instanceof Event) {
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | } catch (NoSuchElementException e) {
|
---|
66 | }
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * <p>
|
---|
72 | * Checks if an object is of type {@link List}<{@link Event}
|
---|
73 | * <?>>.
|
---|
74 | * </p>
|
---|
75 | *
|
---|
76 | * @param obj
|
---|
77 | * object that is checked
|
---|
78 | * @return true, if obj is of type {@link List}<{@link Event}
|
---|
79 | * <?>>; false otherwise
|
---|
80 | */
|
---|
81 | public static boolean isEventSequence(Object obj) {
|
---|
82 | try {
|
---|
83 | if (obj instanceof List<?>) {
|
---|
84 | if (((List<?>) obj).iterator().next() instanceof Event) {
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | } catch (NoSuchElementException e) {
|
---|
89 | }
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | }
|
---|