8240137: Support chained use of Content.add

Reviewed-by: hannesw
This commit is contained in:
Jonathan Gibbons 2020-03-06 18:03:09 -08:00
parent c203cebcdf
commit c46623d071
11 changed files with 65 additions and 137 deletions

@ -1357,8 +1357,8 @@ public class HtmlDocletWriter {
{
final Content result = new ContentBuilder() {
@Override
public void add(CharSequence text) {
super.add(utils.normalizeNewlines(text));
public ContentBuilder add(CharSequence text) {
return super.add(utils.normalizeNewlines(text));
}
};
CommentHelper ch = utils.getCommentHelper(element);

@ -52,28 +52,6 @@ public class Comment extends Content {
commentText = nullCheck(comment);
}
/**
* This method is not supported by the class.
*
* @param content content that needs to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
/**
* This method is not supported by the class.
*
* @param stringContent string content that needs to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(CharSequence stringContent) {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
return commentText.isEmpty();

@ -48,28 +48,30 @@ public class ContentBuilder extends Content {
}
@Override
public void add(Content content) {
public ContentBuilder add(Content content) {
nullCheck(content);
ensureMutableContents();
if (content instanceof ContentBuilder) {
contents.addAll(((ContentBuilder) content).contents);
} else
contents.add(content);
return this;
}
@Override
public void add(CharSequence text) {
if (text.length() == 0)
return;
ensureMutableContents();
Content c = contents.isEmpty() ? null : contents.get(contents.size() - 1);
StringContent sc;
if (c != null && c instanceof StringContent) {
sc = (StringContent) c;
} else {
contents.add(sc = new StringContent());
public ContentBuilder add(CharSequence text) {
if (text.length() > 0) {
ensureMutableContents();
Content c = contents.isEmpty() ? null : contents.get(contents.size() - 1);
StringContent sc;
if (c != null && c instanceof StringContent) {
sc = (StringContent) c;
} else {
contents.add(sc = new StringContent());
}
sc.add(text);
}
sc.add(text);
return this;
}
@Override

@ -51,16 +51,6 @@ public class Entity extends Content {
this.text = text;
}
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
@Override
public void add(CharSequence stringContent) {
throw new UnsupportedOperationException();
}
@Override
public boolean write(Writer writer, boolean atNewline) throws IOException {
writer.write(text);

@ -50,29 +50,6 @@ public class FixedStringContent extends Content {
string = Entity.escapeHtmlChars(content);
}
/**
* This method is not supported by the class.
*
* @param content content that needs to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
/**
* Adds content for the StringContent object. The method escapes
* HTML characters for the string content that is added.
*
* @param strContent string content to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(CharSequence strContent) {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
return string.isEmpty();

@ -150,20 +150,19 @@ public class HtmlTree extends Content {
/**
* Adds content for the HTML tag.
*
* @param tagContent tag content to be added
* @param content content to be added
*/
@Override
public void add(Content tagContent) {
if (tagContent instanceof ContentBuilder) {
for (Content c: ((ContentBuilder)tagContent).contents) {
add(c);
}
public HtmlTree add(Content content) {
if (content instanceof ContentBuilder) {
((ContentBuilder) content).contents.forEach(this::add);
}
else if (tagContent == HtmlTree.EMPTY || tagContent.isValid()) {
if (content.isEmpty())
content = new ArrayList<>();
content.add(tagContent);
else if (content == HtmlTree.EMPTY || content.isValid()) {
if (this.content.isEmpty())
this.content = new ArrayList<>();
this.content.add(content);
}
return this;
}
/**
@ -174,16 +173,19 @@ public class HtmlTree extends Content {
* @param stringContent string content that needs to be added
*/
@Override
public void add(CharSequence stringContent) {
public HtmlTree add(CharSequence stringContent) {
if (!content.isEmpty()) {
Content lastContent = content.get(content.size() - 1);
if (lastContent instanceof StringContent)
lastContent.add(stringContent);
else
else {
add(new StringContent(stringContent));
}
}
else
else {
add(new StringContent(stringContent));
}
return this;
}
@Override

@ -52,28 +52,6 @@ public class RawHtml extends Content {
rawHtmlContent = rawHtml.toString();
}
/**
* This method is not supported by the class.
*
* @param content content that needs to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
/**
* This method is not supported by the class.
*
* @param stringContent string content that needs to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(CharSequence stringContent) {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
return rawHtmlContent.isEmpty();

@ -108,15 +108,11 @@ public class Script {
ScriptContent scriptContent = new ScriptContent(sb);
HtmlTree tree = new HtmlTree(HtmlTag.SCRIPT) {
@Override
public void add(CharSequence s) {
throw new UnsupportedOperationException();
}
@Override
public void add(Content c) {
public HtmlTree add(Content c) {
if (c != scriptContent) {
throw new IllegalArgumentException();
}
super.add(scriptContent);
return super.add(scriptContent);
}
};
tree.put(HtmlAttr.TYPE, "text/javascript");
@ -200,13 +196,9 @@ public class Script {
}
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
@Override
public void add(CharSequence code) {
public ScriptContent add(CharSequence code) {
sb.append(code);
return this;
}
@Override

@ -60,17 +60,6 @@ public class StringContent extends Content {
Entity.escapeHtmlChars(initialContent, stringContent);
}
/**
* This method is not supported by the class.
*
* @param content content that needs to be added
* @throws UnsupportedOperationException always
*/
@Override
public void add(Content content) {
throw new UnsupportedOperationException();
}
/**
* Adds content for the StringContent object. The method escapes
* HTML characters for the string content that is added.
@ -78,8 +67,9 @@ public class StringContent extends Content {
* @param strContent string content to be added
*/
@Override
public void add(CharSequence strContent) {
public StringContent add(CharSequence strContent) {
Entity.escapeHtmlChars(strContent, stringContent);
return this;
}
@Override

@ -288,7 +288,7 @@ public class Table {
}
/**
* Add a row of data to the table.
* Adds a row of data to the table.
* Each item of content should be suitable for use as the content of a
* {@code <th>} or {@code <td>} cell.
* This method should not be used when the table has tabs: use a method
@ -301,7 +301,7 @@ public class Table {
}
/**
* Add a row of data to the table.
* Adds a row of data to the table.
* Each item of content should be suitable for use as the content of a
* {@code <th>} or {@code <td> cell}.
* This method should not be used when the table has tabs: use a method
@ -314,14 +314,14 @@ public class Table {
}
/**
* Add a row of data to the table.
* Adds a row of data to the table.
* Each item of content should be suitable for use as the content of a
* {@code <th>} or {@code <td>} cell.
*
* If tabs have been added to the table, the specified element will be used
* to determine whether the row should be displayed when any particular tab
* is selected, using the predicate specified when the tab was
* {@link #add(String,Predicate) added}.
* {@link #addTab(String,Predicate) added}.
*
* @param element the element
* @param contents the contents for the row
@ -333,14 +333,14 @@ public class Table {
}
/**
* Add a row of data to the table.
* Adds a row of data to the table.
* Each item of content should be suitable for use as the content of a
* {@code <th>} or {@code <td>} cell.
*
* If tabs have been added to the table, the specified element will be used
* to determine whether the row should be displayed when any particular tab
* is selected, using the predicate specified when the tab was
* {@link #add(String,Predicate) added}.
* {@link #addTab(String,Predicate) added}.
*
* @param element the element
* @param contents the contents for the row

@ -59,17 +59,36 @@ public abstract class Content {
/**
* Adds content to the existing content.
* This is an optional operation.
*
* @param content content that needs to be added
* @implSpec This implementation throws {@linkplain UnsupportedOperationException}.
*
* @param content content to be added
* @return this object
* @throws UnsupportedOperationException if this operation is not supported by
* a particular implementation
* @throws IllegalArgumentException if the content is not suitable to be added
*/
public abstract void add(Content content);
public Content add(Content content) {
throw new UnsupportedOperationException();
}
/**
* Adds a string content to the existing content.
* This is an optional operation.
*
* @implSpec
* This implementation throws {@linkplain UnsupportedOperationException}.
*
* @param stringContent the string content to be added
* @return this object
* @throws UnsupportedOperationException if this operation is not supported by
* a particular implementation
* @throws IllegalArgumentException if the content is not suitable to be added
*/
public abstract void add(CharSequence stringContent);
public Content add(CharSequence stringContent) {
throw new UnsupportedOperationException();
}
/**
* Writes content to a writer.