CPD Results

The following document contains the results of PMD's CPD 4.2.5.

Duplications

FileLine
org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java33
org/apache/maven/shared/dependency/tree/traversal/SerializingDependencyNodeVisitor.java35
public class SerializingDependencyNodeVisitor
    implements DependencyNodeVisitor
{
    // classes ----------------------------------------------------------------

    /**
     * Provides tokens to use when serializing the dependency tree.
     */
    public static class TreeTokens
    {
        private final String nodeIndent;

        private final String lastNodeIndent;

        private final String fillIndent;

        private final String lastFillIndent;

        public TreeTokens( String nodeIndent, String lastNodeIndent, String fillIndent, String lastFillIndent )
        {
            this.nodeIndent = nodeIndent;
            this.lastNodeIndent = lastNodeIndent;
            this.fillIndent = fillIndent;
            this.lastFillIndent = lastFillIndent;
        }

        public String getNodeIndent( boolean last )
        {
            return last ? lastNodeIndent : nodeIndent;
        }

        public String getFillIndent( boolean last )
        {
            return last ? lastFillIndent : fillIndent;
        }
    }

    // constants --------------------------------------------------------------

    /**
     * Whitespace tokens to use when outputing the dependency tree.
     */
    public static final TreeTokens WHITESPACE_TOKENS = new TreeTokens( "   ", "   ", "   ", "   " );

    /**
     * The standard ASCII tokens to use when outputing the dependency tree.
     */
    public static final TreeTokens STANDARD_TOKENS = new TreeTokens( "+- ", "\\- ", "|  ", "   " );

    /**
     * The extended ASCII tokens to use when outputing the dependency tree.
     */
    public static final TreeTokens EXTENDED_TOKENS =
        new TreeTokens( "\u00c3\u00c4 ", "\u00c0\u00c4 ", "\u00b3  ", "   " );

    // fields -----------------------------------------------------------------

    /**
     * The writer to serialize to.
     */
    private final PrintWriter writer;

    /**
     * The tokens to use when serializing the dependency tree.
     */
    private final TreeTokens tokens;

    /**
     * The depth of the currently visited dependency node.
     */
    private int depth;

    // constructors -----------------------------------------------------------

    /**
     * Creates a dependency node visitor that serializes visited nodes to the specified writer using whitespace tokens.
     * 
     * @param writer
     *            the writer to serialize to
     */
    public SerializingDependencyNodeVisitor( Writer writer )
    {
        this( writer, WHITESPACE_TOKENS );
    }

    /**
     * Creates a dependency node visitor that serializes visited nodes to the specified writer using the specified
     * tokens.
     * 
     * @param writer
     *            the writer to serialize to
     * @param tokens
     *            the tokens to use when serializing the dependency tree
     */
    public SerializingDependencyNodeVisitor( Writer writer, TreeTokens tokens )
    {
        if ( writer instanceof PrintWriter )
        {
            this.writer = (PrintWriter) writer;
        }
        else
        {
            this.writer = new PrintWriter( writer, true );
        }

        this.tokens = tokens;

        depth = 0;
    }

    // DependencyNodeVisitor methods ------------------------------------------

    /**
     * {@inheritDoc}
     */
    public boolean visit( DependencyNode node )
    {
        indent( node );

        writer.println( node.toNodeString() );

        depth++;

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public boolean endVisit( DependencyNode node )
    {
        depth--;

        return true;
    }

    // private methods --------------------------------------------------------

    /**
     * Writes the necessary tokens to indent the specified dependency node to this visitor's writer.
     * 
     * @param node
     *            the dependency node to indent
     */
    private void indent( DependencyNode node )
    {
        for ( int i = 1; i < depth; i++ )
        {
            writer.write( tokens.getFillIndent( isLast( node, i ) ) );
        }

        if ( depth > 0 )
        {
            writer.write( tokens.getNodeIndent( isLast( node ) ) );
        }
    }

    /**
     * Gets whether the specified dependency node is the last of its siblings.
     * 
     * @param node
     *            the dependency node to check
     * @return <code>true</code> if the specified dependency node is the last of its last siblings
     */
    private boolean isLast( DependencyNode node )
    {
        // TODO: remove node argument and calculate from visitor calls only
        
        DependencyNode parent = node.getParent();

        boolean last;

        if ( parent == null )
        {
            last = true;
        }
        else
        {
            List<DependencyNode> siblings = parent.getChildren();

            last = ( siblings.indexOf( node ) == siblings.size() - 1 );
        }

        return last;
    }

    /**
     * Gets whether the specified dependency node ancestor is the last of its siblings.
     * 
     * @param node
     *            the dependency node whose ancestor to check
     * @param ancestorDepth
     *            the depth of the ancestor of the specified dependency node to check
     * @return <code>true</code> if the specified dependency node ancestor is the last of its siblings
     */
    private boolean isLast( DependencyNode node, int ancestorDepth )
    {
        // TODO: remove node argument and calculate from visitor calls only
        
        int distance = depth - ancestorDepth;

        while ( distance-- > 0 )
        {
            node = node.getParent();
        }

        return isLast( node );
    }
}
FileLine
org/apache/maven/shared/dependency/graph/traversal/FilteringDependencyNodeVisitor.java32
org/apache/maven/shared/dependency/tree/traversal/FilteringDependencyNodeVisitor.java32
public class FilteringDependencyNodeVisitor
    implements DependencyNodeVisitor
{
    // fields -----------------------------------------------------------------

    /**
     * The dependency node visitor to delegate to.
     */
    private final DependencyNodeVisitor visitor;

    /**
     * The dependency node filter to apply before delegation.
     */
    private final DependencyNodeFilter filter;

    // constructors -----------------------------------------------------------

    /**
     * Creates a dependency node visitor that delegates nodes that are accepted by the specified filter to the specified
     * visitor.
     * 
     * @param visitor
     *            the dependency node visitor to delegate to
     * @param filter
     *            the dependency node filter to apply before delegation
     */
    public FilteringDependencyNodeVisitor( DependencyNodeVisitor visitor, DependencyNodeFilter filter )
    {
        this.visitor = visitor;
        this.filter = filter;
    }

    // DependencyNodeVisitor methods ------------------------------------------

    /**
     * {@inheritDoc}
     */
    public boolean visit( DependencyNode node )
    {
        boolean visit;

        if ( filter.accept( node ) )
        {
            visit = visitor.visit( node );
        }
        else
        {
            visit = true;
        }

        return visit;
    }

    /**
     * {@inheritDoc}
     */
    public boolean endVisit( DependencyNode node )
    {
        boolean visit;

        if ( filter.accept( node ) )
        {
            visit = visitor.endVisit( node );
        }
        else
        {
            visit = true;
        }

        return visit;
    }

    // public methods ---------------------------------------------------------

    /**
     * Gets the dependency node visitor that this visitor delegates to.
     * 
     * @return the dependency node visitor
     */
    public DependencyNodeVisitor getDependencyNodeVisitor()
    {
        return visitor;
    }

    /**
     * Gets the dependency node filter that this visitor applies before delegation.
     * 
     * @return the dependency node filter
     */
    public DependencyNodeFilter getDependencyNodeFilter()
    {
        return filter;
    }
}
FileLine
org/apache/maven/shared/dependency/graph/filter/AncestorOrSelfDependencyNodeFilter.java34
org/apache/maven/shared/dependency/tree/filter/AncestorOrSelfDependencyNodeFilter.java34
public class AncestorOrSelfDependencyNodeFilter
    implements DependencyNodeFilter
{
    // fields -----------------------------------------------------------------

    /**
     * The list of nodes that this filter accepts ancestors-or-self of.
     */
    private final List<DependencyNode> descendantNodes;

    // constructors -----------------------------------------------------------

    public AncestorOrSelfDependencyNodeFilter( DependencyNode descendantNode )
    {
        this( Collections.singletonList( descendantNode ) );
    }

    /**
     * Creates a dependency node filter that only accepts nodes that are ancestors of, or equal to, the specified list
     * of nodes.
     * 
     * @param descendantNodes
     *            the list of nodes to accept ancestors-or-self of
     */
    public AncestorOrSelfDependencyNodeFilter( List<DependencyNode> descendantNodes )
    {
        this.descendantNodes = descendantNodes;
    }

    // DependencyNodeFilter methods -------------------------------------------

    /**
     * {@inheritDoc}
     */
    public boolean accept( DependencyNode node )
    {
        for ( DependencyNode descendantNode : descendantNodes )
        {
            if ( isAncestorOrSelf( node, descendantNode ) )
            {
                return true;
            }
        }

        return false;
    }

    // private methods --------------------------------------------------------

    /**
     * Gets whether the first dependency node is an ancestor-or-self of the second.
     * 
     * @param ancestorNode
     *            the ancestor-or-self dependency node
     * @param descendantNode
     *            the dependency node to test
     * @return <code>true</code> if <code>ancestorNode</code> is an ancestor, or equal to,
     *         <code>descendantNode</code>
     */
    private boolean isAncestorOrSelf( DependencyNode ancestorNode, DependencyNode descendantNode )
    {
        boolean ancestor = false;

        while ( !ancestor && descendantNode != null )
        {
            ancestor = ancestorNode.equals( descendantNode );

            descendantNode = descendantNode.getParent();
        }

        return ancestor;
    }
}
FileLine
org/apache/maven/shared/dependency/graph/filter/AndDependencyNodeFilter.java35
org/apache/maven/shared/dependency/tree/filter/AndDependencyNodeFilter.java35
public class AndDependencyNodeFilter
    implements DependencyNodeFilter
{
    // fields -----------------------------------------------------------------

    /**
     * The dependency node filters that this filter ANDs together.
     */
    private final List<DependencyNodeFilter> filters;

    // constructors -----------------------------------------------------------

    /**
     * Creates a dependency node filter that logically ANDs together the two specified dependency node filters.
     * 
     * @param filter1
     *            the first dependency node filter to logically AND together
     * @param filter2
     *            the second dependency node filter to logically AND together
     */
    public AndDependencyNodeFilter( DependencyNodeFilter filter1, DependencyNodeFilter filter2 )
    {
        this( Arrays.asList( new DependencyNodeFilter[] { filter1, filter2 } ) );
    }

    /**
     * Creates a dependency node filter that logically ANDs together the specified dependency node filters.
     * 
     * @param filters
     *            the list of dependency node filters to logically AND together
     */
    public AndDependencyNodeFilter( List<DependencyNodeFilter> filters )
    {
        this.filters = Collections.unmodifiableList( filters );
    }

    // DependencyNodeFilter methods -------------------------------------------

    /**
     * {@inheritDoc}
     */
    public boolean accept( DependencyNode node )
    {
        for ( DependencyNodeFilter filter : filters )
        {
            if ( !filter.accept( node ) )
            {
                return false;
            }
        }

        return true;
    }

    // public methods ---------------------------------------------------------

    /**
     * Gets the list of dependency node filters that this filter ANDs together.
     * 
     * @return the dependency node filters that this filter ANDs together
     */
    public List<DependencyNodeFilter> getDependencyNodeFilters()
    {
        return filters;
    }
}