~ubuntu-branches/ubuntu/trusty/ivy/trusty

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/circular/CircularDependencyHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 *  contributor license agreements.  See the NOTICE file distributed with
 
4
 *  this work for additional information regarding copyright ownership.
 
5
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 *  (the "License"); you may not use this file except in compliance with
 
7
 *  the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 *  Unless required by applicable law or agreed to in writing, software
 
12
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
13
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 *  See the License for the specific language governing permissions and
 
15
 *  limitations under the License.
 
16
 *
 
17
 */
 
18
package org.apache.ivy.plugins.circular;
 
19
 
 
20
import java.util.HashSet;
 
21
import java.util.Iterator;
 
22
import java.util.List;
 
23
import java.util.Set;
 
24
 
 
25
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
 
26
import org.apache.ivy.core.module.id.ModuleRevisionId;
 
27
 
 
28
public final class CircularDependencyHelper {
 
29
 
 
30
    /** CircularDependencyHelper is not designed to be an instance */
 
31
    private CircularDependencyHelper() {
 
32
        
 
33
    }
 
34
    
 
35
    /**
 
36
     * Returns a string representation of this circular dependency graph
 
37
     * 
 
38
     * @param descriptors
 
39
     *            in order of circular dependency
 
40
     * @return a string representation of this circular dependency graph
 
41
     */
 
42
    public static String formatMessage(final ModuleRevisionId[] mrids) {
 
43
        Set alreadyAdded = new HashSet();
 
44
        StringBuffer buff = new StringBuffer();
 
45
        buff.append(mrids[0]);
 
46
        alreadyAdded.add(mrids[0]);
 
47
        for (int i = 1; i < mrids.length; i++) {
 
48
            buff.append("->");
 
49
            if (alreadyAdded.add(mrids[i])) {
 
50
                buff.append(mrids[i]);
 
51
            } else {
 
52
                buff.append("...");
 
53
                break;
 
54
            }
 
55
        }
 
56
        return buff.toString();
 
57
    }
 
58
 
 
59
    public static String formatMessage(final ModuleDescriptor[] descriptors) {
 
60
        return formatMessage(toMrids(descriptors));
 
61
    }
 
62
 
 
63
    /**
 
64
     * @param loopElements
 
65
     *            a List<ModuleDescriptor>
 
66
     */
 
67
    public static String formatMessageFromDescriptors(List loopElements) {
 
68
        ModuleRevisionId[] mrids = new ModuleRevisionId[loopElements.size()];
 
69
        int pos = 0;
 
70
        for (Iterator it = loopElements.iterator(); it.hasNext();) {
 
71
            ModuleDescriptor descriptor = (ModuleDescriptor) it.next();
 
72
            mrids[pos] = descriptor.getModuleRevisionId();
 
73
            pos++;
 
74
        }
 
75
        return formatMessage(mrids);
 
76
    }
 
77
 
 
78
    public static ModuleRevisionId[] toMrids(ModuleDescriptor[] descriptors) {
 
79
        ModuleRevisionId[] mrids = new ModuleRevisionId[descriptors.length];
 
80
        for (int i = 0; i < descriptors.length; i++) {
 
81
            mrids[i] = descriptors[i].getModuleRevisionId();
 
82
        }
 
83
        return mrids;
 
84
    }
 
85
 
 
86
}