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.regex.Pattern;
019
020/**
021 * This class represents an SMS message containing the phone number that sent  
022 * the message and the content (data) of the message. 
023 * 
024 * <p>This class is used within the XBee Java Library to read SMS sent to 
025 * Cellular devices.</p>
026 * 
027 * @since 1.2.0
028 */
029public class SMSMessage {
030
031        // Constants.
032        private static final String PHONE_NUMBER_PATTERN = "^\\+?\\d+$";
033        
034        // Variables.
035        private final String phoneNumber;
036        private final String data;
037        
038        /**
039         * Class constructor. Instantiates a new object of type 
040         * {@code SMSMessage} with the given parameters.
041         * 
042         * @param phoneNumber The phone number that sent the message.
043         * @param data String containing the message text.
044         * 
045         * @throws IllegalArgumentException if {@code phoneNumber} is invalid.
046         * @throws NullPointerException if {@code phoneNumber == null} or
047         *                              if {@code data == null}.
048         */
049        public SMSMessage(String phoneNumber, String data) {
050                if (phoneNumber == null)
051                        throw new NullPointerException("Phone number cannot be null.");
052                if (data == null)
053                        throw new NullPointerException("Data cannot be null.");
054                if (!Pattern.matches(PHONE_NUMBER_PATTERN, phoneNumber))
055                        throw new IllegalArgumentException("Invalid phone number.");
056                
057                this.phoneNumber = phoneNumber;
058                this.data = data;
059        }
060        
061        /**
062         * Returns the phone number that sent the message.
063         * 
064         * @return The phone number that sent the message.
065         */
066        public String getPhoneNumber() {
067                return phoneNumber;
068        }
069        
070        /**
071         * Returns a string containing the data of the message.
072         * 
073         * @return A string containing the data of the message.
074         */
075        public String getData() {
076                return data;
077        }
078}