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
018/**
019 * This class represents the hardware version number of an XBee device.
020 */
021public class HardwareVersion {
022        
023        // Constants.
024        private static final int HASH_SEED = 23;
025        
026        // Variables.
027        private final int value;
028        
029        private final String description;
030        
031        /**
032         * Class constructor. Instantiates a new {@code HardwareVersion} object 
033         * with the given parameters.
034         * 
035         * @param value The hardware version numeric value.
036         * @param description The hardware version description.
037         * 
038         * @throws IllegalArgumentException if {@code value < 0} or
039         *                                  if {@code description.length() < 1}.
040         * @throws NullPointerException if {@code description == null}.
041         */
042        private HardwareVersion(int value, String description) {
043                if (description == null)
044                        throw new NullPointerException("Description cannot be null.");
045                if (value < 0)
046                        throw new IllegalArgumentException("Value cannot be less than 0.");
047                if (description.length() < 1)
048                        throw new IllegalArgumentException("Description cannot be empty.");
049                
050                this.value = value;
051                this.description = description;
052        }
053        
054        /**
055         * Returns the Hardware version numeric value.
056         * 
057         * @return Hardware version numeric value.
058         */
059        public int getValue() {
060                return value;
061        }
062        
063        /**
064         * Returns the Hardware version description.
065         * 
066         * @return Hardware version description.
067         */
068        public String getDescription() {
069                return description;
070        }
071        
072        /**
073         * Returns the {@code HardwareVersion} object associated to the given 
074         * numeric value.
075         * 
076         * @param value Numeric value of the {@code HardwareVersion} retrieve.
077         * 
078         * @return The {@code HardwareVersion} associated to the given value.
079         */
080        public static HardwareVersion get(int value) {
081                HardwareVersionEnum hvEnum = HardwareVersionEnum.get(value);
082                if (hvEnum == null)
083                        return new HardwareVersion(value, "Unknown");
084                return new HardwareVersion(hvEnum.getValue(), hvEnum.getDescription());
085        }
086        
087        /**
088         * Returns the {@code HardwareVersion} object associated to the given 
089         * numeric value and description.
090         * 
091         * @param value Numeric value of the {@code HardwareVersion} retrieve.
092         * @param description Description of the {@code HardwareVersion} retrieve.
093         * 
094         * @return The {@code HardwareVersion} associated to the given value and 
095         *         description.
096         * 
097         * @throws IllegalArgumentException if {@code value < 0} or
098         *                                  if {@code description.length() < 1}.
099         * @throws NullPointerException if {@code description == null}.
100         */
101        public static HardwareVersion get(int value, String description) {
102                return new HardwareVersion(value, description);
103        }
104        
105        /*
106         * (non-Javadoc)
107         * @see java.lang.Object#equals(java.lang.Object)
108         */
109        @Override
110        public boolean equals(Object obj) {
111                if (!(obj instanceof HardwareVersion))
112                        return false;
113                HardwareVersion hwVersion = (HardwareVersion)obj;
114                if (hwVersion.getValue() == getValue() 
115                                && hwVersion.getDescription().equals(getDescription()))
116                        return true;
117                return false;
118        }
119        
120        /*
121         * (non-Javadoc)
122         * @see java.lang.Object#hashCode()
123         */
124        @Override
125        public int hashCode() {
126                int hash = HASH_SEED * (HASH_SEED + value);
127                return hash;
128        }
129        
130        /*
131         * (non-Javadoc)
132         * @see java.lang.Object#toString()
133         */
134        @Override
135        public String toString() {
136                return "" + value;
137        }
138}