01: package com.sun.codemodel;
02:
03: import java.util.ArrayList;
04: import java.util.Collection;
05: import java.util.Iterator;
06:
07: /**
08: * A part is a part of a javadoc comment, and it is a list of values.
09: *
10: * <p>
11: * A part can contain a free-form text. This text is modeled as a collection of 'values'
12: * in this class. A value can be a {@link JType} (which will be prinited with a @link tag),
13: * anything that can be turned into a {@link String} via the {@link Object#toString()} method,
14: * or a {@link Collection}/array of those objects.
15: *
16: * <p>
17: * Values can be added through the various append methods one by one or in a bulk.
18: *
19: * @author Kohsuke Kawaguchi
20: */
21: public class JCommentPart extends ArrayList<Object> {
22: /**
23: * Appends a new value.
24: *
25: * If the value is {@link JType} it will be printed as a @link tag.
26: * Otherwise it will be converted to String via {@link Object#toString()}.
27: */
28: public JCommentPart append(Object o) {
29: add(o);
30: return this ;
31: }
32:
33: public boolean add(Object o) {
34: flattenAppend(o);
35: return true;
36: }
37:
38: private void flattenAppend(Object value) {
39: if (value == null)
40: return;
41: if (value instanceof Object[]) {
42: for (Object o : (Object[]) value)
43: flattenAppend(o);
44: } else if (value instanceof Collection) {
45: for (Object o : (Collection) value)
46: flattenAppend(o);
47: } else
48: super .add(value);
49: }
50:
51: /**
52: * Writes this part into the formatter by using the specified indentation.
53: */
54: protected void format(JFormatter f, String indent) {
55: if (!f.isPrinting()) {
56: // quickly pass the types to JFormatter
57: for (Object o : this )
58: if (o instanceof JClass)
59: f.g((JClass) o);
60: return;
61: }
62:
63: if (!isEmpty())
64: f.p(indent);
65:
66: Iterator itr = iterator();
67: while (itr.hasNext()) {
68: Object o = itr.next();
69:
70: if (o instanceof String) {
71: int idx;
72: String s = (String) o;
73: while ((idx = s.indexOf('\n')) != -1) {
74: String line = s.substring(0, idx);
75: if (line.length() > 0)
76: f.p(line);
77: s = s.substring(idx + 1);
78: f.nl().p(indent);
79: }
80: if (s.length() != 0)
81: f.p(s);
82: } else if (o instanceof JClass) {
83: // TODO: this doesn't print the parameterized type properly
84: ((JClass) o).printLink(f);
85: } else if (o instanceof JType) {
86: f.g((JType) o);
87: } else
88: throw new IllegalStateException();
89: }
90:
91: if (!isEmpty())
92: f.nl();
93: }
94: }
|