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