/*******************************************************************************
- * 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
import junit.framework.TestCase;
+import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
@SuppressWarnings("nls")
public class TmfLocationTest extends TestCase {
- // ------------------------------------------------------------------------
- // Variables
- // ------------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+ // Variables
+ // ------------------------------------------------------------------------
- String aString = "some location";
- Long aLong = 12345L;
- TmfTimestamp aTimestamp = new TmfTimestamp();
+ String aString = "some location";
+ Long aLong = 12345L;
+ TmfTimestamp aTimestamp = new TmfTimestamp();
- TmfLocation<String> fLocation1;
- TmfLocation<Long> fLocation2;
- TmfLocation<TmfTimestamp> fLocation3;
+ TmfLocation<Boolean> fLocation0;
+ TmfLocation<String> fLocation1;
+ TmfLocation<String> fLocation2;
+ TmfLocation<Long> fLocation3;
+ TmfLocation<ITmfTimestamp> fLocation4;
// ------------------------------------------------------------------------
// Housekeeping
// ------------------------------------------------------------------------
- /**
- * @param name the test name
- */
- public TmfLocationTest(String name) {
- super(name);
- }
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- fLocation1 = new TmfLocation<String>(aString);
- fLocation2 = new TmfLocation<Long>(aLong);
- fLocation3 = new TmfLocation<TmfTimestamp>(aTimestamp);
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- }
+ /**
+ * @param name
+ * the test name
+ */
+ public TmfLocationTest(String name) {
+ super(name);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ fLocation0 = TmfLocation.NULL_LOCATION;
+ fLocation1 = new TmfLocation<String>((String) null);
+ fLocation2 = new TmfLocation<String>(aString);
+ fLocation3 = new TmfLocation<Long>(aLong);
+ fLocation4 = new TmfLocation<ITmfTimestamp>(aTimestamp);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
- public void testTmfLocation() {
- assertEquals("TmfLocation", aString, fLocation1.getLocation());
- assertEquals("TmfLocation", aLong, fLocation2.getLocation());
- assertEquals("TmfLocation", aTimestamp, fLocation3.getLocation());
- }
-
- public void testTmfLocationCopy() {
- TmfLocation<String> location1 = new TmfLocation<String>(fLocation1);
- TmfLocation<Long> location2 = new TmfLocation<Long>(fLocation2);
- TmfLocation<TmfTimestamp> location3 = new TmfLocation<TmfTimestamp>(fLocation3);
-
- assertEquals("TmfLocation", aString, location1.getLocation());
- assertEquals("TmfLocation", aLong, location2.getLocation());
- assertEquals("TmfLocation", aTimestamp, location3.getLocation());
- }
-
- public void testTmfLocationCopy2() throws Exception {
- try {
- new TmfLocation<Long>((TmfLocation<Long>) null);
- fail("null copy");
- }
- catch (IllegalArgumentException e) {
- // Success
- }
- }
+ public void testTmfLocation() {
+ assertNull("TmfLocation", fLocation0.getLocation());
+ assertNull("TmfLocation", fLocation1.getLocation());
+ assertEquals("TmfLocation", aString, fLocation2.getLocation());
+ assertEquals("TmfLocation", aLong, fLocation3.getLocation());
+ assertEquals("TmfLocation", aTimestamp, fLocation4.getLocation());
+ }
+
+ public void testTmfLocationCopy() {
+ TmfLocation<Boolean> location0 = new TmfLocation<Boolean>(fLocation0);
+ TmfLocation<String> location1 = new TmfLocation<String>(fLocation1);
+ TmfLocation<String> location2 = new TmfLocation<String>(fLocation2);
+ TmfLocation<Long> location3 = new TmfLocation<Long>(fLocation3);
+ TmfLocation<ITmfTimestamp> location4 = new TmfLocation<ITmfTimestamp>(fLocation4);
+
+ assertNull("TmfLocation", location0.getLocation());
+ assertNull("TmfLocation", location1.getLocation());
+ assertEquals("TmfLocation", aString, location2.getLocation());
+ assertEquals("TmfLocation", aLong, location3.getLocation());
+ assertEquals("TmfLocation", aTimestamp, location4.getLocation());
+ }
+
+ // ------------------------------------------------------------------------
+ // clone
+ // ------------------------------------------------------------------------
+
+ public void testClone() {
+ try {
+ TmfLocation<String> location1 = fLocation1.clone();
+ TmfLocation<String> location2 = fLocation2.clone();
+ TmfLocation<Long> location3 = fLocation3.clone();
+ TmfLocation<ITmfTimestamp> location4 = fLocation4.clone();
+
+ assertEquals("clone", fLocation1, location1);
+ assertEquals("clone", fLocation2, location2);
+ assertEquals("clone", fLocation3, location3);
+ assertEquals("clone", fLocation4, location4);
+
+ assertEquals("clone", fLocation1.getLocation(), location1.getLocation());
+ assertEquals("clone", fLocation2.getLocation(), location2.getLocation());
+ assertEquals("clone", fLocation3.getLocation(), location3.getLocation());
+ assertEquals("clone", fLocation4.getLocation(), location4.getLocation());
+
+ assertNull("clone", location1.getLocation());
+ assertEquals("clone", aString, location2.getLocation());
+ assertEquals("clone", aLong, location3.getLocation());
+ assertEquals("clone", aTimestamp, location4.getLocation());
+ } catch (InternalError e) {
+ fail("clone()");
+ }
+ }
+
+ public static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> {
+ private String fName;
+
+ public MyCloneableClass(String name) {
+ fName = name;
+ }
+
+ @Override
+ public String toString() {
+ return fName;
+ }
+
+ @Override
+ public MyCloneableClass clone() {
+ MyCloneableClass clone = null;
+ try {
+ clone = (MyCloneableClass) super.clone();
+ clone.fName = fName;
+ } catch (CloneNotSupportedException e) {
+ }
+ return clone;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return fName.compareTo(((MyCloneableClass) o).fName) == 0;
+ }
+
+ @Override
+ public int compareTo(MyCloneableClass o) {
+ return fName.compareTo(o.fName);
+ }
+ }
+
+ public void testCloneCloneable() {
+ try {
+ MyCloneableClass myClass = new MyCloneableClass("myCloneableClass");
+ TmfLocation<MyCloneableClass> location = new TmfLocation<MyCloneableClass>(myClass);
+ TmfLocation<MyCloneableClass> clone = location.clone();
+
+ assertEquals("clone", location, clone);
+ assertEquals("clone", location.getLocation(), clone.getLocation());
+ assertEquals("clone", myClass, location.getLocation());
+ } catch (InternalError e) {
+ fail("clone a cloneable class");
+ }
+ }
+
+ public static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> {
+ private String fName;
+
+ public MyUnCloneableClass(String name) {
+ fName = name;
+ }
+
+ @Override
+ public String toString() {
+ return fName;
+ }
+
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ throw new CloneNotSupportedException();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return fName.compareTo(((MyUnCloneableClass) o).fName) == 0;
+ }
+
+ @Override
+ public int compareTo(MyUnCloneableClass o) {
+ return fName.compareTo(o.fName);
+ }
+ }
+
+ public void testCloneUncloneable() {
+ try {
+ MyUnCloneableClass myClass = new MyUnCloneableClass("myUncloneableClass");
+ TmfLocation<MyUnCloneableClass> myLocation = new TmfLocation<MyUnCloneableClass>(myClass);
+ myLocation.clone();
+ fail("clone an uncloneable class");
+ } catch (InternalError e) {
+ }
+ }
// ------------------------------------------------------------------------
- // setLocation
+ // hashCode
// ------------------------------------------------------------------------
- public void testSetLocation() {
- String aString2 = "some other location";
- Long aLong2 = 1234567L;
- TmfTimestamp aTimestamp2 = (TmfTimestamp) TmfTimestamp.BIG_BANG;
+ public void testHashCode() throws Exception {
+ TmfLocation<String> location1 = new TmfLocation<String>((String) null);
+ TmfLocation<String> location2 = new TmfLocation<String>(aString);
+ TmfLocation<Long> location3 = new TmfLocation<Long>(aLong);
- fLocation1.setLocation(aString2);
- fLocation2.setLocation(aLong2);
- fLocation3.setLocation(aTimestamp2);
-
- assertEquals("TmfLocation", aString2, fLocation1.getLocation());
- assertEquals("TmfLocation", aLong2, fLocation2.getLocation());
- assertEquals("TmfLocation", aTimestamp2, fLocation3.getLocation());
- }
+ assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode());
+ assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode());
+ assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode());
+
+ assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode());
+ assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode());
+ }
// ------------------------------------------------------------------------
// toEquals
// ------------------------------------------------------------------------
- public void testEqualsReflexivity() throws Exception {
- assertTrue("equals", fLocation1.equals(fLocation1));
- assertTrue("equals", fLocation2.equals(fLocation2));
-
- assertTrue("equals", !fLocation1.equals(fLocation2));
- assertTrue("equals", !fLocation2.equals(fLocation1));
- }
-
- public void testEqualsSymmetry() throws Exception {
- TmfLocation<String> location1 = new TmfLocation<String>(aString);
- TmfLocation<Long> location2 = new TmfLocation<Long>(aLong);
-
- assertTrue("equals", location1.equals(fLocation1));
- assertTrue("equals", fLocation1.equals(location1));
-
- assertTrue("equals", location2.equals(fLocation2));
- assertTrue("equals", fLocation2.equals(location2));
- }
-
- public void testEqualsTransivity() throws Exception {
- TmfLocation<String> location1 = new TmfLocation<String>(aString);
- TmfLocation<String> location2 = new TmfLocation<String>(aString);
- TmfLocation<String> location3 = new TmfLocation<String>(aString);
-
- assertTrue("equals", location1.equals(location2));
- assertTrue("equals", location2.equals(location3));
- assertTrue("equals", location1.equals(location3));
- }
-
- public void testEqualsNull() throws Exception {
- assertTrue("equals", !fLocation1.equals(null));
- assertTrue("equals", !fLocation1.equals(null));
- }
-
- // ------------------------------------------------------------------------
- // hashCode
- // ------------------------------------------------------------------------
-
- public void testHashCode() throws Exception {
- TmfLocation<String> location1 = new TmfLocation<String>(aString);
- TmfLocation<Long> location2 = new TmfLocation<Long>(aLong);
-
- assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode());
- assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode());
-
- assertTrue("hashCode", fLocation1.hashCode() != location2.hashCode());
- assertTrue("hashCode", fLocation2.hashCode() != location1.hashCode());
- }
-
+ private static class TmfLocation2 extends TmfLocation<String> {
+ public TmfLocation2(String location) {
+ super(location);
+ }
+ }
+
+ public void testEqualsWrongTypes() throws Exception {
+ TmfLocation<String> location1 = new TmfLocation<String>(aString);
+ TmfLocation2 location2 = new TmfLocation2(aString);
+
+ assertFalse("equals", location1.equals(location2));
+ assertFalse("equals", location2.equals(location1));
+ }
+
+ public void testEqualsWithNulls() throws Exception {
+ TmfLocation<String> location1 = new TmfLocation<String>(aString);
+ TmfLocation<String> location2 = new TmfLocation<String>((String) null);
+
+ assertFalse("equals", location1.equals(location2));
+ assertFalse("equals", location2.equals(location1));
+ }
+
+ public void testEqualsReflexivity() throws Exception {
+ assertTrue("equals", fLocation2.equals(fLocation2));
+ assertTrue("equals", fLocation3.equals(fLocation3));
+
+ assertTrue("equals", !fLocation2.equals(fLocation3));
+ assertTrue("equals", !fLocation3.equals(fLocation2));
+ }
+
+ public void testEqualsSymmetry() throws Exception {
+ TmfLocation<String> location2 = new TmfLocation<String>(aString);
+ TmfLocation<Long> location3 = new TmfLocation<Long>(aLong);
+
+ assertTrue("equals", location2.equals(fLocation2));
+ assertTrue("equals", fLocation2.equals(location2));
+
+ assertTrue("equals", location3.equals(fLocation3));
+ assertTrue("equals", fLocation3.equals(location3));
+ }
+
+ public void testEqualsTransivity() throws Exception {
+ TmfLocation<String> location1 = new TmfLocation<String>(aString);
+ TmfLocation<String> location2 = new TmfLocation<String>(aString);
+ TmfLocation<String> location3 = new TmfLocation<String>(aString);
+
+ assertTrue("equals", location1.equals(location2));
+ assertTrue("equals", location2.equals(location3));
+ assertTrue("equals", location3.equals(location1));
+ }
+
+ public void testEqualsNull() throws Exception {
+ assertTrue("equals", !fLocation2.equals(null));
+ assertTrue("equals", !fLocation2.equals(null));
+ }
+
// ------------------------------------------------------------------------
// toString
// ------------------------------------------------------------------------
- public void testToString() {
- String aString = "some location";
- Long aLong = 12345L;
- TmfTimestamp aTimestamp = new TmfTimestamp();
-
- TmfLocation<String> location1 = new TmfLocation<String>(aString);
- TmfLocation<Long> location2 = new TmfLocation<Long>(aLong);
- TmfLocation<TmfTimestamp> location3 = new TmfLocation<TmfTimestamp>(aTimestamp);
+ public void testToString() {
+ String aString = "some location";
+ Long aLong = 12345L;
+ TmfTimestamp aTimestamp = new TmfTimestamp();
- assertEquals("TmfLocation", aString.toString(), location1.toString());
- assertEquals("TmfLocation", aLong.toString(), location2.toString());
- assertEquals("TmfLocation", aTimestamp.toString(), location3.toString());
- }
+ TmfLocation<String> location1 = new TmfLocation<String>(aString);
+ TmfLocation<Long> location2 = new TmfLocation<Long>(aLong);
+ TmfLocation<ITmfTimestamp> location3 = new TmfLocation<ITmfTimestamp>(aTimestamp);
- // ------------------------------------------------------------------------
- // clone
- // ------------------------------------------------------------------------
+ String expected1 = "TmfLocation [fLocation=" + aString + "]";
+ String expected2 = "TmfLocation [fLocation=" + aLong + "]";
+ String expected3 = "TmfLocation [fLocation=" + aTimestamp + "]";
- public void testClone() {
- try {
- TmfLocation<String> location1 = fLocation1.clone();
- TmfLocation<Long> location2 = fLocation2.clone();
- TmfLocation<TmfTimestamp> location3 = fLocation3.clone();
-
- assertEquals("TmfLocation", aString.toString(), location1.toString());
- assertEquals("TmfLocation", aLong.toString(), location2.toString());
- assertEquals("TmfLocation", aTimestamp.toString(), location3.toString());
- }
- catch (InternalError e) {
- fail("clone()");
- }
- }
-
- public static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> {
- private String fName;
- public MyCloneableClass(String name) {
- fName = name;
- }
- @Override
- public String toString() {
- return fName;
- }
- @Override
- public MyCloneableClass clone() {
- MyCloneableClass clone = null;
- try {
- clone = (MyCloneableClass) super.clone();
- clone.fName = fName;
- } catch (CloneNotSupportedException e) {
- }
- return clone;
- }
- @Override
- public int compareTo(MyCloneableClass o) {
- return 0;
- }
- }
-
- public void testCloneCloneable() {
- try {
- MyCloneableClass myClass = new MyCloneableClass("myClass");
- TmfLocation<MyCloneableClass> myLocation = new TmfLocation<MyCloneableClass>(myClass);
- TmfLocation<MyCloneableClass> location4 = myLocation.clone();
-
- assertEquals("TmfLocation", myClass.toString(), location4.toString());
- }
- catch (InternalError e) {
- fail("clone()");
- }
- }
-
- public static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> {
- private String fName;
- public MyUnCloneableClass(String name) {
- fName = name;
- }
- @Override
- public String toString() {
- return fName;
- }
- @Override
- public Object clone() throws CloneNotSupportedException {
- throw new CloneNotSupportedException();
- }
- @Override
- public int compareTo(MyUnCloneableClass o) {
- return 0;
- }
- }
-
- public void testCloneUnCloneable() {
- try {
- MyUnCloneableClass myClass = new MyUnCloneableClass("myClass");
- TmfLocation<MyUnCloneableClass> myLocation = new TmfLocation<MyUnCloneableClass>(myClass);
- myLocation.clone();
- fail("clone()");
- }
- catch (InternalError e) {
- // Success
- }
- }
+ assertEquals("toString", expected1, location1.toString());
+ assertEquals("toString", expected2, location2.toString());
+ assertEquals("toString", expected3, location3.toString());
+ }
}
/*******************************************************************************
- * 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
*
* Contributors:
* Francois Chouinard - Initial API and implementation
+ * Francois Chouinard - Updated as per TMF Trace Model 1.0
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.trace;
/**
* <b><u>TmfLocation</u></b>
* <p>
- * A generic implementation of ITmfLocation
+ * A convenience implementation on of ITmfLocation. The generic class (L) must
+ * be comparable.
*/
-@SuppressWarnings("rawtypes")
-public class TmfLocation<L extends Comparable> implements ITmfLocation<L> {
-
- private L fLocation;
-
- @SuppressWarnings("unused")
- private TmfLocation() {
- }
-
- public TmfLocation(L location) {
- fLocation = location;
- }
-
- public TmfLocation(TmfLocation<L> other) {
- if (other == null)
- throw new IllegalArgumentException();
- fLocation = other.fLocation;
- }
-
- @Override
- public void setLocation(L location) {
- fLocation = location;
- }
-
- @Override
- public L getLocation() {
- return fLocation;
- }
-
- // ------------------------------------------------------------------------
+public class TmfLocation<L extends Comparable<L>> implements ITmfLocation<L> {
+
+ // ------------------------------------------------------------------------
+ // Constants
+ // ------------------------------------------------------------------------
+
+ static public final TmfLocation<Boolean> NULL_LOCATION = new TmfLocation<Boolean>();
+
+ // ------------------------------------------------------------------------
+ // Attributes
+ // ------------------------------------------------------------------------
+
+ private L fLocation;
+
+ // ------------------------------------------------------------------------
+ // Constructors
+ // ------------------------------------------------------------------------
+
+ /**
+ * Default constructor (for the 'null' location)
+ */
+ private TmfLocation() {
+ fLocation = null;
+ }
+
+ /**
+ * Standard constructor.
+ *
+ * @param location the trace location
+ */
+ public TmfLocation(L location) {
+ fLocation = location;
+ }
+
+ /**
+ * Copy constructor
+ *
+ * @param other the original location
+ */
+ public TmfLocation(TmfLocation<L> location) {
+ fLocation = location.fLocation;
+ }
+
+ // ------------------------------------------------------------------------
+ // Getters
+ // ------------------------------------------------------------------------
+
+ /* (non-Javadoc)
+ * @see org.eclipse.linuxtools.tmf.core.trace.ITmfLocation#getLocation()
+ */
+ @Override
+ public L getLocation() {
+ return fLocation;
+ }
+
+ // ------------------------------------------------------------------------
+ // Cloneable
+ // ------------------------------------------------------------------------
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ public TmfLocation<L> clone() {
+ TmfLocation<L> clone = null;
+ try {
+ clone = (TmfLocation<L>) super.clone();
+ if (fLocation != null) {
+ Class<?> clazz = fLocation.getClass();
+ Method method = clazz.getMethod("clone", new Class[0]);
+ Object copy = method.invoke(this.fLocation, new Object[0]);
+ clone.fLocation = (L) copy;
+ } else {
+ clone.fLocation = null;
+ }
+ } catch (CloneNotSupportedException e) {
+ } catch (NoSuchMethodException e) {
+ } catch (Exception e) {
+ throw new InternalError(e.toString());
+ }
+ return clone;
+ }
+
+ // ------------------------------------------------------------------------
// Object
// ------------------------------------------------------------------------
- @Override
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
public int hashCode() {
- if (fLocation == null)
- return -1;
- return fLocation.hashCode();
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((fLocation != null) ? fLocation.hashCode() : 0);
+ return result;
}
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
@Override
- public boolean equals(Object other) {
- if (!(other instanceof TmfLocation<?>))
- return false;
- TmfLocation<?> o = (TmfLocation<?>) other;
- if (fLocation == null)
- return (o.fLocation == null);
- return fLocation.equals(o.fLocation);
+ @SuppressWarnings("unchecked")
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ TmfLocation<L> other = (TmfLocation<L>) obj;
+ if (fLocation == null) {
+ if (other.fLocation != null)
+ return false;
+ } else if (!fLocation.equals(other.fLocation))
+ return false;
+ return true;
}
- @Override
- @SuppressWarnings("nls")
- public String toString() {
- if (fLocation == null)
- return "null";
- return fLocation.toString();
- }
-
- @Override
- @SuppressWarnings({ "nls", "unchecked" })
- public TmfLocation<L> clone() {
- TmfLocation<L> clone = null;
- try {
- clone = (TmfLocation<L>) super.clone();
- if (this.fLocation != null) {
- Class<?> clazz = this.fLocation.getClass();
- Method method = clazz.getMethod("clone", new Class[0]);
- Object duplic = method.invoke(this.fLocation, new Object[0]);
- clone.fLocation = (L) duplic;
- }
- } catch (NoSuchMethodException e) {
- // exception suppressed
- } catch (Exception e) {
- throw new InternalError(e.toString());
- }
- return clone;
- }
+ @Override
+ public String toString() {
+ return "TmfLocation [fLocation=" + fLocation + "]";
+ }
}