001package org.eclipse.aether;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022/**
023 * A trace of nested requests that are performed by the repository system. This trace information can be used to
024 * correlate repository events with higher level operations in the application code that eventually caused the events. A
025 * single trace can carry an arbitrary object as data which is meant to describe a request/operation that is currently
026 * executed. For call hierarchies within the repository system itself, this data will usually be the {@code *Request}
027 * object that is currently processed. When invoking methods on the repository system, client code may provide a request
028 * trace that has been prepopulated with whatever data is useful for the application to indicate its state for later
029 * evaluation when processing the repository events.
030 * 
031 * @see RepositoryEvent#getTrace()
032 */
033public class RequestTrace
034{
035
036    private final RequestTrace parent;
037
038    private final Object data;
039
040    /**
041     * Creates a child of the specified request trace. This method is basically a convenience that will invoke
042     * {@link RequestTrace#newChild(Object) parent.newChild()} when the specified parent trace is not {@code null} or
043     * otherwise instantiante a new root trace.
044     * 
045     * @param parent The parent request trace, may be {@code null}.
046     * @param data The data to associate with the child trace, may be {@code null}.
047     * @return The child trace, never {@code null}.
048     */
049    public static RequestTrace newChild( RequestTrace parent, Object data )
050    {
051        if ( parent == null )
052        {
053            return new RequestTrace( data );
054        }
055        return parent.newChild( data );
056    }
057
058    /**
059     * Creates a new root trace with the specified data.
060     * 
061     * @param data The data to associate with the trace, may be {@code null}.
062     */
063    public RequestTrace( Object data )
064    {
065        this( null, data );
066    }
067
068    /**
069     * Creates a new trace with the specified data and parent
070     * 
071     * @param parent The parent trace, may be {@code null} for a root trace.
072     * @param data The data to associate with the trace, may be {@code null}.
073     */
074    protected RequestTrace( RequestTrace parent, Object data )
075    {
076        this.parent = parent;
077        this.data = data;
078    }
079
080    /**
081     * Gets the data associated with this trace.
082     * 
083     * @return The data associated with this trace or {@code null} if none.
084     */
085    public final Object getData()
086    {
087        return data;
088    }
089
090    /**
091     * Gets the parent of this trace.
092     * 
093     * @return The parent of this trace or {@code null} if this is the root of the trace stack.
094     */
095    public final RequestTrace getParent()
096    {
097        return parent;
098    }
099
100    /**
101     * Creates a new child of this trace.
102     * 
103     * @param data The data to associate with the child, may be {@code null}.
104     * @return The child trace, never {@code null}.
105     */
106    public RequestTrace newChild( Object data )
107    {
108        return new RequestTrace( this, data );
109    }
110
111    @Override
112    public String toString()
113    {
114        return String.valueOf( getData() );
115    }
116
117}