Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2019 The ZuSmart Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zusmart.base.logging.format;
import java.util.HashSet;
import java.util.Set;
/**
* @author Administrator
*
*/
public final class Formatter {
private static final String PLACEHODLER = "{}";
private static final char ESCAPE_CHAR = '\\';
public static FormatterTuple format(final String pattern, final Object arg) {
return format(pattern, new Object[] { arg });
}
public static FormatterTuple format(final String pattern, final Object argA, final Object argB) {
return format(pattern, new Object[] { argA, argB });
}
public static FormatterTuple format(final String pattern, final Object[] args) {
if (null == args || args.length == 0) {
return new FormatterTuple(pattern, null);
}
int idx = args.length - 1;
Object entry = args[idx];
Throwable throwable = entry instanceof Throwable ? (Throwable) entry : null;
if (null == pattern) {
return new FormatterTuple(null, throwable);
}
int j = pattern.indexOf(PLACEHODLER);
if (j == -1) {
return new FormatterTuple(pattern, throwable);
}
StringBuilder sb = new StringBuilder(pattern.length() + 50);
int i = 0;
int L = 0;
do {
boolean notEscaped = j == 0 || pattern.charAt(j - 1) != ESCAPE_CHAR;
if (notEscaped) {
sb.append(pattern, i, j);
} else {
sb.append(pattern, i, j - 1);
notEscaped = j >= 2 && pattern.charAt(j - 2) == ESCAPE_CHAR;
}
i = j + 2;
if (notEscaped) {
deeplyAppendParameter(sb, args[L], null);
L++;
if (L > idx) {
break;
}
} else {
sb.append(PLACEHODLER);
}
j = pattern.indexOf(PLACEHODLER, i);
} while (j != -1);
sb.append(pattern, i, pattern.length());
return new FormatterTuple(sb.toString(), L <= idx ? throwable : null);
}
private static void deeplyAppendParameter(StringBuilder sb, Object obj, Set