1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.maps; 18 19 import java.util.Iterator; 20 21 import com.badlogic.gdx.graphics.Color; 22 import com.badlogic.gdx.math.Vector2; 23 import com.badlogic.gdx.utils.ObjectMap; 24 25 /** @brief Set of string indexed values representing map elements' properties, allowing to retrieve, modify and add properties to 26 * the set. */ 27 public class MapProperties { 28 29 private ObjectMap<String, Object> properties; 30 31 /** Creates an empty properties set */ 32 public MapProperties () { 33 properties = new ObjectMap<String, Object>(); 34 } 35 36 /** @param key property name 37 * @return true if and only if the property exists */ 38 public boolean containsKey (String key) { 39 return properties.containsKey(key); 40 } 41 42 /** @param key property name 43 * @return the value for that property if it exists, otherwise, null */ 44 public Object get (String key) { 45 return properties.get(key); 46 } 47 48 /** Returns the object for the given key, casting it to clazz. 49 * @param key the key of the object 50 * @param clazz the class of the object 51 * @return the object or null if the object is not in the map 52 * @throws ClassCastException if the object with the given key is not of type clazz */ 53 public <T> T get (String key, Class<T> clazz) { 54 return (T)get(key); 55 } 56 57 /** Returns the object for the given key, casting it to clazz. 58 * @param key the key of the object 59 * @param defaultValue the default value 60 * @param clazz the class of the object 61 * @return the object or the defaultValue if the object is not in the map 62 * @throws ClassCastException if the object with the given key is not of type clazz */ 63 public <T> T get (String key, T defaultValue, Class<T> clazz) { 64 Object object = get(key); 65 return object == null ? defaultValue : (T)object; 66 } 67 68 /** @param key property name 69 * @param value value to be inserted or modified (if it already existed) */ 70 public void put (String key, Object value) { 71 properties.put(key, value); 72 } 73 74 /** @param properties set of properties to be added */ 75 public void putAll (MapProperties properties) { 76 this.properties.putAll(properties.properties); 77 } 78 79 /** @param key property name to be removed */ 80 public void remove (String key) { 81 properties.remove(key); 82 } 83 84 /** Removes all properties */ 85 public void clear () { 86 properties.clear(); 87 } 88 89 /** @return iterator for the property names */ 90 public Iterator<String> getKeys () { 91 return properties.keys(); 92 } 93 94 /** @return iterator to properties' values */ 95 public Iterator<Object> getValues () { 96 return properties.values(); 97 } 98 99 } 100