Skip to content

Commit

Permalink
#2317 Duplicate ArrayUtils cleanup
Browse files Browse the repository at this point in the history
 - Move to commons module
 - Deprecate dropToString() api
  • Loading branch information
emeroad committed Dec 9, 2016
1 parent f3de4c4 commit aaf7f49
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,29 @@
package com.navercorp.pinpoint.bootstrap.plugin.jdbc;

/**
* @author emeroad
* @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils}
* @author Woonduk Kang(emeroad)
*/
@Deprecated
public final class ArrayUtils {

private ArrayUtils() {
}

/**
* @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils#abbreviate(byte[])}
*/
@Deprecated
public static String dropToString(byte[] bytes) {
return dropToString(bytes, 32);
return com.navercorp.pinpoint.common.util.ArrayUtils.abbreviate(bytes);
}

public static String dropToString(byte[] bytes, int limit) {
if (bytes == null) {
return "null";
}
if (limit < 0) {
throw new IllegalArgumentException("negative limit:" + limit);
}
// TODO handle negative limit

// Last valid index is length - 1
int bytesMaxLength = bytes.length - 1;
final int maxLimit = limit - 1;
if (bytesMaxLength > maxLimit) {
bytesMaxLength = maxLimit;
}
if (bytesMaxLength == -1) {
if (bytes.length == 0) {
return "[]";
} else {
return "[...(" + bytes.length + ")]";
}
}


final StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; ; i++) {
sb.append(bytes[i]);
if (i == bytesMaxLength) {
if ((bytes.length - 1) <= maxLimit) {
return sb.append(']').toString();
} else {
sb.append(", ...(");
sb.append(bytes.length - (i+1));
sb.append(")]");
return sb.toString();
}
}
sb.append(", ");
}
/**
* @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils#abbreviate(byte[], int)}
*/
@Deprecated
public static String dropToString(byte[] bytes, int maxWidth) {
return com.navercorp.pinpoint.common.util.ArrayUtils.abbreviate(bytes, maxWidth);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.navercorp.pinpoint.bootstrap.plugin.jdbc.bindvalue;

import com.navercorp.pinpoint.bootstrap.plugin.jdbc.ArrayUtils;
import com.navercorp.pinpoint.common.util.ArrayUtils;

/**
* @author emeroad
Expand All @@ -28,11 +28,11 @@ public String convert(Object[] args) {
return "null";
}
if (args.length == 2) {
byte[] bytes = (byte[]) args[1];
final byte[] bytes = (byte[]) args[1];
if (bytes == null) {
return "null";
} else {
return ArrayUtils.dropToString(bytes);
return ArrayUtils.abbreviate(bytes);
}
}
return "error";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.sql.Time;
import java.sql.Timestamp;

import com.navercorp.pinpoint.bootstrap.plugin.jdbc.ArrayUtils;
import com.navercorp.pinpoint.bootstrap.util.StringUtils;
import com.navercorp.pinpoint.common.util.ArrayUtils;

/**
* @author emeroad
Expand All @@ -35,11 +35,11 @@ public String convert(Object[] args) {
return "null";
}
if (args.length == 2) {
Object param = args[1];
final Object param = args[1];
return getParameter(param);

} else if (args.length == 3) {
Object param = args[1];
final Object param = args[1];
return getParameter(param);
}
return "error";
Expand All @@ -50,33 +50,33 @@ private String getParameter(Object param) {
return "null";
} else {
if (param instanceof Byte) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof String) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof BigDecimal) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Short) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Integer) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Long) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Float) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Double) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof BigInteger) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof java.sql.Date) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Time) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Timestamp) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof Boolean) {
return dropToString(param);
return abbreviate(param);
} else if (param instanceof byte[]) {
return ArrayUtils.dropToString((byte[]) param);
return ArrayUtils.abbreviate((byte[]) param);
} else if (param instanceof InputStream) {
return getClassName(param);
} else if (param instanceof java.sql.Blob) {
Expand All @@ -89,7 +89,7 @@ private String getParameter(Object param) {
}
}

private String dropToString(Object param) {
private String abbreviate(Object param) {
return StringUtils.abbreviate(param.toString());
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2016 NAVER Corp.
*
* 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.navercorp.pinpoint.common.util;

/**
* @author Woonduk Kang(emeroad)
*/
public final class ArrayUtils {

private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 32;

private ArrayUtils() {
}

public static String abbreviate(byte[] bytes) {
return abbreviate(bytes, DEFAULT_ABBREVIATE_MAX_WIDTH);
}

public static String abbreviate(byte[] bytes, int maxWidth) {
if (bytes == null) {
return "null";
}
if (maxWidth < 0) {
throw new IllegalArgumentException("negative maxWidth:" + maxWidth);
}
// TODO handle negative limit

// Last valid index is length - 1
int bytesMaxLength = bytes.length - 1;
final int maxLimit = maxWidth - 1;
if (bytesMaxLength > maxLimit) {
bytesMaxLength = maxLimit;
}
if (bytesMaxLength == -1) {
if (bytes.length == 0) {
return "[]";
} else {
return "[...(" + bytes.length + ")]";
}
}


final StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; ; i++) {
sb.append(bytes[i]);
if (i == bytesMaxLength) {
if ((bytes.length - 1) <= maxLimit) {
return sb.append(']').toString();
} else {
sb.append(", ...(");
sb.append(bytes.length - (i+1));
sb.append(")]");
return sb.toString();
}
}
sb.append(", ");
}
}
}
Loading

0 comments on commit aaf7f49

Please # to comment.