1 package org.csc.phynixx.common.cast;
2
3 /*
4 * #%L
5 * phynixx-common
6 * %%
7 * Copyright (C) 2014 Christoph Schmidt-Casdorff
8 * %%
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * #L%
21 */
22
23
24 public class ImplementorUtils {
25
26 /**
27 * versucht, eine Objekt in den Zieltyp <code>targetClass</code> zu casten.
28 * <p/>
29 * Wird <code>null</code> uebergeben, so wird <code>null</code> geliefert.
30 *
31 * @param obj
32 * @param targetClass
33 * @return
34 * @throws IllegalArgumentException object oder Zielklasse sind undefiniert
35 * @throws ClassCastException cast konnte nicht durch gefuehrt werden
36 */
37 public static <T> T cast(final Object obj, final Class<T> targetClass) {
38 if (targetClass == null) {
39 throw new IllegalArgumentException("Zielklasse, in welche gecasted werden soll, ist anzugeben.");
40 }
41 if (obj == null) {
42 return null;
43 }
44 return new ObjectImplementor<Object>(obj).cast(targetClass);
45 }
46
47 /**
48 * prueft, ob eine Objekt in den Zieltyp <code>targetClass</code> zu casten
49 * ist.
50 * <p/>
51 * Wird <code>null</code> uebergeben, so wird <code>false</code> geliefert.
52 *
53 * @param obj
54 * @param targetClass
55 * @return
56 * @throws IllegalArgumentException object oder Zielklasse sind undefiniert
57 * @throws ClassCastException cast konnte nicht durch gefuehrt werden
58 */
59 public static <T> boolean isImplementationOf(final Object obj, final Class<T> targetClass) {
60 if (targetClass == null) {
61 throw new IllegalArgumentException("Zielklasse, in welche gecasted werden soll, ist anzugeben.");
62 }
63 if (obj == null) {
64 return false;
65 }
66 return new ObjectImplementor<Object>(obj).isImplementationOf(targetClass);
67 }
68
69 }