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

org.stjs.javascript.Map 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 java.util.HashMap;
import java.util.Iterator;

import org.stjs.javascript.annotation.ServerSide;
import org.stjs.javascript.annotation.SyntheticType;
import org.stjs.javascript.annotation.Template;

/**
 * This interface represents a normal object in javascript (that acts as a map). The key can be only be a String! is done on the keys to have the
 * javascript equivalent of 
* for(var key in map)
* The methods are prefixed with $ to let the generator know that is should generate bracket access instead, i.e
* map.$get(key) => map[key]
* map.$put(key, value) => map[key]=value * @author acraciun */ @SyntheticType public class Map implements Iterable { private final java.util.Map map; /** * Constructor is package private, it isn't supposed to be used directly by clients of the API. Use JSCollections.$map() instead. */ protected Map() { this(new HashMap()); } /** * Constructor is package private, it isn't supposed to be used directly by clients of the API. Use JSCollections.$map() instead. */ private Map(java.util.Map map) { this.map = map; } /** * constructors used on the server side only. It only wraps the given map, no copy is done * @param list * @return */ @ServerSide public static Map wrap(java.util.Map map) { return new Map(map); } /** * constructors used on the server side only. It copies the given parameter * @param list * @return */ @ServerSide public static Map copyOf(java.util.Map map) { return new Map(new HashMap(map)); } /** * this gives access to the java implementation. used on the server side only * @return */ @ServerSide public java.util.Map java() { return map; } public Iterator iterator() { return map.keySet().iterator(); } @Template("get") public V $get(K key) { return map.get(key); } @Template("put") public void $put(K key, V value) { map.put(key, value); } @Template("delete") public void $delete(K key) { map.remove(key); } @Override public String toString() { return map.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy