001/*
002 * Copyright 2019, 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
018/**
019 * This class represents a User Data Relay message containing the source
020 * interface and the content (data) of the message.
021 *
022 * @since 1.3.0
023 */
024public class UserDataRelayMessage {
025
026        // Variables.
027        private final XBeeLocalInterface localInterface;
028        private final byte[] data;
029
030        /**
031         * Class constructor. Instantiates a new object of type
032         * {@code UserDataRelayMessage} with the given parameters.
033         *
034         * @param localInterface The source XBee local interface.
035         * @param data Byte array containing the data of the message.
036         *
037         * @throws NullPointerException if {@code localInterface == null}.
038         *
039         * @see XBeeLocalInterface
040         */
041        public UserDataRelayMessage(XBeeLocalInterface localInterface, byte[] data) {
042                if (localInterface == null)
043                        throw new NullPointerException("XBee local interface cannot be null.");
044
045                this.localInterface = localInterface;
046                this.data = data;
047        }
048
049        /**
050         * Returns the source interface that sent this message.
051         *
052         * @return The source interface that sent this message.
053         *
054         * @see XBeeLocalInterface
055         */
056        public XBeeLocalInterface getSourceInterface() {
057                return localInterface;
058        }
059        
060        /**
061         * Returns a byte array containing the data of the message.
062         *
063         * @return A byte array containing the data of the message or {@code null}
064         *         if the message does not have any data.
065         */
066        public byte[] getData() {
067                return data;
068        }
069        
070        /**
071         * Returns the data of the message in string format.
072         *
073         * @return The data of the message in string format or {@code null} if the
074         *         message does not have any data.
075         */
076        public String getDataString() {
077                return data == null ? null : new String(data);
078        }
079}