001/**
002 * Copyright (c) 2014-2015 Digi International Inc.,
003 * All rights not expressly granted are reserved.
004 *
005 * This Source Code Form is subject to the terms of the Mozilla Public
006 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
007 * You can obtain one at http://mozilla.org/MPL/2.0/.
008 *
009 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
010 * =======================================================================
011 */
012package com.digi.xbee.api.models;
013
014import java.util.HashMap;
015
016import com.digi.xbee.api.utils.HexUtils;
017
018/**
019 * Enumerates the different power levels. The power level indicates the output 
020 * power value of a radio when transmitting data.
021 */
022public enum PowerLevel {
023
024        // Enumeration entries
025        LEVEL_LOWEST(0x00, "Lowest"),
026        LEVEL_LOW(0x01, "Low"),
027        LEVEL_MEDIUM(0x02, "Medium"),
028        LEVEL_HIGH(0x03, "High"),
029        LEVEL_HIGHEST(0x04, "Highest"),
030        LEVEL_UNKNOWN(0xFF, "Unknown");
031        
032        // Variables
033        private final int value;
034        
035        private final String description;
036        
037        private final static HashMap<Integer, PowerLevel> lookupTable = new HashMap<Integer, PowerLevel>();
038        
039        static {
040                for (PowerLevel powerLevel:values())
041                        lookupTable.put(powerLevel.getValue(), powerLevel);
042        }
043        
044        /**
045         * Class constructor. Instantiates a new {@code PowerLevel} enumeration
046         * entry with the given parameters.
047         * 
048         * @param value Power level value 
049         * @param description Power level description.
050         */
051        private PowerLevel(int value, String description) {
052                this.value = value;
053                this.description = description;
054        }
055        
056        /**
057         * Returns the power level value.
058         * 
059         * @return The power level value.
060         */
061        public int getValue() {
062                return value;
063        }
064        
065        /**
066         * Returns the power level description.
067         * 
068         * @return The power level description.
069         */
070        public String getDescription() {
071                return description;
072        }
073        
074        /**
075         * Returns the {@code PowerLevel} entry associated to the given value.
076         * 
077         * @param value Value of the {@code PowerLevel} to retrieve.
078         * 
079         * @return The {@code PowerLevel} entry associated to the given value, 
080         *         {@code #LEVEL_UNKNOWN} if the value could not be found in the 
081         *         list.
082         */
083        public static PowerLevel get(int value) {
084                PowerLevel powerLevel = lookupTable.get(value);
085                if (powerLevel != null)
086                        return powerLevel;
087                return LEVEL_UNKNOWN;
088        }
089        
090        /*
091         * (non-Javadoc)
092         * @see java.lang.Enum#toString()
093         */
094        @Override
095        public String toString() {
096                return HexUtils.byteToHexString((byte)value) + ": " + description;
097        }
098}