01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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(
60: final ModuleDescriptor[] descriptors) {
61: return formatMessage(toMrids(descriptors));
62: }
63:
64: /**
65: * @param loopElements
66: * a List<ModuleDescriptor>
67: */
68: public static String formatMessageFromDescriptors(List loopElements) {
69: ModuleRevisionId[] mrids = new ModuleRevisionId[loopElements
70: .size()];
71: int pos = 0;
72: for (Iterator it = loopElements.iterator(); it.hasNext();) {
73: ModuleDescriptor descriptor = (ModuleDescriptor) it.next();
74: mrids[pos] = descriptor.getModuleRevisionId();
75: pos++;
76: }
77: return formatMessage(mrids);
78: }
79:
80: public static ModuleRevisionId[] toMrids(
81: ModuleDescriptor[] descriptors) {
82: ModuleRevisionId[] mrids = new ModuleRevisionId[descriptors.length];
83: for (int i = 0; i < descriptors.length; i++) {
84: mrids[i] = descriptors[i].getModuleRevisionId();
85: }
86: return mrids;
87: }
88:
89: }
|