source: trunk/autoquest-core-events/src/main/java/de/ugoe/cs/autoquest/SequenceInstanceOf.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 2.5 KB
Line 
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
15package de.ugoe.cs.autoquest;
16
17import java.util.Collection;
18import java.util.List;
19import java.util.NoSuchElementException;
20
21import 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 */
33public 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}&lt;{@link List}&lt;
47         * {@link Event}&lt;?&gt;&gt;&gt;.
48         * </p>
49         *
50         * @param obj
51         *            object that is checked
52         * @return true, if the obj is of type {@link Collection}&lt;{@link List}
53         *         &lt; {@link Event}&lt;?&gt;&gt;&gt;; 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}&lt;{@link Event}
73         * &lt;?&gt;&gt;.
74         * </p>
75         *
76         * @param obj
77         *            object that is checked
78         * @return true, if obj is of type {@link List}&lt;{@link Event}
79         *         &lt;?&gt;&gt;; 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}
Note: See TracBrowser for help on using the repository browser.