tmf: reduce complexity of equals() methods in tmf.core
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / event / lookup / TmfCallsite.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.event.lookup;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.equalsNullable;
16
17 /**
18 * TMF call site information for source code lookup.
19 *
20 * @since 2.0
21 * @author Bernd Hufmann
22 */
23 public class TmfCallsite implements ITmfCallsite {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 /** The file name string. */
30 final private String fFileName;
31
32 /** The function name. */
33 final private String fFunctionName;
34
35 /** The line number. */
36 final private long fLineNumber;
37
38 // ------------------------------------------------------------------------
39 // Constructors
40 // ------------------------------------------------------------------------
41
42 /**
43 * Default constructor.
44 *
45 * @param fileName
46 * - a file name
47 * @param functionName
48 * - a function name
49 * @param lineNumber
50 * - a line number
51 */
52 public TmfCallsite(String fileName, String functionName, long lineNumber) {
53 if (fileName == null) {
54 throw new IllegalArgumentException();
55 }
56 fFileName = fileName;
57 fFunctionName = functionName;
58 fLineNumber = lineNumber;
59 }
60
61 /**
62 * Copy Constructor.
63 *
64 * @param other
65 * - An other call site implementation
66 */
67 public TmfCallsite(ITmfCallsite other) {
68 if ((other == null) || (other.getFileName() == null)) {
69 throw new IllegalArgumentException();
70 }
71 fFileName = other.getFileName();
72 fFunctionName = other.getFunctionName();
73 fLineNumber = other.getLineNumber();
74 }
75
76 // ------------------------------------------------------------------------
77 // Accessors
78 // ------------------------------------------------------------------------
79
80 @Override
81 public String getFileName() {
82 return fFileName;
83 }
84
85 @Override
86 public String getFunctionName() {
87 return fFunctionName;
88 }
89
90 @Override
91 public long getLineNumber() {
92 return fLineNumber;
93 }
94
95 // ------------------------------------------------------------------------
96 // Operations
97 // ------------------------------------------------------------------------
98
99 @Override
100 public int hashCode() {
101 final int prime = 31;
102 int result = 1;
103 // fFileName cannot be null!
104 result = prime * result + fFileName.hashCode();
105 result = prime * result + ((fFunctionName == null) ? 0 : fFunctionName.hashCode());
106 result = prime * result + (int) (fLineNumber ^ (fLineNumber >>> 32));
107 return result;
108 }
109
110 @Override
111 public boolean equals(Object obj) {
112 if (this == obj) {
113 return true;
114 }
115 if (obj == null) {
116 return false;
117 }
118 if (getClass() != obj.getClass()) {
119 return false;
120 }
121 TmfCallsite other = (TmfCallsite) obj;
122
123 // fFileName cannot be null!
124 if (!fFileName.equals(other.fFileName)) {
125 return false;
126 }
127
128 if (!equalsNullable(fFunctionName, other.fFunctionName)) {
129 return false;
130 }
131 if (fLineNumber != other.fLineNumber) {
132 return false;
133 }
134 return true;
135 }
136
137 @Override
138 public String toString() {
139 StringBuilder builder = new StringBuilder();
140 builder.append(fFileName).append(':');
141 builder.append(Long.toString(fLineNumber));
142 if (fFunctionName != null) {
143 builder.append(' ');
144 builder.append(fFunctionName).append("()"); //$NON-NLS-1$
145 }
146 return builder.toString();
147 }
148 }
This page took 0.039075 seconds and 6 git commands to generate.