1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.maven.api.services; 20 21 import org.apache.maven.api.annotations.Nonnull; 22 23 /** 24 * Message builder that supports configurable styling. 25 * 26 * @since 4.0.0 27 * @see MessageBuilderFactory 28 */ 29 public interface MessageBuilder extends Appendable { 30 31 /** 32 * Append message content in trace style. 33 * By default, bold magenta 34 * 35 * @param message the message to append 36 * @return the current builder 37 */ 38 @Nonnull 39 default MessageBuilder trace(Object message) { 40 return style(".trace:-bold,f:magenta", message); 41 } 42 43 /** 44 * Append message content in debug style. 45 * By default, bold cyan 46 * 47 * @param message the message to append 48 * @return the current builder 49 */ 50 @Nonnull 51 default MessageBuilder debug(Object message) { 52 return style(".debug:-bold,f:cyan", message); 53 } 54 55 /** 56 * Append message content in info style. 57 * By default, bold blue 58 * 59 * @param message the message to append 60 * @return the current builder 61 */ 62 @Nonnull 63 default MessageBuilder info(Object message) { 64 return style(".info:-bold,f:blue", message); 65 } 66 67 /** 68 * Append message content in warning style. 69 * By default, bold yellow 70 * 71 * @param message the message to append 72 * @return the current builder 73 */ 74 @Nonnull 75 default MessageBuilder warning(Object message) { 76 return style(".warning:-bold,f:yellow", message); 77 } 78 79 /** 80 * Append message content in error style. 81 * By default, bold red 82 * 83 * @param message the message to append 84 * @return the current builder 85 */ 86 @Nonnull 87 default MessageBuilder error(Object message) { 88 return style(".error:-bold,f:red", message); 89 } 90 91 /** 92 * Append message content in success style. 93 * By default, bold green 94 * 95 * @param message the message to append 96 * @return the current builder 97 */ 98 @Nonnull 99 default MessageBuilder success(Object message) { 100 return style(".success:-bold,f:green", message); 101 } 102 103 /** 104 * Append message content in failure style. 105 * By default, bold red 106 * 107 * @param message the message to append 108 * @return the current builder 109 */ 110 @Nonnull 111 default MessageBuilder failure(Object message) { 112 return style(".failure:-bold,f:red", message); 113 } 114 115 /** 116 * Append message content in strong style. 117 * By default, bold 118 * 119 * @param message the message to append 120 * @return the current builder 121 */ 122 @Nonnull 123 default MessageBuilder strong(Object message) { 124 return style(".strong:-bold", message); 125 } 126 127 /** 128 * Append message content in mojo style. 129 * By default, green 130 * 131 * @param message the message to append 132 * @return the current builder 133 */ 134 @Nonnull 135 default MessageBuilder mojo(Object message) { 136 return style(".mojo:-f:green", message); 137 } 138 139 /** 140 * Append message content in project style. 141 * By default, cyan 142 * 143 * @param message the message to append 144 * @return the current builder 145 */ 146 @Nonnull 147 default MessageBuilder project(Object message) { 148 return style(".project:-f:cyan", message); 149 } 150 151 @Nonnull 152 default MessageBuilder style(String style, Object message) { 153 return style(style).a(message).resetStyle(); 154 } 155 156 MessageBuilder style(String style); 157 158 MessageBuilder resetStyle(); 159 160 // 161 // message building methods modelled after Ansi methods 162 // 163 164 @Nonnull 165 @Override 166 MessageBuilder append(CharSequence cs); 167 168 @Nonnull 169 @Override 170 MessageBuilder append(CharSequence cs, int start, int end); 171 172 @Nonnull 173 @Override 174 MessageBuilder append(char c); 175 176 /** 177 * Append content to the message buffer. 178 * 179 * @param value the content to append 180 * @param offset the index of the first {@code char} to append 181 * @param len the number of {@code char}s to append 182 * @return the current builder 183 */ 184 @Nonnull 185 default MessageBuilder a(char[] value, int offset, int len) { 186 return append(String.valueOf(value, offset, len)); 187 } 188 189 /** 190 * Append content to the message buffer. 191 * 192 * @param value the content to append 193 * @return the current builder 194 */ 195 @Nonnull 196 default MessageBuilder a(char[] value) { 197 return append(String.valueOf(value)); 198 } 199 200 /** 201 * Append content to the message buffer. 202 * 203 * @param value the content to append 204 * @param start the starting index of the subsequence to be appended 205 * @param end the end index of the subsequence to be appended 206 * @return the current builder 207 */ 208 @Nonnull 209 default MessageBuilder a(CharSequence value, int start, int end) { 210 return append(value, start, end); 211 } 212 213 /** 214 * Append content to the message buffer. 215 * 216 * @param value the content to append 217 * @return the current builder 218 */ 219 @Nonnull 220 default MessageBuilder a(CharSequence value) { 221 return append(value); 222 } 223 224 /** 225 * Append content to the message buffer. 226 * 227 * @param value the content to append 228 * @return the current builder 229 */ 230 @Nonnull 231 default MessageBuilder a(Object value) { 232 return append(String.valueOf(value)); 233 } 234 235 /** 236 * Append newline to the message buffer. 237 * 238 * @return the current builder 239 */ 240 @Nonnull 241 default MessageBuilder newline() { 242 return append(System.lineSeparator()); 243 } 244 245 /** 246 * Append formatted content to the buffer. 247 * @see String#format(String, Object...) 248 * 249 * @param pattern a <a href="../util/Formatter.html#syntax">format string</a> 250 * @param args arguments referenced by the format specifiers in the format string 251 * @return the current builder 252 */ 253 @Nonnull 254 default MessageBuilder format(String pattern, Object... args) { 255 return append(String.format(pattern, args)); 256 } 257 258 /** 259 * Set the buffer length. 260 * 261 * @param length the new length 262 * @return the current builder 263 */ 264 MessageBuilder setLength(int length); 265 266 /** 267 * Return the built message. 268 * 269 * @return the message 270 */ 271 @Nonnull 272 String build(); 273 }