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 the different IP protocols.
022 * 
023 * @since 1.2.0
024 */
025public enum IPProtocol {
026
027        // Enumeration types.
028        UDP(0, "UDP"),
029        TCP(1, "TCP"),
030        COAP(3, "CoAP"),
031        TCP_SSL(4, "TCP SSL");
032
033        // Variables.
034        private int id;
035
036        private String name;
037
038        private static HashMap<Integer, IPProtocol> lookupTable = new HashMap<Integer, IPProtocol>();
039
040        static {
041                for (IPProtocol protocol:values())
042                        lookupTable.put(protocol.getID(), protocol);
043        }
044
045        /**
046         * Class constructor. Instantiates a new {@code IPProtocol} enumeration
047         * entry with the given parameters.
048         *
049         * @param id IP protocol ID.
050         * @param name IP protocol name.
051         */
052        private IPProtocol(int id, String name) {
053                this.id = id;
054                this.name = name;
055        }
056
057        /**
058         * Retrieves the IP protocol ID.
059         *
060         * @return IP protocol ID.
061         */
062        public int getID() {
063                return id;
064        }
065
066        /**
067         * Retrieves the IP protocol name.
068         *
069         * @return IP protocol name.
070         */
071        public String getName() {
072                return name;
073        }
074
075        /**
076         * Retrieves the IP protocol for the given ID.
077         *
078         * @param id ID to retrieve the IP protocol.
079         *
080         * @return The IP protocol associated with the given ID.
081         */
082        public static IPProtocol get(int id) {
083                return lookupTable.get(id);
084        }
085
086        /*
087         * (non-Javadoc)
088         * @see java.lang.Enum#toString()
089         */
090        @Override
091        public String toString() {
092                return name;
093        }
094}