Path: blob/master/test/langtools/tools/lib/builder/AbstractBuilder.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package builder;2324import java.io.PrintWriter;25import java.io.StringWriter;26import java.util.Collections;27import java.util.List;2829public abstract class AbstractBuilder {3031final String name;3233Modifiers modifiers;34Comment comments;35String cname;3637/**38* Constructs the base builder.39* @param modifiers for the class40* @param name of the element41*/42public AbstractBuilder(Modifiers modifiers, String name) {43this.modifiers = modifiers;44this.name = name;45this.comments = new Comment(Comment.Kind.AUTO);46}4748AbstractBuilder setModifiers(String... mods) {49this.modifiers = new Modifiers(mods);50return this;51}5253/**54* Sets the enclosing type's name.55* @param className the enclosing type's name56*/57void setClassName(String className) {58this.cname = className;59}6061/**62* Sets the comment type for the member.63* @param comment for the member.64* @return this builder.65*/66public AbstractBuilder setComments(Comment comment) {67this.comments = comment;68return this;69}7071/**72* Sets the comments for the member.73* @param comments for the member.74* @return this builder.75*/76public AbstractBuilder setComments(String... comments) {77this.comments = new Comment(comments);78return this;79}8081/**82* Sets a comment for the element. Typically used to set83* user's preferences whether an automatic comment is84* required or no API comment.85*86* @param kind of comment, automatic or no comment.87* @return this builder.88*/89public AbstractBuilder setComments(Comment.Kind kind) {90switch (kind) {91case NO_API_COMMENT: case AUTO: case INHERIT_DOC:92this.comments = new Comment(kind);93break;94default:95throw new IllegalArgumentException(kind + " not allowed");96}97return this;98}99100/**101* The comment container.102*/103public static class Comment {104105/**106* The kinds of a comment.107*/108public enum Kind {109/**110* user specified111*/112USER,113/**114* no API comments115*/116NO_API_COMMENT,117/**118* inserts the javadoc tag119*/120INHERIT_DOC,121/**122* auto generate one123*/124AUTO125}126127final Kind kind;128final List<String> comments;129130/**131* Construct an initial comment.132*133* @param kind134*/135public Comment(Kind kind) {136this.kind = kind;137comments = Collections.emptyList();138}139140/**141* Specify a user comment.142*143* @param comments the string of API comments.144*/145public Comment(String... comments) {146kind = Kind.USER;147this.comments = comments == null148? Collections.emptyList()149: List.of(comments);150}151152@Override153public String toString() {154ClassBuilder.OutputWriter ow = new ClassBuilder.OutputWriter();155switch (kind) {156case USER:157comments.forEach((s) -> ow.println(" " + s));158break;159case INHERIT_DOC:160ow.println("{@inheritDoc}");161break;162}163return ow.toString();164}165}166167/**168* The modifier representation for an element.169*/170public static class Modifiers {171List<String> modifiers;172173/**174* Constructs a modifier container.175* @param modifiers for an element.176*/177public Modifiers(String... modifiers) {178this.modifiers = List.of(modifiers);179}180181/**182* Constructs a modifier container.183* @param modifiers a list of modifier strings.184*/185public Modifiers(List<String> modifiers) {186this.modifiers = modifiers;187}188189/**190* Sets the modifiers for this element.191* @param modifiers192*/193public void setModifiers(String... modifiers) {194this.modifiers = List.of(modifiers);195}196197/**198* Sets the modifiers for this element.199* @param modifiers200*/201public void setModifiers(List<String> modifiers) {202this.modifiers = modifiers;203}204205@Override206public String toString() {207OutputWriter ow = new OutputWriter();208modifiers.forEach(i -> ow.print(i + " "));209return ow.toString();210}211}212213/**214* The output writer.215*/216public static class OutputWriter {217private final StringWriter sw = new StringWriter();218private final PrintWriter pw = new PrintWriter(sw);219220@Override221public String toString() {222return sw.getBuffer().toString();223}224225/**226* Prints a string without NL.227* @param s the string to print.228*/229public void print(String s) {230pw.print(s);231}232233/**234* Prints a string with a NL.235* @param s the string to print.236*/237public void println(String s) {238pw.println(s);239}240}241242/**243* A container to encapsulate a pair of values.244*/245public static class Pair {246final String first;247final String second;248249public Pair(String first, String second) {250this.first = first;251this.second = second;252}253}254}255256257