From a749efcb7a75c57ab6a52e3293625c742c93c555 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Thu, 7 Feb 2013 14:22:56 -0500 Subject: [PATCH] tmf: Make CtfLocation extend TmfLocation We save one field, but more interestingly we don't need to duplicate the equals() and hashCode() methods. refs bug 387929 Change-Id: I4c23d6e80cc860470453963a2d559066a811fbc1 Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/10240 Tested-by: Hudson CI Reviewed-by: Patrick Tasse IP-Clean: Patrick Tasse --- .../tests/ctfadaptor/CtfLocationTest.java | 15 +-- .../tmf/core/tests/trace/TmfLocationTest.java | 6 +- .../tmf/core/ctfadaptor/CtfLocation.java | 99 +++++++------------ .../tmf/core/trace/TmfLocation.java | 25 +---- 4 files changed, 48 insertions(+), 97 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationTest.java index 1f88ad9b18..d3407d5907 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfLocationTest.java @@ -66,17 +66,6 @@ public class CtfLocationTest { assertEquals(new Long(0L), (Long)result.getLocationInfo().getTimestamp()); } - /** - * Run the CtfLocation clone() method test. - */ - @Test - public void testClone() { - CtfLocation result = fixture.clone(); - - assertNotNull(result); - assertEquals(Long.valueOf(1), (Long)result.getLocationInfo().getTimestamp()); - } - /** * Run the Long getLocation() method test. */ @@ -109,7 +98,7 @@ public class CtfLocationTest { @Test public void testToString_valid(){ CtfLocation fixture2 = new CtfLocation(new CtfLocationInfo(1337, 7331)); - assertEquals("CtfLocation: Element [1337/7331]",fixture2.toString()); //$NON-NLS-1$ + assertEquals("CtfLocation [fLocationInfo=Element [1337/7331]]", fixture2.toString()); //$NON-NLS-1$ } /** @@ -118,6 +107,6 @@ public class CtfLocationTest { @Test public void testToString_invalid(){ CtfLocation fixture2 = new CtfLocation(new CtfLocationInfo(-1, -1)); - assertEquals("CtfLocation: INVALID",fixture2.toString()); //$NON-NLS-1$ + assertEquals("CtfLocation [INVALID]", fixture2.toString()); //$NON-NLS-1$ } } diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java index f091cd9fc0..7306294668 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfLocationTest.java @@ -179,9 +179,9 @@ public class TmfLocationTest { TmfLongLocation location2 = new TmfLongLocation(lng); TmfTimestampLocation location3 = new TmfTimestampLocation(ts); - String expected1 = "TmfLocation [fLocation=" + str + "]"; - String expected2 = "TmfLocation [fLocation=" + lng + "]"; - String expected3 = "TmfLocation [fLocation=" + ts + "]"; + String expected1 = "TmfStringLocation [fLocationInfo=" + str + "]"; + String expected2 = "TmfLongLocation [fLocationInfo=" + lng + "]"; + String expected3 = "TmfTimestampLocation [fLocationInfo=" + ts + "]"; assertEquals("toString", expected1, location1.toString()); assertEquals("toString", expected2, location2.toString()); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java index 93e3b1013d..a41066b50a 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java @@ -6,12 +6,14 @@ * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: Matthew Khouzam - Initial API and implementation + * Contributors: + * Matthew Khouzam - Initial API and implementation + * Alexandre Montplaisir - Extends TmfLocation *******************************************************************************/ package org.eclipse.linuxtools.tmf.core.ctfadaptor; import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; -import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; +import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; /** * The nugget of information that is unique to a location in a CTF trace. @@ -21,17 +23,23 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; * @version 1.0 * @author Matthew Khouzam */ -public final class CtfLocation implements ITmfLocation { +public final class CtfLocation extends TmfLocation { - private final CtfLocationInfo fLocation; + // ------------------------------------------------------------------------ + // Attributes + // ------------------------------------------------------------------------ /** * An invalid location */ public static final CtfLocationInfo INVALID_LOCATION = new CtfLocationInfo(-1, -1); + // ------------------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------------------ + /** - * Constructor for CtfLocation. Uses a default index of 0. + * Basic constructor for CtfLocation. Uses a default index of 0. * * @param timestamp * The timestamp of this location @@ -41,7 +49,7 @@ public final class CtfLocation implements ITmfLocation { } /** - * Standard constructor + * Constructor using timestamp object and index * * @param timestamp * The timestamp of this location @@ -54,7 +62,7 @@ public final class CtfLocation implements ITmfLocation { } /** - * Change this location's timestamp and index values. + * Constructor using a long value for the timestamp, and an index * * @param timestampValue * The new timestamp @@ -63,84 +71,53 @@ public final class CtfLocation implements ITmfLocation { * @since 2.0 */ public CtfLocation(final long timestampValue, final long index) { - this(new CtfLocationInfo(timestampValue, index)); + super(new CtfLocationInfo(timestampValue, index)); } /** - * Copy constructor + * Constructor using a pre-made locationInfo object * - * @param location - * Other location to copy + * @param locationInfo + * The locationInfo object to use * @since 2.0 */ - public CtfLocation(final CtfLocationInfo location) { - fLocation = location; + public CtfLocation(CtfLocationInfo locationInfo) { + super(locationInfo); } /** - * Get the Location Data of this location + * Copy constructor * - * @return The CtfLocationData - * @see org.eclipse.linuxtools.tmf.core.trace.ITmfLocation#getLocationInfo() + * @param location + * Other location to copy * @since 2.0 */ - @Override - public CtfLocationInfo getLocationInfo() { - return fLocation; - } - - @Override - public CtfLocation clone() { - return new CtfLocation(new CtfLocationInfo(fLocation.getTimestamp(), fLocation.getIndex())); + public CtfLocation(final CtfLocation location) { + super(location); } + // ------------------------------------------------------------------------ + // TmfLocation + // ------------------------------------------------------------------------ - /* (non-Javadoc) - * @see java.lang.Object#hashCode() + /** + * @since 2.0 */ @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = (prime * result) - + ((fLocation == null) ? 0 : fLocation.hashCode()); - return result; + public CtfLocationInfo getLocationInfo() { + return (CtfLocationInfo) super.getLocationInfo(); } - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof CtfLocation)) { - return false; - } - CtfLocation other = (CtfLocation) obj; - if (fLocation == null) { - if (other.fLocation != null) { - return false; - } - } else if (!fLocation.equals(other.fLocation)) { - return false; - } - return true; - } + // ------------------------------------------------------------------------ + // Object + // ------------------------------------------------------------------------ - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ @Override public String toString() { if( this.getLocationInfo().equals(CtfLocation.INVALID_LOCATION )) { - return "CtfLocation: INVALID"; //$NON-NLS-1$ + return getClass().getSimpleName() + " [INVALID]"; //$NON-NLS-1$ } - return "CtfLocation: " + getLocationInfo().toString(); //$NON-NLS-1$ + return super.toString(); } } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfLocation.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfLocation.java index 363ceef022..e8647740a8 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfLocation.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfLocation.java @@ -33,18 +33,11 @@ public abstract class TmfLocation implements ITmfLocation { // Constructors // ------------------------------------------------------------------------ - /** - * Default constructor (for the 'null' location) - */ - @SuppressWarnings("unused") - private TmfLocation() { - fLocationInfo = null; - } - /** * Standard constructor. * - * @param locationInfo the concrete trace location + * @param locationInfo + * The concrete trace location */ public TmfLocation(final Comparable locationInfo) { fLocationInfo = locationInfo; @@ -53,7 +46,8 @@ public abstract class TmfLocation implements ITmfLocation { /** * Copy constructor * - * @param location the original trace location + * @param location + * The original trace location */ public TmfLocation(final TmfLocation location) { fLocationInfo = location.fLocationInfo; @@ -63,9 +57,6 @@ public abstract class TmfLocation implements ITmfLocation { // Getters // ------------------------------------------------------------------------ - /* (non-Javadoc) - * @see org.eclipse.linuxtools.tmf.core.trace.ITmfLocation#getLocationInfo() - */ /** * @since 2.0 */ @@ -78,9 +69,6 @@ public abstract class TmfLocation implements ITmfLocation { // Object // ------------------------------------------------------------------------ - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { final int prime = 31; @@ -89,9 +77,6 @@ public abstract class TmfLocation implements ITmfLocation { return result; } - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(final Object obj) { if (this == obj) { @@ -117,7 +102,7 @@ public abstract class TmfLocation implements ITmfLocation { @Override @SuppressWarnings("nls") public String toString() { - return "TmfLocation [fLocation=" + fLocationInfo + "]"; + return getClass().getSimpleName() + " [fLocationInfo=" + fLocationInfo.toString() + "]"; } } -- 2.34.1