AI2 Component  (Version nb184)
TypeUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright © 2017 Massachusetts Institute of Technology, All rights reserved.
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime.util;
7 
9 
10 public final class TypeUtil {
11 
12  private TypeUtil() {}
13 
14  public static <T> T cast(Object o, Class<T> tClass, String expected) {
15  if (o == null) {
16  return null;
17  } else if (tClass.isInstance(o)) {
18  return tClass.cast(o);
19  } else {
20  throw new DispatchableError(ErrorMessages.ERROR_INVALID_TYPE, o.getClass().getSimpleName(), expected);
21  }
22  }
23 
24  public static <T> T castNotNull(Object o, Class<T> tClass, String expected) {
25  if (o == null) {
26  throw new DispatchableError(ErrorMessages.ERROR_INVALID_TYPE, "null", expected);
27  } else {
28  return cast(o, tClass, expected);
29  }
30  }
31 }
com.google.appinventor.components.runtime.errors.DispatchableError
Definition: DispatchableError.java:12
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util.TypeUtil
Definition: TypeUtil.java:10
com.google.appinventor.components
com.google.appinventor.components.runtime.util.TypeUtil.cast
static< T > T cast(Object o, Class< T > tClass, String expected)
Definition: TypeUtil.java:14
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_TYPE
static final int ERROR_INVALID_TYPE
Definition: ErrorMessages.java:239
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google
com
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.util.TypeUtil.castNotNull
static< T > T castNotNull(Object o, Class< T > tClass, String expected)
Definition: TypeUtil.java:24
com.google.appinventor