View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model;
17  
18  import java.net.InetAddress;
19  import java.util.Arrays;
20  
21  /**
22   * IP address, port, and optional interface hardware address (MAC) of a network service.
23   *
24   * @author Christian Bauer
25   */
26  public class NetworkAddress {
27  
28      protected InetAddress address;
29      protected int port;
30      protected byte[] hardwareAddress;
31  
32      public NetworkAddress(InetAddress address, int port) {
33          this(address, port, null);
34      }
35  
36      public NetworkAddress(InetAddress address, int port, byte[] hardwareAddress) {
37          this.address = address;
38          this.port = port;
39          this.hardwareAddress = hardwareAddress;
40      }
41  
42      public InetAddress getAddress() {
43          return address;
44      }
45  
46      public int getPort() {
47          return port;
48      }
49  
50      public byte[] getHardwareAddress() {
51          return hardwareAddress;
52      }
53  
54      @Override
55      public boolean equals(Object o) {
56          if (this == o) return true;
57          if (o == null || getClass() != o.getClass()) return false;
58  
59          NetworkAddress that = (NetworkAddress) o;
60  
61          if (port != that.port) return false;
62          if (!address.equals(that.address)) return false;
63          if (!Arrays.equals(hardwareAddress, that.hardwareAddress)) return false;
64  
65          return true;
66      }
67  
68      @Override
69      public int hashCode() {
70          int result = address.hashCode();
71          result = 31 * result + port;
72          result = 31 * result + (hardwareAddress != null ? Arrays.hashCode(hardwareAddress) : 0);
73          return result;
74      }
75  }