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.Collections;
032import java.util.Map;
033
034/**
035 * A wrapper class around a {@link Map}.
036 * <p>
037 * We use wrapper classes instead of just passing around plain Maps because
038 * </p><ul>
039 * <li>it's more descriptive</li>
040 * <li>there's no confusion between types (e.g. {@code Client} and {@code Channel})</li>
041 * <li>we can create getters for the specific use-cases</li>
042 * <li>we don't have to check for {@code null} each time</li>
043 * </ul>
044 */
045public class Wrapper {
046
047        public static final Wrapper EMPTY = new Wrapper(Collections.<String, String>emptyMap());
048
049        private final Map<String, String> map;
050
051        /**
052         * Creates a new wrapper around the given map.
053         *
054         * @param map
055         *              the Map to abstract, cannot be {@code null}
056         */
057        public Wrapper(Map<String, String> map) {
058                this.map = map;
059        }
060
061        /**
062         * Gets the map underlying this {@code Wrapper}.
063         *
064         * @return the underlying map
065         */
066        public Map<String, String> getMap() {
067                return map;
068        }
069
070        /**
071         * Gets a property as a boolean from the underlying map.
072         * Returns {@code true} if the property exists in the map and its value is {@code "1"}.
073         *
074         * @param propertyName
075         *              the name of the property
076         *
077         * @return the boolean value of the property or {@code false} if the property doesn't exist
078         */
079        public boolean getBoolean(String propertyName) {
080                return getInt(propertyName) == 1;
081        }
082
083        /**
084         * Gets a property as a boolean from the underlying map.
085         * Returns {@code true} if the property exists in the map and its value is {@code "1"}.
086         *
087         * @param property
088         *              the property
089         *
090         * @return the boolean value of the property or {@code false} if the property doesn't exist
091         */
092        public boolean getBoolean(Property property) {
093                return getBoolean(property.getName());
094        }
095
096        /**
097         * Gets a property as a double from the underlying map.
098         * If the property doesn't exist in the underlying map, {@code -1.0} is returned.
099         *
100         * @param propertyName
101         *              the name of the property
102         *
103         * @return the double value of the property or {@code -1.0} if the property doesn't exist
104         */
105        public double getDouble(String propertyName) {
106                final String value = get(propertyName);
107                if (value == null || value.isEmpty()) {
108                        return -1D;
109                }
110                return Double.valueOf(value);
111        }
112
113        /**
114         * Gets a property as a double from the underlying map.
115         * If the property doesn't exist in the underlying map, {@code -1.0} is returned.
116         *
117         * @param property
118         *              the property
119         *
120         * @return the double value of the property or {@code -1.0} if the property doesn't exist
121         */
122        public double getDouble(Property property) {
123                return getDouble(property.getName());
124        }
125
126        /**
127         * Gets a property as a long from the underlying map.
128         * If the property doesn't exist in the underlying map, {@code -1} is returned.
129         *
130         * @param propertyName
131         *              the name of the property
132         *
133         * @return the long value of the property or {@code -1} if the property doesn't exist
134         */
135        public long getLong(String propertyName) {
136                final String value = get(propertyName);
137                if (value == null || value.isEmpty()) {
138                        return -1L;
139                }
140                return Long.parseLong(value);
141        }
142
143        /**
144         * Gets a property as a long from the underlying map.
145         * If the property doesn't exist in the underlying map, {@code -1} is returned.
146         *
147         * @param property
148         *              the property
149         *
150         * @return the long value of the property or {@code -1} if the property doesn't exist
151         */
152        public long getLong(Property property) {
153                return getLong(property.getName());
154        }
155
156        /**
157         * Gets a property as an integer from the underlying map.
158         * If the property doesn't exist in the underlying map, {@code -1} is returned.
159         *
160         * @param propertyName
161         *              the name of the property
162         *
163         * @return the integer value of the property or {@code -1} if the property doesn't exist
164         */
165        public int getInt(String propertyName) {
166                final String value = get(propertyName);
167                if (value == null || value.isEmpty()) {
168                        return -1;
169                }
170                return Integer.parseInt(value);
171        }
172
173        /**
174         * Gets a property as an integer from the underlying map.
175         * If the property doesn't exist in the underlying map, {@code -1} is returned.
176         *
177         * @param property
178         *              the property
179         *
180         * @return the integer value of the property or {@code -1} if the property doesn't exist
181         */
182        public int getInt(Property property) {
183                return getInt(property.getName());
184        }
185
186        /**
187         * Gets a property as a String from the underlying map.
188         * If the property doesn't exist in the underlying map, an empty String is returned.
189         *
190         * @param propertyName
191         *              the name of the property
192         *
193         * @return the String value of the property or an empty String if the property doesn't exist
194         */
195        public String get(String propertyName) {
196                final String result = map.get(propertyName);
197                return result != null ? result : "";
198        }
199
200        /**
201         * Gets a property as a {@code String} from the underlying map.
202         * If the property doesn't exist in the underlying map, an empty String is returned.
203         *
204         * @param property
205         *              the property
206         *
207         * @return the String value of the property or an empty String if the property doesn't exist
208         */
209        public String get(Property property) {
210                return get(property.getName());
211        }
212
213        @Override
214        public String toString() {
215                return map.toString();
216        }
217}