Refactor ITmfLocation and fix dependencies
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfLocation.java
index 55c5a8a8d2235a28dad68126c9d212230b59b1f8..a9e2947d5a4a060483dd9b5aa8ad0aabe73c3179 100644 (file)
@@ -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
@@ -8,6 +8,7 @@
  * 
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Francois Chouinard - Updated as per TMF Trace Model 1.0
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
@@ -17,84 +18,132 @@ import java.lang.reflect.Method;
 /**
  * <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 + "]";
+    }
 
 }
This page took 0.026845 seconds and 5 git commands to generate.