All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.stjs.javascript.JSCollections Maven / Gradle / Ivy

/**
 *  Copyright 2011 Alexandru Craciun, Eyal Kaspi
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.stjs.javascript;

import org.stjs.javascript.annotation.GlobalScope;
import org.stjs.javascript.annotation.Template;

/**
 * this class offers map and array builders for both client and server side. For the server side, it delegates the execution to an instance of
 * {@link JSCollectionsImplementor}. On the client side, it simply throws an {@link UnsupportedOperationException} like all the client-side
 * bridges.
 * @author acraciun
 */
@GlobalScope
@SuppressWarnings("unchecked")
public class JSCollections {

	@Template("array")
	public static  Array $array(V... values) {
		Array a = new Array();
		a.splice(0, 0, values);
		return a;
	}

	@Template("map")
	public static  Map $map() {
		return new Map();
	}

	@Template("map")
	public static  Map $map(K k1, V v1) {
		Map m = new Map();
		m.$put(k1, v1);
		return m;
	}

	@Template("map")
	public static  Map $map(K k1, V v1, K k2, V v2) {
		Map m = new Map();
		m.$put(k1, v1);
		m.$put(k2, v2);
		return m;
	}

	@Template("map")
	public static  Map $map(K k1, V v1, K k2, V v2, K k3, V v3) {
		Map m = new Map();
		m.$put(k1, v1);
		m.$put(k2, v2);
		m.$put(k3, v3);
		return m;
	}

	@Template("map")
	public static  Map $map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
		Map m = new Map();
		m.$put(k1, v1);
		m.$put(k2, v2);
		m.$put(k3, v3);
		m.$put(k4, v4);
		return m;
	}

	@Template("map")
	public static  Map $map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
		Map m = new Map();
		m.$put(k1, v1);
		m.$put(k2, v2);
		m.$put(k3, v3);
		m.$put(k4, v4);
		m.$put(k5, v5);
		return m;
	}

	@Template("map")
	@SuppressWarnings("unchecked")
	public static  Map $map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, Object... morePairs) {
		Map m = new Map();
		m.$put(k1, v1);
		m.$put(k2, v2);
		m.$put(k3, v3);
		m.$put(k4, v4);
		m.$put(k5, v5);
		for (int i = 0; i < morePairs.length - 1; i += 2) {
			m.$put((K) morePairs[i], (V) morePairs[i + 1]);
		}
		return m;
	}

	@Template("properties")
	public static  Array $castArray(T[] a) {
		return $array(a);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy