Java Code Examples for org.rapidoid.u.U#cast()

The following examples show how to use org.rapidoid.u.U#cast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcClient.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private <T> List<T> fetchData(Class<T> resultType, Mapper<ResultSet, T> resultMapper,
                              String sql, Map<String, ?> namedArgs, Object[] args) {

	try (Connection conn = provideConnection();
	     PreparedStatement stmt = JDBC.prepare(conn, sql, namedArgs, args);
	     ResultSet rs = stmt.executeQuery()) {

		if (resultMapper != null) {
			return JDBC.rows(resultMapper, rs);

		} else {
			if (resultType.equals(Map.class)) {
				return U.cast(JDBC.rows(rs));
			} else {
				return JDBC.rows(resultType, rs);
			}
		}

	} catch (Exception e) {
		throw U.rte(e);
	}
}
 
Example 2
Source File: Coll.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> List<T> range(Iterable<T> items, int from, int to) {
	U.must(from <= to, "'from' (%s) must be <= 'to' (%s)!", from, to);

	if (from == to) {
		return Collections.emptyList();
	}

	if (items instanceof Results) {
		Results results = (Results) items;
		return results.page(from, to - from);
	}

	List<?> list = (items instanceof List<?>) ? (List<?>) items : U.list(items);

	from = Math.min(from, list.size());
	to = Math.min(to, list.size());

	return U.cast(list.subList(from, to));
}
 
Example 3
Source File: JSTest.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompileJS() throws ScriptException {
	eq(((Number) JS.compile("1 + 2").eval()).intValue(), 3);
	eq(JS.compile("1 + 'ab'").eval(), "1ab");

	Map<String, Object> map = U.cast(U.map("U", new U()));
	SimpleBindings bindings = new SimpleBindings(map);

	Object res1;
	try {
		// Rhino style
		res1 = JS.compile("(function (x) { return U.str(x); })('hey')").eval(bindings);
	} catch (Exception e) {
		// Nashorn style
		res1 = JS.compile("(function (x) { return U.class.static.str(x); })('hey')").eval(bindings);
	}

	eq(res1, "hey");
}
 
Example 4
Source File: English.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private static Map<String, String> loadIrregularPlural() {
	Properties props = new Properties();

	try {
		props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("irregular-plural.txt"));
	} catch (IOException e) {
		Log.error("Couldn't load irregular plural!", e);
	}

	return U.cast(props);
}
 
Example 5
Source File: ByType.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public T findByType(Class<? extends K> type) {
	T value = null;

	for (Class<? extends K> cls = type; cls.getSuperclass() != null; cls = U.cast(cls.getSuperclass())) {
		value = mappings.get(cls);

		if (value != null) {
			break;
		}
	}

	return value;
}
 
Example 6
Source File: ScreenBean.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Screen addSubMenuItem(String targetUrl, int order, String caption, int subItemOrder, String subItemCaption) {
	U.must(U.notEmpty(targetUrl), "The target URL cannot be empty!");

	String key = menuKey(order, caption);
	Map<String, Object> subMenu = U.cast(menu.computeIfAbsent(key, x -> Coll.synchronizedMap()));

	String subKey = menuKey(subItemOrder, subItemCaption);
	subMenu.put(subKey, targetUrl);

	return this;
}
 
Example 7
Source File: GUI.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static Var<List<String>> listVar(String name, List<String> defaultValue) {
	Class<List<String>> type = U.cast(List.class);
	return new ReqDataVar<>(name, type, defaultValue);
}
 
Example 8
Source File: DelegatingParamsAwareReqHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public DelegatingParamsAwareReqHandler(FastHttp http, HttpRoutes routes, RouteOptions options, OneParamLambda<?, ?> handler) {
	super(http, options);
	this.handler = U.cast(handler);
}
 
Example 9
Source File: SixParamLambdaHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public SixParamLambdaHandler(FastHttp http, HttpRoutes routes, RouteOptions options, SixParamLambda<?, ?, ?, ?, ?, ?, ?> lambda) {
	super(http, routes, options, lambda);
	this.lambda = U.cast(lambda);
}
 
Example 10
Source File: ThreeParamLambdaHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public ThreeParamLambdaHandler(FastHttp http, HttpRoutes routes, RouteOptions options, ThreeParamLambda<?, ?, ?, ?> lambda) {
	super(http, routes, options, lambda);
	this.lambda = U.cast(lambda);
}
 
Example 11
Source File: FourParamLambdaHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public FourParamLambdaHandler(FastHttp http, HttpRoutes routes, RouteOptions options, FourParamLambda<?, ?, ?, ?, ?> lambda) {
	super(http, routes, options, lambda);
	this.lambda = U.cast(lambda);
}
 
Example 12
Source File: WatcherThread.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private Path getChild(Path dir, WatchEvent<?> event) {
	WatchEvent<Path> ev = U.cast(event);
	Path path = ev.context();
	return dir.resolve(path);
}
 
Example 13
Source File: OneParamLambdaHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public OneParamLambdaHandler(FastHttp http, HttpRoutes routes, RouteOptions options, OneParamLambda<?, ?> lambda) {
	super(http, routes, options, lambda);
	this.lambda = U.cast(lambda);
}
 
Example 14
Source File: DelegatingParamsAwareRespHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public DelegatingParamsAwareRespHandler(FastHttp http, HttpRoutes routes, RouteOptions options, OneParamLambda<?, ?> handler) {
	super(http, options);
	this.handler = U.cast(handler);
}
 
Example 15
Source File: JdbcClient.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public Results<Map<String, Object>> query(String sql, Map<String, ?> namedArgs) {
	return U.cast(query(Map.class, sql, namedArgs));
}
 
Example 16
Source File: JPQL.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> namedArgs() {
	return U.cast(namedArgs);
}
 
Example 17
Source File: GUI.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static Var<List<String>> listVar(String name) {
	Class<List<String>> type = U.cast(List.class);
	return new ReqDataVar<>(name, type, U.list());
}
 
Example 18
Source File: JdbcClient.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public Results<Map<String, Object>> query(String sql, Object... args) {
	return U.cast(query(Map.class, sql, args));
}
 
Example 19
Source File: Expectation.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private <T> T as(Class<T> type) {
	U.must(Cls.instanceOf(target, type), "Expected a type '%s', but found: %s", type, target);
	return U.cast(target);
}
 
Example 20
Source File: DelegatingParamsAwareReqRespHandler.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public DelegatingParamsAwareReqRespHandler(FastHttp http, HttpRoutes routes, RouteOptions options, TwoParamLambda<?, ?, ?> handler) {
	super(http, options);
	this.handler = U.cast(handler);
}