001package com.github.theholywaffle.teamspeak3.api.wrapper; 002 003/* 004 * #%L 005 * TeamSpeak 3 Java API 006 * %% 007 * Copyright (C) 2014 Bert De Geyter 008 * %% 009 * Permission is hereby granted, free of charge, to any person obtaining a copy 010 * of this software and associated documentation files (the "Software"), to deal 011 * in the Software without restriction, including without limitation the rights 012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 013 * copies of the Software, and to permit persons to whom the Software is 014 * furnished to do so, subject to the following conditions: 015 * 016 * The above copyright notice and this permission notice shall be included in 017 * all copies or substantial portions of the Software. 018 * 019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 025 * THE SOFTWARE. 026 * #L% 027 */ 028 029import com.github.theholywaffle.teamspeak3.api.Property; 030 031import java.util.Map; 032 033/** 034 * A wrapper class around a {@link Map}. 035 * <p> 036 * We use wrapper classes instead of just passing around plain Maps because 037 * </p><ul> 038 * <li>it's more descriptive</li> 039 * <li>there's no confusion between types (e.g. {@code Client} and {@code Channel})</li> 040 * <li>we can create getters for the specific use-cases</li> 041 * <li>we don't have to check for {@code null} each time</li> 042 * </ul> 043 */ 044public class Wrapper { 045 046 private final Map<String, String> map; 047 048 /** 049 * Creates a new wrapper around the given map. 050 * 051 * @param map 052 * the Map to abstract, cannot be {@code null} 053 */ 054 public Wrapper(Map<String, String> map) { 055 this.map = map; 056 } 057 058 /** 059 * Gets the map underlying this {@code Wrapper}. 060 * 061 * @return the underlying map 062 */ 063 public Map<String, String> getMap() { 064 return map; 065 } 066 067 /** 068 * Gets a property as a boolean from the underlying map. 069 * Returns {@code true} if the property exists in the map and its value is {@code "1"}. 070 * 071 * @param propertyName 072 * the name of the property 073 * 074 * @return the boolean value of the property or {@code false} if the property doesn't exist 075 */ 076 public boolean getBoolean(String propertyName) { 077 return getInt(propertyName) == 1; 078 } 079 080 /** 081 * Gets a property as a boolean from the underlying map. 082 * Returns {@code true} if the property exists in the map and its value is {@code "1"}. 083 * 084 * @param property 085 * the property 086 * 087 * @return the boolean value of the property or {@code false} if the property doesn't exist 088 */ 089 public boolean getBoolean(Property property) { 090 return getBoolean(property.getName()); 091 } 092 093 /** 094 * Gets a property as a double from the underlying map. 095 * If the property doesn't exist in the underlying map, {@code -1.0} is returned. 096 * 097 * @param propertyName 098 * the name of the property 099 * 100 * @return the double value of the property or {@code -1.0} if the property doesn't exist 101 */ 102 public double getDouble(String propertyName) { 103 final String value = get(propertyName); 104 if (value == null || value.isEmpty()) { 105 return -1D; 106 } 107 return Double.valueOf(value); 108 } 109 110 /** 111 * Gets a property as a double from the underlying map. 112 * If the property doesn't exist in the underlying map, {@code -1.0} is returned. 113 * 114 * @param property 115 * the property 116 * 117 * @return the double value of the property or {@code -1.0} if the property doesn't exist 118 */ 119 public double getDouble(Property property) { 120 return getDouble(property.getName()); 121 } 122 123 /** 124 * Gets a property as a long from the underlying map. 125 * If the property doesn't exist in the underlying map, {@code -1} is returned. 126 * 127 * @param propertyName 128 * the name of the property 129 * 130 * @return the long value of the property or {@code -1} if the property doesn't exist 131 */ 132 public long getLong(String propertyName) { 133 final String value = get(propertyName); 134 if (value == null || value.isEmpty()) { 135 return -1L; 136 } 137 return Long.valueOf(value); 138 } 139 140 /** 141 * Gets a property as a long from the underlying map. 142 * If the property doesn't exist in the underlying map, {@code -1} is returned. 143 * 144 * @param property 145 * the property 146 * 147 * @return the long value of the property or {@code -1} if the property doesn't exist 148 */ 149 public long getLong(Property property) { 150 return getLong(property.getName()); 151 } 152 153 /** 154 * Gets a property as an integer from the underlying map. 155 * If the property doesn't exist in the underlying map, {@code -1} is returned. 156 * 157 * @param propertyName 158 * the name of the property 159 * 160 * @return the integer value of the property or {@code -1} if the property doesn't exist 161 */ 162 public int getInt(String propertyName) { 163 final String value = get(propertyName); 164 if (value == null || value.isEmpty()) { 165 return -1; 166 } 167 return Integer.valueOf(value); 168 } 169 170 /** 171 * Gets a property as an integer from the underlying map. 172 * If the property doesn't exist in the underlying map, {@code -1} is returned. 173 * 174 * @param property 175 * the property 176 * 177 * @return the integer value of the property or {@code -1} if the property doesn't exist 178 */ 179 public int getInt(Property property) { 180 return getInt(property.getName()); 181 } 182 183 /** 184 * Gets a property as a String from the underlying map. 185 * If the property doesn't exist in the underlying map, an empty String is returned. 186 * 187 * @param propertyName 188 * the name of the property 189 * 190 * @return the String value of the property or an empty String if the property doesn't exist 191 */ 192 public String get(String propertyName) { 193 final String result = map.get(propertyName); 194 return result != null ? result : ""; 195 } 196 197 /** 198 * Gets a property as a {@code String} from the underlying map. 199 * If the property doesn't exist in the underlying map, an empty String is returned. 200 * 201 * @param property 202 * the property 203 * 204 * @return the String value of the property or an empty String if the property doesn't exist 205 */ 206 public String get(Property property) { 207 return get(property.getName()); 208 } 209 210 @Override 211 public String toString() { 212 return map.toString(); 213 } 214}