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.connection.serial;
013
014/**
015 * Helper class used to store serial connection parameters information.
016 * 
017 * <p>Parameters are stored as public variables so that they can be accessed
018 * and read from any class.</p>
019 */
020public final class SerialPortParameters {
021        
022        // Constants.
023        private static final int HASH_SEED = 23;
024        
025        // Variables.
026        public final int baudrate;
027        public final int dataBits;
028        public final int stopBits;
029        public final int parity;
030        public final int flowControl;
031        
032        /**
033         * Class constructor. Instances a new {@code SerialPortParameters} object
034         * with the given parameters.
035         * 
036         * @param baudrate Serial connection baud rate,
037         * @param dataBits Serial connection data bits.
038         * @param stopBits Serial connection stop bits.
039         * @param parity Serial connection parity.
040         * @param flowControl Serial connection flow control.
041         * 
042         * @throws IllegalArgumentException if {@code baudrate < 0} or
043         *                                  if {@code dataBits < 0} or
044         *                                  if {@code stopBits < 0} or
045         *                                  if {@code parity < 0} or
046         *                                  if {@code flowControl < 0}.
047         */
048        public SerialPortParameters(int baudrate, int dataBits, int stopBits, int parity, int flowControl) {
049                if (baudrate < 0)
050                        throw new IllegalArgumentException("Baudrate cannot be less than 0.");
051                if (dataBits < 0)
052                        throw new IllegalArgumentException("Number of data bits cannot be less than 0.");
053                if (stopBits < 0)
054                        throw new IllegalArgumentException("Number of stop bits cannot be less than 0.");
055                if (parity < 0)
056                        throw new IllegalArgumentException("Illegal parity value.");
057                if (flowControl < 0)
058                        throw new IllegalArgumentException("Illegal flow control value.");
059                
060                this.baudrate = baudrate;
061                this.dataBits = dataBits;
062                this.stopBits = stopBits;
063                this.parity = parity;
064                this.flowControl = flowControl;
065        }
066        
067        /*
068         * (non-Javadoc)
069         * @see java.lang.Object#equals(java.lang.Object)
070         */
071        @Override
072        public boolean equals(Object obj) {
073                if (obj instanceof SerialPortParameters)
074                        return ((SerialPortParameters)obj).baudrate == baudrate 
075                                && ((SerialPortParameters)obj).dataBits == dataBits 
076                                && ((SerialPortParameters)obj).stopBits == stopBits
077                                && ((SerialPortParameters)obj).parity == parity
078                                && ((SerialPortParameters)obj).flowControl == flowControl;
079                else
080                        return false;
081        }
082        
083        /*
084         * (non-Javadoc)
085         * @see java.lang.Object#hashCode()
086         */
087        @Override
088        public int hashCode() {
089                int hash = HASH_SEED;
090                hash = hash * (hash + baudrate);
091                hash = hash * (hash + dataBits);
092                hash = hash * (hash + stopBits);
093                hash = hash * (hash + parity);
094                hash = hash * (hash + flowControl);
095                return hash;
096        }
097        
098        /*
099         * (non-Javadoc)
100         * @see java.lang.Object#toString()
101         */
102        @Override
103        public String toString() {
104                return "Baud Rate: "+ baudrate + ", Data Bits: " + dataBits 
105                                + ", Stop Bits: " + stopBits + ", Parity: " + parity 
106                                + ", Flow Control: " + flowControl;
107        }
108}