Java tutorial
/* * The MIT License (MIT) * Copyright (c) 2015 "Yukthi Techsoft Pvt. Ltd." (http://yukthi-tech.co.in) * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.yukthi.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; /** * Utility to format strings in log4j 2 way. * * {} will match with the current index argument. If index is greater than provided values then <undefined> string will be used. * {<idx>} can be used to refer to argument at particular index. Helpful in building messages which uses same argument multiple times. * * @author akiran */ public class MessageFormatter { /** * String that will be used when argument index is out of range */ public static final String UNDEFINED = "<undefined>"; /** * Param argument pattern */ private static final Pattern PARAM_PATTERN = Pattern.compile("\\{(\\d*)\\}"); /** * Replaces the args values in "message" using patterns mentioned below and same will be returned. * * {} will match with the current index argument. If index is greater than provided values then <undefined> string will be used. * {<idx>} can be used to refer to argument at particular index. Helpful in building messages which uses same argument multiple times. * * @param message Message string with expressions * @param args Values for expression * @return Formatted string */ public static String format(String message, Object... args) { //when message is null, return null if (message == null) { return null; } //when args is null, assume empty values if (args == null) { args = new Object[0]; } Matcher matcher = PARAM_PATTERN.matcher(message); StringBuffer buffer = new StringBuffer(); int loopIndex = 0; int argIndex = 0; Object arg = null; //loop through pattern matches while (matcher.find()) { //if index is mentioned in pattern if (StringUtils.isNotBlank(matcher.group(1))) { argIndex = Integer.parseInt(matcher.group(1)); } //if index is not specified, use current loop index else { argIndex = loopIndex; } //if the index is within provided arguments length if (argIndex < args.length) { arg = args[argIndex]; } //if the index is greater than available values else { arg = UNDEFINED; } //if argument value is null if (arg == null) { arg = "null"; } matcher.appendReplacement(buffer, arg.toString()); loopIndex++; } matcher.appendTail(buffer); return buffer.toString(); } }