tmf: Add SWTBot test for import from archive
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core / src / org / eclipse / linuxtools / pcap / core / protocol / ipv4 / IPv4Endpoint.java
CommitLineData
5255c030
VP
1/*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.pcap.core.protocol.ipv4;
14
15import java.util.Arrays;
16
17import org.eclipse.jdt.annotation.Nullable;
18import org.eclipse.linuxtools.pcap.core.endpoint.ProtocolEndpoint;
19import org.eclipse.linuxtools.pcap.core.util.ConversionHelper;
20
21/**
22 * Class that extends the {@link ProtocolEndpoint} class. It represents the
23 * endpoint at an IPv4 level.
24 *
25 * @author Vincent Perot
26 */
27public class IPv4Endpoint extends ProtocolEndpoint {
28
29 private final byte[] fIPAddress;
30
31 /**
32 * Constructor of the {@link IPv4Endpoint} class. It takes a packet to get
33 * its endpoint. Since every packet has two endpoints (source and
34 * destination), the isSourceEndpoint parameter is used to specify which
35 * endpoint to take.
36 *
37 * @param packet
38 * The packet that contains the endpoints.
39 * @param isSourceEndpoint
40 * Whether to take the source or the destination endpoint of the
41 * packet.
42 */
43 public IPv4Endpoint(IPv4Packet packet, boolean isSourceEndpoint) {
44 super(packet, isSourceEndpoint);
45 fIPAddress = isSourceEndpoint ? packet.getSourceIpAddress() : packet.getDestinationIpAddress();
46 }
47
48 @Override
49 public int hashCode() {
50 final int prime = 31;
51 int result = 1;
52
53 ProtocolEndpoint endpoint = getParentEndpoint();
54 if (endpoint == null) {
55 result = 0;
56 } else {
57 result = endpoint.hashCode();
58 }
59
60 result = prime * result + Arrays.hashCode(fIPAddress);
61 return result;
62 }
63
64 @Override
65 public boolean equals(@Nullable Object obj) {
66 if (this == obj) {
67 return true;
68 }
69 if (!(obj instanceof IPv4Endpoint)) {
70 return false;
71 }
72
73 IPv4Endpoint other = (IPv4Endpoint) obj;
74
75 // Check on layer
76 boolean localEquals = Arrays.equals(fIPAddress, other.fIPAddress);
77 if (!localEquals) {
78 return false;
79 }
80
81 // Check above layers.
82 ProtocolEndpoint endpoint = getParentEndpoint();
83 if (endpoint != null) {
84 return endpoint.equals(other.getParentEndpoint());
85 }
86 return true;
87 }
88
89 @Override
90 public String toString() {
91 ProtocolEndpoint endpoint = getParentEndpoint();
92 if (endpoint == null) {
93 return ConversionHelper.toIpAddress(fIPAddress);
94 }
95 return endpoint.toString() + '/' + ConversionHelper.toIpAddress(fIPAddress);
96 }
97
98}
This page took 0.027566 seconds and 5 git commands to generate.