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
020/**
021 * Enumerates all the possible states of the discovery. Discovery status field 
022 * is part of the {@code TransmitStatusPacket} indicating the status of the 
023 * discovery when a packet is sent.
024 * 
025 * @see com.digi.xbee.api.packet.common.TransmitStatusPacket
026 */
027public enum XBeeDiscoveryStatus {
028
029        // Enumeration elements
030        DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD (0x00, "No discovery overhead"),
031        DISCOVERY_STATUS_ADDRESS_DISCOVERY (0x01, "Address discovery"),
032        DISCOVERY_STATUS_ROUTE_DISCOVERY (0x02, "Route discovery"),
033        DISCOVERY_STATUS_ADDRESS_AND_ROUTE (0x03, "Address and route"),
034        DISCOVERY_STATUS_EXTENDED_TIMEOUT_DISCOVERY (0x40, "Extended timeout discovery"),
035        DISCOVERY_STATUS_UNKNOWN (0xFF, "Unknown");
036        
037        // Variables
038        private final int id;
039        
040        private final String description;
041        
042        private static final HashMap<Integer, XBeeDiscoveryStatus> lookupTable = new HashMap<Integer, XBeeDiscoveryStatus>();
043        
044        static {
045                for (XBeeDiscoveryStatus at:values())
046                        lookupTable.put(at.getId(), at);
047        }
048        
049        /**
050         * Class constructor. Instantiates a new enumeration element of type 
051         * {@code XBeeDiscoveryStatus} with the given parameters.
052         * 
053         * @param id Discovery status ID.
054         * @param description Discovery status description.
055         */
056        private XBeeDiscoveryStatus(int id, String description) {
057                this.id = id;
058                this.description = description;
059        }
060        
061        /**
062         * Returns the numeric value of the discovery status identifier.
063         * 
064         * @return The discovery status identifier.
065         */
066        public int getId() {
067                return id;
068        }
069        
070        /**
071         * Returns the discovery status description.
072         * 
073         * @return Discovery status description.
074         */
075        public String getDescription() {
076                return description;
077        }
078        
079        /**
080         * Returns the {@code XBeeDiscoveryStatus} associated to the given 
081         * identifier.
082         * 
083         * Returns {@code DISCOVERY_STATUS_UNKNOWN} if the identifier is not in the 
084         * enumeration.
085         * 
086         * @param id Identifier of the {@code XBeeDiscoveryStatus} to retrieve.
087         * 
088         * @return The {@code XBeeDiscoveryStatus} associated with the given 
089         *         identifier.
090         *         {@code DISCOVERY_STATUS_UNKNOWN} if the identifier is not in the 
091         *         enumeration.
092         */
093        public static XBeeDiscoveryStatus get(int id) {
094                XBeeDiscoveryStatus status = lookupTable.get(id);
095                if (status != null)
096                        return status;
097                return DISCOVERY_STATUS_UNKNOWN;
098        }
099        
100        /*
101         * (non-Javadoc)
102         * @see java.lang.Enum#toString()
103         */
104        @Override
105        public String toString() {
106                return String.format("%s (0x%02X)", description, id);
107        }
108}