ContextId.java
package org.microspace.thread;
import java.io.Serializable;
/**
* ContextId can be used to create threads within threads and having another transaction.
* <p>
* ContextId is hierarchical, the lowest element is the actual thread.
*
* @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
* @version 2016-06-26
*/
public final class ContextId implements Serializable {
private static final long serialVersionUID = 4136356825074105484L;
final String delegate;
public ContextId () {
delegate = "" + Thread.currentThread().getId();
}
public ContextId (int id) {
delegate = "" + Thread.currentThread().getId() + "." + id;
}
public ContextId(String str) {
this.delegate = "" + Thread.currentThread().getId() + "." + str;
}
private ContextId(String str, boolean skipThreadId) {
if (skipThreadId) {
this.delegate = str;
} else {
this.delegate = "" + Thread.currentThread().getId() + "." + str;
}
}
public ContextId extend (ContextId contextId) {
return new ContextId (delegate + "." + contextId.delegate, true);
}
@Override
public boolean equals (Object o) {
if (!(o instanceof ContextId))return false;
ContextId anotherContextId = (ContextId) o;
return delegate.equals(anotherContextId.delegate);
}
@Override
public int hashCode () {
return delegate.hashCode();
}
@Override
public String toString () {
return delegate;
}
}