001/**
002 * Copyright 2017, Digi International Inc.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015 */
016package com.digi.xbee.api.models;
017
018import java.util.HashMap;
019
020import com.digi.xbee.api.utils.HexUtils;
021
022/**
023 * Enumerates the different power levels. The power level indicates the output 
024 * power value of a radio when transmitting data.
025 */
026public enum PowerLevel {
027
028        // Enumeration entries
029        LEVEL_LOWEST(0x00, "Lowest"),
030        LEVEL_LOW(0x01, "Low"),
031        LEVEL_MEDIUM(0x02, "Medium"),
032        LEVEL_HIGH(0x03, "High"),
033        LEVEL_HIGHEST(0x04, "Highest"),
034        LEVEL_UNKNOWN(0xFF, "Unknown");
035        
036        // Variables
037        private final int value;
038        
039        private final String description;
040        
041        private final static HashMap<Integer, PowerLevel> lookupTable = new HashMap<Integer, PowerLevel>();
042        
043        static {
044                for (PowerLevel powerLevel:values())
045                        lookupTable.put(powerLevel.getValue(), powerLevel);
046        }
047        
048        /**
049         * Class constructor. Instantiates a new {@code PowerLevel} enumeration
050         * entry with the given parameters.
051         * 
052         * @param value Power level value 
053         * @param description Power level description.
054         */
055        private PowerLevel(int value, String description) {
056                this.value = value;
057                this.description = description;
058        }
059        
060        /**
061         * Returns the power level value.
062         * 
063         * @return The power level value.
064         */
065        public int getValue() {
066                return value;
067        }
068        
069        /**
070         * Returns the power level description.
071         * 
072         * @return The power level description.
073         */
074        public String getDescription() {
075                return description;
076        }
077        
078        /**
079         * Returns the {@code PowerLevel} entry associated to the given value.
080         * 
081         * @param value Value of the {@code PowerLevel} to retrieve.
082         * 
083         * @return The {@code PowerLevel} entry associated to the given value, 
084         *         {@code #LEVEL_UNKNOWN} if the value could not be found in the 
085         *         list.
086         */
087        public static PowerLevel get(int value) {
088                PowerLevel powerLevel = lookupTable.get(value);
089                if (powerLevel != null)
090                        return powerLevel;
091                return LEVEL_UNKNOWN;
092        }
093        
094        /*
095         * (non-Javadoc)
096         * @see java.lang.Enum#toString()
097         */
098        @Override
099        public String toString() {
100                return HexUtils.byteToHexString((byte)value) + ": " + description;
101        }
102}