|
| 1 | +/** |
| 2 | + * Copyright (c) 2016 Digi International Inc., |
| 3 | + * All rights not expressly granted are reserved. |
| 4 | + * |
| 5 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 7 | + * You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + * |
| 9 | + * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
| 10 | + * ======================================================================= |
| 11 | + */ |
| 12 | +package com.digi.xbee.api.models; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.regex.Matcher; |
| 16 | +import java.util.regex.Pattern; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * This class represents a 32-bit IP address used in some protocols where each |
| 21 | + * radio module has its own 32 bit IP address. |
| 22 | + * |
| 23 | + * <p>This address is only applicable for:</p> |
| 24 | + * <ul> |
| 25 | + * <li>Cellular</li> |
| 26 | + * </ul> |
| 27 | + */ |
| 28 | +public class IP32BitAddress { |
| 29 | + |
| 30 | + // Constants |
| 31 | + private static final String ERROR_IP_NULL = "IP address cannot be null."; |
| 32 | + private static final String ERROR_IP_TOO_LONG = "IP address cannot be longer than 4 bytes."; |
| 33 | + private static final String ERROR_IP_INVALID = "Invalid IP address."; |
| 34 | + |
| 35 | + private static final int HASH_SEED = 23; |
| 36 | + |
| 37 | + public final static String IP_V4_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." |
| 38 | + + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." |
| 39 | + + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." |
| 40 | + + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; |
| 41 | + |
| 42 | + // Variables |
| 43 | + private byte[] address; |
| 44 | + |
| 45 | + /** |
| 46 | + * Class constructor. Instantiates a new object of type |
| 47 | + * {@code IP32BitAddress} with the given parameters. |
| 48 | + * |
| 49 | + * @param address The 32 bit IP address as byte array. |
| 50 | + * |
| 51 | + * @throws IllegalArgumentException If {@code address.length > 4}. |
| 52 | + * @throws NullPointerException If {@code address == null}. |
| 53 | + */ |
| 54 | + public IP32BitAddress(byte[] address) { |
| 55 | + if (address == null) |
| 56 | + throw new NullPointerException(ERROR_IP_NULL); |
| 57 | + if (address.length > 4) |
| 58 | + throw new IllegalArgumentException(ERROR_IP_TOO_LONG); |
| 59 | + |
| 60 | + generateIPAddress(address); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Class constructor. Instantiates a new object of type |
| 65 | + * {@code IP32BitAddress} with the given parameters. |
| 66 | + * |
| 67 | + * @param address The 32 bit IP address as integer array. |
| 68 | + * |
| 69 | + * @throws IllegalArgumentException If {@code address.length > 8}. |
| 70 | + * @throws NullPointerException If {@code address == null}. |
| 71 | + */ |
| 72 | + public IP32BitAddress(int[] address) { |
| 73 | + if (address == null) |
| 74 | + throw new NullPointerException(ERROR_IP_NULL); |
| 75 | + if (address.length > 4) |
| 76 | + throw new IllegalArgumentException(ERROR_IP_TOO_LONG); |
| 77 | + |
| 78 | + byte[] byteAddress = new byte[address.length]; |
| 79 | + for (int i = 0; i < address.length; i++) |
| 80 | + byteAddress[i] = (byte)address[i]; |
| 81 | + |
| 82 | + generateIPAddress(byteAddress); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Class constructor. Instantiates a new object of type |
| 87 | + * {@code IP32BitAddress} with the given parameters. |
| 88 | + * |
| 89 | + * @param address The 32 bit IP address as string. |
| 90 | + * |
| 91 | + * @throws IllegalArgumentException If the given address doesn't match the |
| 92 | + * IP address pattern. |
| 93 | + * @throws NullPointerException If {@code address == null}. |
| 94 | + */ |
| 95 | + public IP32BitAddress(String address) { |
| 96 | + if (address == null) |
| 97 | + throw new NullPointerException(ERROR_IP_NULL); |
| 98 | + |
| 99 | + Pattern p = Pattern.compile(IP_V4_PATTERN); |
| 100 | + Matcher m = p.matcher(address); |
| 101 | + if (!m.matches()) |
| 102 | + throw new IllegalArgumentException(ERROR_IP_INVALID); |
| 103 | + |
| 104 | + byte[] byteAddress = new byte[4]; |
| 105 | + |
| 106 | + byteAddress[0] = (byte)Integer.parseInt(m.group(1)); |
| 107 | + byteAddress[1] = (byte)Integer.parseInt(m.group(2)); |
| 108 | + byteAddress[2] = (byte)Integer.parseInt(m.group(3)); |
| 109 | + byteAddress[3] = (byte)Integer.parseInt(m.group(4)); |
| 110 | + |
| 111 | + generateIPAddress(byteAddress); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Generates and saves the IP byte address based on the given byte array. |
| 116 | + * |
| 117 | + * @param byteAddress The byte array used to generate the final IP byte |
| 118 | + * address. |
| 119 | + */ |
| 120 | + private void generateIPAddress(byte[] byteAddress) { |
| 121 | + this.address = new byte[4]; |
| 122 | + |
| 123 | + int diff = 4 - byteAddress.length; |
| 124 | + for (int i = 0; i < diff; i++) |
| 125 | + this.address[i] = 0; |
| 126 | + for (int i = diff; i < 4; i++) |
| 127 | + this.address[i] = byteAddress[i - diff]; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Retrieves the 32 bit IP address value as byte array. |
| 132 | + * |
| 133 | + * @return 32 bit IP address value as byte array. |
| 134 | + */ |
| 135 | + public byte[] getValue() { |
| 136 | + return address; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Retrieves the 32 bit IP address value as string. |
| 141 | + * |
| 142 | + * @return 32 bit IP address value as string. |
| 143 | + */ |
| 144 | + public String getValueString() { |
| 145 | + String prettyIP = ""; |
| 146 | + for (int i = 0; i < address.length; i++) { |
| 147 | + if (i != 0) |
| 148 | + prettyIP += "."; |
| 149 | + prettyIP += address[i] & 0xFF; |
| 150 | + } |
| 151 | + |
| 152 | + return prettyIP; |
| 153 | + } |
| 154 | + |
| 155 | + /* |
| 156 | + * (non-Javadoc) |
| 157 | + * @see java.lang.Object#equals(java.lang.Object) |
| 158 | + */ |
| 159 | + @Override |
| 160 | + public boolean equals(Object obj) { |
| 161 | + if (obj == null) |
| 162 | + return false; |
| 163 | + if (!(obj instanceof IP32BitAddress)) |
| 164 | + return false; |
| 165 | + IP32BitAddress addr = (IP32BitAddress)obj; |
| 166 | + return Arrays.equals(addr.getValue(), getValue()); |
| 167 | + } |
| 168 | + |
| 169 | + /* |
| 170 | + * (non-Javadoc) |
| 171 | + * @see java.lang.Object#hashCode() |
| 172 | + */ |
| 173 | + @Override |
| 174 | + public int hashCode() { |
| 175 | + int hash = HASH_SEED; |
| 176 | + for (byte b:getValue()) |
| 177 | + hash = hash * (hash + b); |
| 178 | + return hash; |
| 179 | + } |
| 180 | + |
| 181 | + /* |
| 182 | + * (non-Javadoc) |
| 183 | + * @see java.lang.Object#toString() |
| 184 | + */ |
| 185 | + @Override |
| 186 | + public String toString() { |
| 187 | + return getValueString(); |
| 188 | + } |
| 189 | +} |
0 commit comments