source: java/main/src/main/java/com/framsticks/util/lang/Casting.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 772 bytes
Line 
1package com.framsticks.util.lang;
2
3
4/**
5 * @author Piotr Sniegowski
6 */
7public abstract class Casting {
8        public static <T> T tryCast(Class<T> class_, Object object) {
9                try {
10                        return class_.cast(object);
11                } catch (ClassCastException ignored) {
12                        return null;
13                }
14        }
15
16        public static <T> T assertCast(Class<T> class_, Object object) {
17                try {
18                        return class_.cast(object);
19                } catch (ClassCastException ignored) {
20                        assert false;
21                        return null;
22                }
23        }
24
25        public static <T> T throwCast(Class<T> class_, Object object) {
26                if (object == null) {
27                        throw new NullPointerException();
28                }
29                return class_.cast(object);
30        }
31
32        public static <T> T nullOrThrowCast(Class<T> class_, Object object) {
33                if (object == null) {
34                        return null;
35                }
36                return class_.cast(object);
37        }
38}
Note: See TracBrowser for help on using the repository browser.