blob: 6c7f71ff55c6201679bb4b8f02ea751d05ac19b1 [file] [log] [blame]
Ray Milkey9a39eca2015-01-05 09:41:01 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey9a39eca2015-01-05 09:41:01 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.util;
17
18/**
19 * Allows slf4j style formatting of parameters into a string.
20 */
21public final class PositionalParameterStringFormatter {
22
23 /**
24 * Hide default constructor.
25 */
26 private PositionalParameterStringFormatter() {
27 }
28
29 /**
30 * Formats a string using slf4j style positional parameter replacement.
31 * Instances of "{}" in the source string are replaced in order by the
32 * specified parameter values as strings.
33 *
34 * @param source original string to format
35 * @param parameters list of parameters that will be substituted
36 * @return formatted string
37 */
38 public static String format(String source, Object... parameters) {
39 String current = source;
40 for (Object parameter : parameters) {
41 if (!current.contains("{}")) {
42 return current;
43 }
Yuta HIGUCHIc8ad76d2015-01-12 22:31:25 -080044 current = current.replaceFirst("\\{\\}", String.valueOf(parameter));
Ray Milkey9a39eca2015-01-05 09:41:01 -080045 }
46 return current;
47 }
48}