pcap: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.pcap.core / src / org / eclipse / tracecompass / internal / pcap / core / protocol / ethernet2 / EthernetIIEndpoint.java
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
13 package org.eclipse.tracecompass.internal.pcap.core.protocol.ethernet2;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.eclipse.tracecompass.internal.pcap.core.endpoint.ProtocolEndpoint;
19 import org.eclipse.tracecompass.internal.pcap.core.util.ConversionHelper;
20
21 /**
22 * Class that extends the {@link ProtocolEndpoint} class. It represents the
23 * endpoint at an Ethernet II level.
24 *
25 * @author Vincent Perot
26 */
27 public class EthernetIIEndpoint extends ProtocolEndpoint {
28
29 private final byte[] fMacAddress;
30
31 /**
32 * Constructor of the {@link EthernetIIEndpoint} class. It takes a packet to
33 * get 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 EthernetIIEndpoint(EthernetIIPacket packet, boolean isSourceEndpoint) {
44 super(packet, isSourceEndpoint);
45 fMacAddress = isSourceEndpoint ? packet.getSourceMacAddress() : packet.getDestinationMacAddress();
46 }
47
48 @Override
49 public int hashCode() {
50 final int prime = 31;
51 int result = 1;
52 ProtocolEndpoint endpoint = getParentEndpoint();
53 if (endpoint == null) {
54 result = 0;
55 } else {
56 result = endpoint.hashCode();
57 }
58 result = prime * result + Arrays.hashCode(fMacAddress);
59 return result;
60 }
61
62 @Override
63 public boolean equals(@Nullable Object obj) {
64 if (this == obj) {
65 return true;
66 }
67 if (!(obj instanceof EthernetIIEndpoint)) {
68 return false;
69 }
70
71 EthernetIIEndpoint other = (EthernetIIEndpoint) obj;
72
73 // Check on layer
74 boolean localEquals = Arrays.equals(fMacAddress, other.fMacAddress);
75 if (!localEquals) {
76 return false;
77 }
78
79 // Check above layers.
80 ProtocolEndpoint endpoint = getParentEndpoint();
81 if (endpoint != null) {
82 return endpoint.equals(other.getParentEndpoint());
83 }
84 return true;
85 }
86
87 @Override
88 public String toString() {
89 ProtocolEndpoint endpoint = getParentEndpoint();
90 if (endpoint == null) {
91 return ConversionHelper.toMacAddress(fMacAddress);
92 }
93 return (endpoint.toString().equals(EMPTY_STRING) ?
94 ConversionHelper.toMacAddress(fMacAddress) :
95 endpoint.toString() + '/' + ConversionHelper.toMacAddress(fMacAddress));
96
97 }
98 }
This page took 0.034507 seconds and 6 git commands to generate.