From 5d837f9b6f66abaa47bf269f82bf6e937e01d40b Mon Sep 17 00:00:00 2001 From: Francois Chouinard Date: Fri, 13 Apr 2012 16:13:19 -0400 Subject: [PATCH] Refactor TmfCheckpoint and dependencies --- .../lttng/core/event/LttngLocation.java | 307 +++++++++--------- .../lttng/core/event/LttngTimestamp.java | 191 ++++++----- .../core/tests/trace/TmfCheckpointTest.java | 276 +++++++++------- .../tmf/core/tests/trace/TmfContextTest.java | 20 ++ .../tmf/core/trace/ITmfCheckpoint.java | 60 ++++ .../tmf/core/trace/TmfCheckpoint.java | 116 ++++--- .../linuxtools/tmf/core/trace/TmfContext.java | 23 +- .../tmf/core/trace/TmfLocation.java | 28 +- 8 files changed, 584 insertions(+), 437 deletions(-) create mode 100644 org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfCheckpoint.java diff --git a/org.eclipse.linuxtools.lttng.core/src/org/eclipse/linuxtools/internal/lttng/core/event/LttngLocation.java b/org.eclipse.linuxtools.lttng.core/src/org/eclipse/linuxtools/internal/lttng/core/event/LttngLocation.java index c1865ac44f..4d693bfc42 100644 --- a/org.eclipse.linuxtools.lttng.core/src/org/eclipse/linuxtools/internal/lttng/core/event/LttngLocation.java +++ b/org.eclipse.linuxtools.lttng.core/src/org/eclipse/linuxtools/internal/lttng/core/event/LttngLocation.java @@ -4,126 +4,126 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; public class LttngLocation implements ITmfLocation, Comparable { - - private final static long DEFAULT_CURR_TIME = 0L; - - private boolean isLastOperationParse = false ; - private boolean isLastOperationReadNext = false; - private boolean isLastOperationSeek = false; - - private LttngTimestamp operationTime = null; - - public LttngLocation() { - this( DEFAULT_CURR_TIME ); - } - - public LttngLocation(long newCurrentTimestampValue) { - isLastOperationParse = false; - isLastOperationReadNext = false; - isLastOperationSeek = false; - operationTime = new LttngTimestamp(newCurrentTimestampValue); - } - - public LttngLocation(LttngTimestamp newCurrentTimestamp) { - isLastOperationParse = false; - isLastOperationReadNext = false; - isLastOperationSeek = false; - operationTime = new LttngTimestamp(newCurrentTimestamp); - } - - - public LttngLocation(LttngLocation oldLocation) { - this.isLastOperationParse = oldLocation.isLastOperationParse; - this.isLastOperationReadNext = oldLocation.isLastOperationReadNext; - this.isLastOperationSeek = oldLocation.isLastOperationSeek; - this.operationTime = oldLocation.operationTime; - } - - @Override - public LttngLocation clone() { - - LttngLocation newLocation = null; - - try { - newLocation = (LttngLocation)super.clone(); - - // *** IMPORTANT *** - // Basic type in java are immutable! - // Thus, using assignation ("=") on basic type is VALID. - newLocation.isLastOperationParse = this.isLastOperationParse; - newLocation.isLastOperationReadNext = this.isLastOperationReadNext; - newLocation.isLastOperationSeek = this.isLastOperationSeek; - - // For other type, we need to create a new timestamp - newLocation.operationTime = new LttngTimestamp( this.operationTime ); - } - catch (CloneNotSupportedException e) { - System.out.println("Cloning failed with : " + e.getMessage()); //$NON-NLS-1$ - } - - return newLocation; - } - - public LttngTimestamp getOperationTime() { - return operationTime; - } - - public long getOperationTimeValue() { - return operationTime.getValue(); - } - - public void setOperationTime(LttngTimestamp newOperationTime) { - this.operationTime.setValue(newOperationTime.getValue()); - } - - public void setOperationTime(Long newOperationTimeValue) { - this.operationTime.setValue(newOperationTimeValue); - } - - - public void setLastOperationParse() { - isLastOperationParse = true; - isLastOperationReadNext = false; - isLastOperationSeek = false; - } - - public boolean isLastOperationParse() { - return isLastOperationParse; - } - - - public void setLastOperationReadNext() { - isLastOperationParse = false; - isLastOperationReadNext = true; - isLastOperationSeek = false; - } - - public boolean isLastOperationReadNext() { - return isLastOperationReadNext; - } - - - public void setLastOperationSeek() { - isLastOperationParse = false; - isLastOperationReadNext = false; - isLastOperationSeek = true; - } - - public boolean isLastOperationSeek() { - return isLastOperationSeek; - } - - public void resetLocationState() { - isLastOperationParse = false; - isLastOperationReadNext = false; - isLastOperationSeek = false; - } - - // ------------------------------------------------------------------------ - // Object - // ------------------------------------------------------------------------ - - /* (non-Javadoc) + + private final static long DEFAULT_CURR_TIME = 0L; + + private boolean isLastOperationParse = false ; + private boolean isLastOperationReadNext = false; + private boolean isLastOperationSeek = false; + + private LttngTimestamp operationTime = null; + + public LttngLocation() { + this( DEFAULT_CURR_TIME ); + } + + public LttngLocation(final long newCurrentTimestampValue) { + isLastOperationParse = false; + isLastOperationReadNext = false; + isLastOperationSeek = false; + operationTime = new LttngTimestamp(newCurrentTimestampValue); + } + + public LttngLocation(final LttngTimestamp newCurrentTimestamp) { + isLastOperationParse = false; + isLastOperationReadNext = false; + isLastOperationSeek = false; + operationTime = new LttngTimestamp(newCurrentTimestamp); + } + + + public LttngLocation(final LttngLocation oldLocation) { + this.isLastOperationParse = oldLocation.isLastOperationParse; + this.isLastOperationReadNext = oldLocation.isLastOperationReadNext; + this.isLastOperationSeek = oldLocation.isLastOperationSeek; + this.operationTime = oldLocation.operationTime; + } + + @Override + public LttngLocation clone() { + + LttngLocation newLocation = null; + + try { + newLocation = (LttngLocation)super.clone(); + + // *** IMPORTANT *** + // Basic type in java are immutable! + // Thus, using assignation ("=") on basic type is VALID. + newLocation.isLastOperationParse = this.isLastOperationParse; + newLocation.isLastOperationReadNext = this.isLastOperationReadNext; + newLocation.isLastOperationSeek = this.isLastOperationSeek; + + // For other type, we need to create a new timestamp + newLocation.operationTime = new LttngTimestamp( this.operationTime ); + } + catch (final CloneNotSupportedException e) { + System.out.println("Cloning failed with : " + e.getMessage()); //$NON-NLS-1$ + } + + return newLocation; + } + + public LttngTimestamp getOperationTime() { + return operationTime; + } + + public long getOperationTimeValue() { + return operationTime.getValue(); + } + + public void setOperationTime(final LttngTimestamp newOperationTime) { + this.operationTime.setValue(newOperationTime.getValue()); + } + + public void setOperationTime(final Long newOperationTimeValue) { + this.operationTime.setValue(newOperationTimeValue); + } + + + public void setLastOperationParse() { + isLastOperationParse = true; + isLastOperationReadNext = false; + isLastOperationSeek = false; + } + + public boolean isLastOperationParse() { + return isLastOperationParse; + } + + + public void setLastOperationReadNext() { + isLastOperationParse = false; + isLastOperationReadNext = true; + isLastOperationSeek = false; + } + + public boolean isLastOperationReadNext() { + return isLastOperationReadNext; + } + + + public void setLastOperationSeek() { + isLastOperationParse = false; + isLastOperationReadNext = false; + isLastOperationSeek = true; + } + + public boolean isLastOperationSeek() { + return isLastOperationSeek; + } + + public void resetLocationState() { + isLastOperationParse = false; + isLastOperationReadNext = false; + isLastOperationSeek = false; + } + + // ------------------------------------------------------------------------ + // Object + // ------------------------------------------------------------------------ + + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override @@ -141,46 +141,43 @@ public class LttngLocation implements ITmfLocation, Comparable * */ - public LttngTimestamp() { - this(Long.MIN_VALUE); - } - - /** + public LttngTimestamp() { + this(Long.MIN_VALUE); + } + + /** * Constructor with parameters.

* * @param newEventTime Time as long, unit expected to be nanoseconds */ - public LttngTimestamp(long newEventTime) { + public LttngTimestamp(final long newEventTime) { super(newEventTime, -9, 0); } - + /** * Copy Constructor.

* * @param oldEventTime The timestamp object we want to copy from */ - public LttngTimestamp(ITmfTimestamp oldEventTime) { + public LttngTimestamp(final ITmfTimestamp oldEventTime) { this(oldEventTime.getValue()); } - - public void setValue(long newValue) { + + public void setValue(final long newValue) { fValue = newValue; } - - /** - * Get the second part in timestamp.

- * - * Note : We do not use scale and assumes contents to be in nano seconds. - * - * @return Seconds in the object, in string. - */ - public String getSeconds() { - return formatSecs(fValue); - } - - /** + + /** + * Get the second part in timestamp.

+ * + * Note : We do not use scale and assumes contents to be in nano seconds. + * + * @return Seconds in the object, in string. + */ + public String getSeconds() { + return formatSecs(fValue); + } + + /** * Get the nanosecond part in timestamp.

* * Note : We do not use scale and assumes contents to be in nanoseconds. - * - * @return Seconds in the object, in string. - */ - public String getNanoSeconds() { - return formatNs(fValue); - } - - /* - * Use the exponent to format the second in the correct format. - */ - private String formatSecs(long time) { - long sec = (long) (time * 1E-9); - return String.valueOf(sec); - } - - /* - * Obtains the remainder fraction on unit Seconds of the entered value in - * nanoseconds. e.g. input: 1241207054171080214 ns. - * The number of fraction seconds can be obtained by removing the last 9 digits: - * In 1241207054, the fractional portion of seconds, expressed in ns is: 171080214 - */ - private String formatNs(long time) { - boolean neg = time < 0; - if (neg) { - time = -time; - } - // The following approach could be used although performance - // decreases in half. - // String strVal = String.format("%09d", time); - // String tmp = strVal.substring(strVal.length() - 9) - StringBuffer temp = new StringBuffer(); - long ns = time; - ns %= 1000000000; - if (ns < 10) { - temp.append("00000000"); //$NON-NLS-1$ - } else if (ns < 100) { - temp.append("0000000"); //$NON-NLS-1$ - } else if (ns < 1000) { - temp.append("000000"); //$NON-NLS-1$ - } else if (ns < 10000) { - temp.append("00000"); //$NON-NLS-1$ - } else if (ns < 100000) { - temp.append("0000"); //$NON-NLS-1$ - } else if (ns < 1000000) { - temp.append("000"); //$NON-NLS-1$ - } else if (ns < 10000000) { - temp.append("00"); //$NON-NLS-1$ - } else if (ns < 100000000) { - temp.append("0"); //$NON-NLS-1$ - } - - temp.append(ns); - return temp.toString(); - } - - + * + * @return Seconds in the object, in string. + */ + public String getNanoSeconds() { + return formatNs(fValue); + } + + /* + * Use the exponent to format the second in the correct format. + */ + private String formatSecs(final long time) { + final long sec = (long) (time * 1E-9); + return String.valueOf(sec); + } + + /* + * Obtains the remainder fraction on unit Seconds of the entered value in + * nanoseconds. e.g. input: 1241207054171080214 ns. + * The number of fraction seconds can be obtained by removing the last 9 digits: + * In 1241207054, the fractional portion of seconds, expressed in ns is: 171080214 + */ + private String formatNs(long time) { + final boolean neg = time < 0; + if (neg) + time = -time; + // The following approach could be used although performance + // decreases in half. + // String strVal = String.format("%09d", time); + // String tmp = strVal.substring(strVal.length() - 9) + final StringBuffer temp = new StringBuffer(); + long ns = time; + ns %= 1000000000; + if (ns < 10) + temp.append("00000000"); //$NON-NLS-1$ + else if (ns < 100) + temp.append("0000000"); //$NON-NLS-1$ + else if (ns < 1000) + temp.append("000000"); //$NON-NLS-1$ + else if (ns < 10000) + temp.append("00000"); //$NON-NLS-1$ + else if (ns < 100000) + temp.append("0000"); //$NON-NLS-1$ + else if (ns < 1000000) + temp.append("000"); //$NON-NLS-1$ + else if (ns < 10000000) + temp.append("00"); //$NON-NLS-1$ + else if (ns < 100000000) + temp.append("0"); //$NON-NLS-1$ + + temp.append(ns); + return temp.toString(); + } + + /** * toString() method. * @@ -132,45 +130,42 @@ public class LttngTimestamp extends TmfTimestamp { */ @Override @SuppressWarnings("nls") - public String toString() { + public String toString() { long value = fValue; - if (fValue < 0) { - value = -fValue; - } - - StringBuilder sb = new StringBuilder(String.valueOf(value)); + if (fValue < 0) + value = -fValue; + + final StringBuilder sb = new StringBuilder(String.valueOf(value)); // Prepend the correct number of "0" so we can insert a "." at the right location - int nbZeroes = (-fScale) - sb.length() + 1; - for (int i = 0; i < nbZeroes; i++) { + final int nbZeroes = (-fScale) - sb.length() + 1; + for (int i = 0; i < nbZeroes; i++) sb.insert(i, "0"); - } sb.insert(sb.length() + fScale, "."); - + // Prepend "-" if negative - if (fValue < 0) { + if (fValue < 0) sb.insert(0, "-"); - } - + return sb.toString(); } @Override public LttngTimestamp clone() { - return (LttngTimestamp) super.clone(); + return (LttngTimestamp) super.clone(); } /** * Compute the delta between two timestamps (adjusted to scale of current timestamp). * * @param reference the reference timestamp to synchronize with - * @return the delta timestamp + * @return the delta timestamp * @throws ArithmeticException */ @Override - public LttngTimestamp getDelta(ITmfTimestamp other) { - TmfTimestamp delta = (TmfTimestamp) super.getDelta(other); - return new LttngTimestamp(delta); + public LttngTimestamp getDelta(final ITmfTimestamp other) { + final TmfTimestamp delta = (TmfTimestamp) super.getDelta(other); + return new LttngTimestamp(delta); } } diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointTest.java index 2589e83ed4..5e15ffd749 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2010 Ericsson + * Copyright (c) 2009, 2010, 2012 Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -27,158 +27,192 @@ import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; @SuppressWarnings("nls") public class TmfCheckpointTest extends TestCase { - // ------------------------------------------------------------------------ - // Variables - // ------------------------------------------------------------------------ + // ------------------------------------------------------------------------ + // Variables + // ------------------------------------------------------------------------ + + ITmfTimestamp fTimestamp1 = new TmfTimestamp(); + ITmfTimestamp fTimestamp2 = TmfTimestamp.BIG_BANG; + ITmfTimestamp fTimestamp3 = TmfTimestamp.BIG_CRUNCH; - ITmfTimestamp fTimestamp1 = new TmfTimestamp(); - ITmfTimestamp fTimestamp2 = TmfTimestamp.BIG_BANG; - ITmfTimestamp fTimestamp3 = TmfTimestamp.BIG_CRUNCH; + Long aLong1 = 12345L; + Long aLong2 = 23456L; + Long aLong3 = 34567L; + TmfLocation fLocation1 = new TmfLocation(aLong1); + TmfLocation fLocation2 = new TmfLocation(aLong2); + TmfLocation fLocation3 = new TmfLocation(aLong3); - Long aLong1 = 12345L; - Long aLong2 = 23456L; - Long aLong3 = 34567L; - TmfLocation fLocation1 = new TmfLocation(aLong1); - TmfLocation fLocation2 = new TmfLocation(aLong2); - TmfLocation fLocation3 = new TmfLocation(aLong3); + TmfCheckpoint fCheckpoint1 = new TmfCheckpoint(fTimestamp1, fLocation1); + TmfCheckpoint fCheckpoint2 = new TmfCheckpoint(fTimestamp2, fLocation2); + TmfCheckpoint fCheckpoint3 = new TmfCheckpoint(fTimestamp3, fLocation3); - TmfCheckpoint fCheckpoint1 = new TmfCheckpoint(fTimestamp1, fLocation1); - TmfCheckpoint fCheckpoint2 = new TmfCheckpoint(fTimestamp2, fLocation2); - TmfCheckpoint fCheckpoint3 = new TmfCheckpoint(fTimestamp3, fLocation3); - // ------------------------------------------------------------------------ // Housekeeping // ------------------------------------------------------------------------ - /** - * @param name the test name - */ - public TmfCheckpointTest(String name) { - super(name); - } + /** + * @param name the test name + */ + public TmfCheckpointTest(final String name) { + super(name); + } - @Override - protected void setUp() throws Exception { - super.setUp(); - } + @Override + protected void setUp() throws Exception { + super.setUp(); + } - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ - public void testTmfCheckpoint() { - assertEquals("TmfCheckpoint", fTimestamp1, fCheckpoint1.getTimestamp()); - assertEquals("TmfCheckpoint", fLocation1, fCheckpoint1.getLocation()); - } + public void testTmfCheckpoint() { + assertEquals("TmfCheckpoint", fTimestamp1, fCheckpoint1.getTimestamp()); + assertEquals("TmfCheckpoint", fLocation1, fCheckpoint1.getLocation()); + } + + public void testTmfLocationCopy() { + final TmfCheckpoint checkpoint = new TmfCheckpoint(fCheckpoint1); + + assertEquals("TmfCheckpoint", fTimestamp1, checkpoint.getTimestamp()); + assertEquals("TmfCheckpoint", fLocation1, checkpoint.getLocation()); + } + + public void testTmfLocationCopy2() throws Exception { + try { + new TmfCheckpoint(null); + fail("null copy"); + } + catch (final IllegalArgumentException e) { + // Success + } + catch (final Exception e) { + fail("wrong exception"); + } + } - public void testTmfLocationCopy() { - TmfCheckpoint checkpoint = new TmfCheckpoint(fCheckpoint1); - - assertEquals("TmfCheckpoint", fTimestamp1, checkpoint.getTimestamp()); - assertEquals("TmfCheckpoint", fLocation1, checkpoint.getLocation()); - } + // ------------------------------------------------------------------------ + // clone + // ------------------------------------------------------------------------ - public void testTmfLocationCopy2() throws Exception { - try { - new TmfCheckpoint(null); - fail("null copy"); - } - catch (IllegalArgumentException e) { - // Success - } - } + public void testClone() { + try { + TmfCheckpoint checkpoint1 = fCheckpoint1.clone(); + TmfCheckpoint checkpoint2 = fCheckpoint1.clone(); + TmfCheckpoint checkpoint3 = fCheckpoint1.clone(); + + assertEquals("clone", checkpoint1, fCheckpoint1); + assertEquals("clone", checkpoint2, fCheckpoint1); + assertEquals("clone", checkpoint3, fCheckpoint1); + + checkpoint1 = new TmfCheckpoint(fTimestamp1, null); + checkpoint2 = checkpoint1.clone(); + assertEquals("clone", checkpoint1, checkpoint2); + assertNull(checkpoint1.getLocation()); + assertNull(checkpoint2.getLocation()); + + checkpoint1 = new TmfCheckpoint(null, fLocation1); + checkpoint3 = checkpoint1.clone(); + assertEquals("clone", checkpoint1, checkpoint3); + assertNull(checkpoint1.getTimestamp()); + assertNull(checkpoint3.getTimestamp()); + + } catch (final InternalError e) { + fail("clone()"); + } + } // ------------------------------------------------------------------------ // equals // ------------------------------------------------------------------------ - public void testEqualsReflexivity() throws Exception { - assertTrue("equals", fCheckpoint1.equals(fCheckpoint1)); - assertTrue("equals", fCheckpoint2.equals(fCheckpoint2)); - - assertTrue("equals", !fCheckpoint1.equals(fCheckpoint2)); - assertTrue("equals", !fCheckpoint2.equals(fCheckpoint1)); - } - - public void testEqualsSymmetry() throws Exception { - TmfCheckpoint checkpoint1 = new TmfCheckpoint(fCheckpoint1); - TmfCheckpoint checkpoint2 = new TmfCheckpoint(fCheckpoint2); - - assertTrue("equals", checkpoint1.equals(fCheckpoint1)); - assertTrue("equals", fCheckpoint1.equals(checkpoint1)); - - assertTrue("equals", checkpoint2.equals(fCheckpoint2)); - assertTrue("equals", fCheckpoint2.equals(checkpoint2)); - } - - public void testEqualsTransivity() throws Exception { - TmfCheckpoint checkpoint1 = new TmfCheckpoint(fCheckpoint1); - TmfCheckpoint checkpoint2 = new TmfCheckpoint(checkpoint1); - TmfCheckpoint checkpoint3 = new TmfCheckpoint(checkpoint2); - - assertTrue("equals", checkpoint1.equals(checkpoint2)); - assertTrue("equals", checkpoint2.equals(checkpoint3)); - assertTrue("equals", checkpoint1.equals(checkpoint3)); - } - - public void testEqualsNull() throws Exception { - assertTrue("equals", !fCheckpoint1.equals(null)); - assertTrue("equals", !fCheckpoint2.equals(null)); - } - - // ------------------------------------------------------------------------ - // hashCode - // ------------------------------------------------------------------------ - - public void testHashCode() throws Exception { - TmfCheckpoint checkpoint1 = new TmfCheckpoint(fCheckpoint1); - TmfCheckpoint checkpoint2 = new TmfCheckpoint(fCheckpoint2); - - assertTrue("hashCode", fCheckpoint1.hashCode() == checkpoint1.hashCode()); - assertTrue("hashCode", fCheckpoint2.hashCode() == checkpoint2.hashCode()); - - assertTrue("hashCode", fCheckpoint1.hashCode() != checkpoint2.hashCode()); - assertTrue("hashCode", fCheckpoint2.hashCode() != checkpoint1.hashCode()); - } - + public void testEqualsReflexivity() throws Exception { + assertTrue("equals", fCheckpoint1.equals(fCheckpoint1)); + assertTrue("equals", fCheckpoint2.equals(fCheckpoint2)); + + assertTrue("equals", !fCheckpoint1.equals(fCheckpoint2)); + assertTrue("equals", !fCheckpoint2.equals(fCheckpoint1)); + } + + public void testEqualsSymmetry() throws Exception { + final TmfCheckpoint checkpoint1 = new TmfCheckpoint(fCheckpoint1); + final TmfCheckpoint checkpoint2 = new TmfCheckpoint(fCheckpoint2); + + assertTrue("equals", checkpoint1.equals(fCheckpoint1)); + assertTrue("equals", fCheckpoint1.equals(checkpoint1)); + + assertTrue("equals", checkpoint2.equals(fCheckpoint2)); + assertTrue("equals", fCheckpoint2.equals(checkpoint2)); + } + + public void testEqualsTransivity() throws Exception { + final TmfCheckpoint checkpoint1 = new TmfCheckpoint(fCheckpoint1); + final TmfCheckpoint checkpoint2 = new TmfCheckpoint(checkpoint1); + final TmfCheckpoint checkpoint3 = new TmfCheckpoint(checkpoint2); + + assertTrue("equals", checkpoint1.equals(checkpoint2)); + assertTrue("equals", checkpoint2.equals(checkpoint3)); + assertTrue("equals", checkpoint1.equals(checkpoint3)); + } + + public void testEqualsNull() throws Exception { + assertTrue("equals", !fCheckpoint1.equals(null)); + assertTrue("equals", !fCheckpoint2.equals(null)); + } + + // ------------------------------------------------------------------------ + // hashCode + // ------------------------------------------------------------------------ + + public void testHashCode() throws Exception { + final TmfCheckpoint checkpoint1 = new TmfCheckpoint(fCheckpoint1); + final TmfCheckpoint checkpoint2 = new TmfCheckpoint(fCheckpoint2); + + assertTrue("hashCode", fCheckpoint1.hashCode() == checkpoint1.hashCode()); + assertTrue("hashCode", fCheckpoint2.hashCode() == checkpoint2.hashCode()); + + assertTrue("hashCode", fCheckpoint1.hashCode() != checkpoint2.hashCode()); + assertTrue("hashCode", fCheckpoint2.hashCode() != checkpoint1.hashCode()); + } + // ------------------------------------------------------------------------ // toString // ------------------------------------------------------------------------ - public void testToString() { - String expected1 = "[TmfCheckpoint(" + fCheckpoint1.getTimestamp() + "," + - fCheckpoint1.getLocation() + ")]"; - String expected2 = "[TmfCheckpoint(" + fCheckpoint2.getTimestamp() + "," + - fCheckpoint2.getLocation() + ")]"; - String expected3 = "[TmfCheckpoint(" + fCheckpoint3.getTimestamp() + "," + - fCheckpoint3.getLocation() + ")]"; + public void testToString() { + final String expected1 = "TmfCheckpoint [fLocation=" + fCheckpoint1.getLocation() + + ", fTimestamp=" + fCheckpoint1.getTimestamp() + "]"; + final String expected2 = "TmfCheckpoint [fLocation=" + fCheckpoint2.getLocation() + + ", fTimestamp=" + fCheckpoint2.getTimestamp() + "]"; + final String expected3 = "TmfCheckpoint [fLocation=" + fCheckpoint3.getLocation() + + ", fTimestamp=" + fCheckpoint3.getTimestamp() + "]"; - assertEquals("toString", expected1, fCheckpoint1.toString()); - assertEquals("toString", expected2, fCheckpoint2.toString()); - assertEquals("toString", expected3, fCheckpoint3.toString()); - } + assertEquals("toString", expected1, fCheckpoint1.toString()); + assertEquals("toString", expected2, fCheckpoint2.toString()); + assertEquals("toString", expected3, fCheckpoint3.toString()); + } // ------------------------------------------------------------------------ // compareTo // ------------------------------------------------------------------------ - public void testCompareTo() { - assertEquals("compareTo", 0, fCheckpoint1.compareTo(fCheckpoint1)); - assertEquals("compareTo", 1, fCheckpoint1.compareTo(fCheckpoint2)); - assertEquals("compareTo", -1, fCheckpoint1.compareTo(fCheckpoint3)); + public void testCompareTo() { + assertEquals("compareTo", 0, fCheckpoint1.compareTo(fCheckpoint1)); + assertEquals("compareTo", 1, fCheckpoint1.compareTo(fCheckpoint2)); + assertEquals("compareTo", -1, fCheckpoint1.compareTo(fCheckpoint3)); - assertEquals("compareTo", -1, fCheckpoint2.compareTo(fCheckpoint1)); - assertEquals("compareTo", 0, fCheckpoint2.compareTo(fCheckpoint2)); - assertEquals("compareTo", -1, fCheckpoint2.compareTo(fCheckpoint3)); + assertEquals("compareTo", -1, fCheckpoint2.compareTo(fCheckpoint1)); + assertEquals("compareTo", 0, fCheckpoint2.compareTo(fCheckpoint2)); + assertEquals("compareTo", -1, fCheckpoint2.compareTo(fCheckpoint3)); - assertEquals("compareTo", 1, fCheckpoint3.compareTo(fCheckpoint1)); - assertEquals("compareTo", 1, fCheckpoint3.compareTo(fCheckpoint2)); - assertEquals("compareTo", 0, fCheckpoint3.compareTo(fCheckpoint3)); - } + assertEquals("compareTo", 1, fCheckpoint3.compareTo(fCheckpoint1)); + assertEquals("compareTo", 1, fCheckpoint3.compareTo(fCheckpoint2)); + assertEquals("compareTo", 0, fCheckpoint3.compareTo(fCheckpoint3)); + } } diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfContextTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfContextTest.java index 558211dd7f..9eb3db9115 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfContextTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfContextTest.java @@ -117,6 +117,19 @@ public class TmfContextTest extends TestCase { assertEquals("getRank", fRank3, context3.getRank()); } + public void testTmfContextCopy2() { + try { + new TmfContext((TmfContext) null); + fail("Copy constructor: no exception"); + } + catch (final IllegalArgumentException e) { + // pass + } + catch (final Exception e) { + fail("Copy constructor: wrong exception"); + } + } + // ------------------------------------------------------------------------ // equals // ------------------------------------------------------------------------ @@ -232,6 +245,13 @@ public class TmfContextTest extends TestCase { assertEquals("clone", context1, fContext1); assertEquals("clone", context2, fContext2); assertEquals("clone", context3, fContext3); + + context1.setLocation(null); + final TmfContext context4 = context1.clone(); + assertEquals("clone", context1, context4); + assertNull(context1.getLocation()); + assertNull(context4.getLocation()); + } catch (final InternalError e) { fail("clone()"); } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfCheckpoint.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfCheckpoint.java new file mode 100644 index 0000000000..ef4aec8420 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfCheckpoint.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2012 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Francois Chouinard - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.core.trace; + +import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; + +/** + * ITmfCheckpoint + *

+ * The basic trace checkpoint structure in TMF. The purpose of the checkpoint is + * to associate a trace location to an event timestamp. + */ +public interface ITmfCheckpoint extends Cloneable, Comparable { + + // ------------------------------------------------------------------------ + // Getters + // ------------------------------------------------------------------------ + + /** + * @return the timestamp of the event referred to by the context + */ + public ITmfTimestamp getTimestamp(); + + /** + * @return the location of the event referred to by the context + */ + public ITmfLocation> getLocation(); + + // ------------------------------------------------------------------------ + // Cloneable + // ------------------------------------------------------------------------ + + /** + * @return a clone of the checkpoint + */ + public ITmfCheckpoint clone(); + + // ------------------------------------------------------------------------ + // Comparable + // ------------------------------------------------------------------------ + + /* + * (non-Javadoc) + * + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(ITmfCheckpoint checkpoint); + +} diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfCheckpoint.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfCheckpoint.java index fb214d13ab..370fc1c220 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfCheckpoint.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfCheckpoint.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 Ericsson + * Copyright (c) 2009, 2012 Ericsson * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which @@ -8,7 +8,8 @@ * * Contributors: * Francois Chouinard - Initial API and implementation - *******************************************************************************/ + * Francois Chouinard - Updated as per TMF Trace Model 1.0 + ******************************************************************************/ package org.eclipse.linuxtools.tmf.core.trace; @@ -19,43 +20,69 @@ import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; *

* This class maps an event timestamp to a generic location. */ -@SuppressWarnings("rawtypes") -public class TmfCheckpoint implements Comparable, Cloneable { +public class TmfCheckpoint implements ITmfCheckpoint { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ + // The checkpoint location + private ITmfLocation> fLocation; + + // The checkpoint timestamp private ITmfTimestamp fTimestamp; - private ITmfLocation fLocation; // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ + /** + * Default constructor + */ @SuppressWarnings("unused") private TmfCheckpoint() { } /** - * @param ts the checkpoint timestamp + * Full constructor + * + * @param timestamp the checkpoint timestamp * @param location the corresponding trace location */ - public TmfCheckpoint(final ITmfTimestamp ts, final ITmfLocation location) { - fTimestamp = ts; + public TmfCheckpoint(final ITmfTimestamp timestamp, final ITmfLocation> location) { + fTimestamp = timestamp; fLocation = location; } /** - * Deep copy constructor + * Copy constructor * * @param other the other checkpoint */ public TmfCheckpoint(final TmfCheckpoint other) { if (other == null) throw new IllegalArgumentException(); - fTimestamp = other.fTimestamp.clone(); - fLocation = other.fLocation.clone(); + fTimestamp = other.fTimestamp; + fLocation = other.fLocation; + } + + // ------------------------------------------------------------------------ + // Cloneable + // ------------------------------------------------------------------------ + + /* (non-Javadoc) + * @see java.lang.Object#clone() + */ + @Override + public TmfCheckpoint clone() { + TmfCheckpoint clone = null; + try { + clone = (TmfCheckpoint) super.clone(); + clone.fLocation = (fLocation != null) ? fLocation.clone() : null; + clone.fTimestamp = (fTimestamp != null) ? fTimestamp.clone() : null; + } catch (final CloneNotSupportedException e) { + } + return clone; } // ------------------------------------------------------------------------ @@ -65,6 +92,7 @@ public class TmfCheckpoint implements Comparable, Cloneable { /** * @return the checkpoint timestamp */ + @Override public ITmfTimestamp getTimestamp() { return fTimestamp; } @@ -72,57 +100,65 @@ public class TmfCheckpoint implements Comparable, Cloneable { /** * @return the checkpoint stream location */ + @Override public ITmfLocation getLocation() { return fLocation; } // ------------------------------------------------------------------------ - // Object + // Comparable // ------------------------------------------------------------------------ + @SuppressWarnings({ "unchecked", "rawtypes" }) @Override - public TmfCheckpoint clone() { - TmfCheckpoint result = null; - try { - result = (TmfCheckpoint) super.clone(); - result.fTimestamp = fTimestamp.clone(); - result.fLocation = fLocation.clone(); - return result; - } catch (final CloneNotSupportedException e) { - e.printStackTrace(); + public int compareTo(final ITmfCheckpoint other) { + if (fTimestamp == null || other.getTimestamp() == null) { + final Comparable location1 = fLocation.getLocation(); + final Comparable location2 = other.getLocation().getLocation(); + return location1.compareTo(location2); } - return result; + return fTimestamp.compareTo(other.getTimestamp(), false); } + // ------------------------------------------------------------------------ + // Object + // ------------------------------------------------------------------------ + @Override public int hashCode() { - return fTimestamp.hashCode(); + final int prime = 31; + int result = 1; + result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode()); + result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode()); + return result; } @Override - public boolean equals(final Object other) { - if (!(other instanceof TmfCheckpoint)) + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof TmfCheckpoint)) return false; - final TmfCheckpoint o = (TmfCheckpoint) other; - return fTimestamp.equals(o.fTimestamp); + final TmfCheckpoint other = (TmfCheckpoint) obj; + if (fLocation == null) { + if (other.fLocation != null) + return false; + } else if (!fLocation.equals(other.fLocation)) + return false; + if (fTimestamp == null) { + if (other.fTimestamp != null) + return false; + } else if (!fTimestamp.equals(other.fTimestamp)) + return false; + return true; } @Override @SuppressWarnings("nls") public String toString() { - return "[TmfCheckpoint(" + fTimestamp + "," + fLocation + ")]"; - } - - // ------------------------------------------------------------------------ - // Comparable - // ------------------------------------------------------------------------ - - @SuppressWarnings("unchecked") - @Override - public int compareTo(final TmfCheckpoint other) { - if (fTimestamp == null || other.fTimestamp == null) - return fLocation.getLocation().compareTo(other.fLocation.getLocation()); - return fTimestamp.compareTo(other.fTimestamp, false); + return "TmfCheckpoint [fLocation=" + fLocation + ", fTimestamp=" + fTimestamp + "]"; } } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfContext.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfContext.java index 173a9b69da..1b67039e5a 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfContext.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfContext.java @@ -48,7 +48,7 @@ public class TmfContext implements ITmfContext, Cloneable { * * @param location the event location */ - public TmfContext(ITmfLocation> location) { + public TmfContext(final ITmfLocation> location) { this(location, UNKNOWN_RANK); } @@ -58,7 +58,7 @@ public class TmfContext implements ITmfContext, Cloneable { * @param location the event location * @param rank the event rank */ - public TmfContext(ITmfLocation> location, long rank) { + public TmfContext(final ITmfLocation> location, final long rank) { fLocation = location; fRank = rank; } @@ -68,8 +68,11 @@ public class TmfContext implements ITmfContext, Cloneable { * * @param context the other context */ - public TmfContext(TmfContext context) { - this(context.fLocation, context.fRank); + public TmfContext(final TmfContext context) { + if (context == null) + throw new IllegalArgumentException(); + fLocation = context.fLocation; + fRank = context.fRank; } // ------------------------------------------------------------------------ @@ -84,9 +87,9 @@ public class TmfContext implements ITmfContext, Cloneable { TmfContext clone = null; try { clone = (TmfContext) super.clone(); - clone.fLocation = fLocation.clone(); + clone.fLocation = (fLocation != null) ? fLocation.clone() : null; clone.fRank = fRank; - } catch (CloneNotSupportedException e) { + } catch (final CloneNotSupportedException e) { } return clone; } @@ -107,7 +110,7 @@ public class TmfContext implements ITmfContext, Cloneable { * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setLocation(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation) */ @Override - public void setLocation(ITmfLocation> location) { + public void setLocation(final ITmfLocation> location) { fLocation = location; } @@ -123,7 +126,7 @@ public class TmfContext implements ITmfContext, Cloneable { * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long) */ @Override - public void setRank(long rank) { + public void setRank(final long rank) { fRank = rank; } @@ -171,14 +174,14 @@ public class TmfContext implements ITmfContext, Cloneable { * @see java.lang.Object#equals(java.lang.Object) */ @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; - TmfContext other = (TmfContext) obj; + final TmfContext other = (TmfContext) obj; if (fLocation == null) { if (other.fLocation != null) return false; 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 7b56b9c0de..7b00f324ef 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 @@ -27,8 +27,11 @@ public class TmfLocation> implements ITmfLocation { // Constants // ------------------------------------------------------------------------ + /** + * The 'null' location + */ static public final TmfLocation NULL_LOCATION = new TmfLocation(); - + // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ @@ -51,7 +54,7 @@ public class TmfLocation> implements ITmfLocation { * * @param location the trace location */ - public TmfLocation(L location) { + public TmfLocation(final L location) { fLocation = location; } @@ -60,7 +63,7 @@ public class TmfLocation> implements ITmfLocation { * * @param other the original location */ - public TmfLocation(TmfLocation location) { + public TmfLocation(final TmfLocation location) { fLocation = location.fLocation; } @@ -90,16 +93,15 @@ public class TmfLocation> implements ITmfLocation { try { clone = (TmfLocation) super.clone(); if (fLocation != null) { - Class clazz = fLocation.getClass(); - Method method = clazz.getMethod("clone", new Class[0]); //$NON-NLS-1$ - Object copy = method.invoke(this.fLocation, new Object[0]); + final Class clazz = fLocation.getClass(); + final Method method = clazz.getMethod("clone", new Class[0]); //$NON-NLS-1$ + final Object copy = method.invoke(this.fLocation, new Object[0]); clone.fLocation = (L) copy; - } else { + } else clone.fLocation = null; - } - } catch (CloneNotSupportedException e) { - } catch (NoSuchMethodException e) { - } catch (Exception e) { + } catch (final CloneNotSupportedException e) { + } catch (final NoSuchMethodException e) { + } catch (final Exception e) { throw new InternalError(e.toString()); } return clone; @@ -125,14 +127,14 @@ public class TmfLocation> implements ITmfLocation { */ @Override @SuppressWarnings("unchecked") - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; - TmfLocation other = (TmfLocation) obj; + final TmfLocation other = (TmfLocation) obj; if (fLocation == null) { if (other.fLocation != null) return false; -- 2.34.1