Skip to content

Commit

Permalink
fixed null error
Browse files Browse the repository at this point in the history
  • Loading branch information
smartloli committed Aug 3, 2020
1 parent 4592158 commit 3b9688d
Show file tree
Hide file tree
Showing 6 changed files with 912 additions and 917 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* to you 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
Expand All @@ -17,136 +17,166 @@
*/
package org.smartloli.kafka.eagle.common.util;

import com.alibaba.fastjson.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

/**
* String conversion tool and null convert.
*
* @author smartloli.
*
* Created by May 21, 2017
* @author smartloli.
* <p>
* Created by May 21, 2017
*/
public class StrUtils {

private final static long KB_IN_BYTES = 1024;

private final static long MB_IN_BYTES = 1024 * KB_IN_BYTES;

private final static long GB_IN_BYTES = 1024 * MB_IN_BYTES;

private final static long TB_IN_BYTES = 1024 * GB_IN_BYTES;

private final static DecimalFormat df = new DecimalFormat("0.00");

private static String SYSTEM_ENCODING = System.getProperty("file.encoding");

static {
if (SYSTEM_ENCODING == null) {
SYSTEM_ENCODING = "UTF-8";
}
}

private StrUtils() {
}

/** Formatter byte to kb,mb or gb etc. */
public static String stringify(long byteNumber) {
if (byteNumber / TB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) TB_IN_BYTES) + "TB";
} else if (byteNumber / GB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) GB_IN_BYTES) + "GB";
} else if (byteNumber / MB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) MB_IN_BYTES) + "MB";
} else if (byteNumber / KB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) KB_IN_BYTES) + "KB";
} else {
return String.valueOf(byteNumber) + "B";
}
}

/** Formatter byte to kb,mb or gb etc. */
public static JSONObject stringifyByObject(long byteNumber) {
JSONObject object = new JSONObject();
if (byteNumber / TB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) TB_IN_BYTES));
object.put("type", "TB");
return object;
} else if (byteNumber / GB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) GB_IN_BYTES));
object.put("type", "GB");
return object;
} else if (byteNumber / MB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) MB_IN_BYTES));
object.put("type", "MB");
return object;
} else if (byteNumber / KB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) KB_IN_BYTES));
object.put("type", "KB");
return object;
} else {
object.put("size", String.valueOf(byteNumber));
object.put("type", "B");
return object;
}
}

/** Formmatter number. */
public static double numberic(String number) {
DecimalFormat formatter = new DecimalFormat("###.##");
return Double.valueOf(formatter.format(Double.valueOf(number)));
}

/** Formmatter number. */
public static double numberic(String number, String format) {
DecimalFormat formatter = new DecimalFormat(format);
return Double.valueOf(formatter.format(Double.valueOf(number)));
}

/** Convert string number to double. */
public static long integer(double number) {
return Math.round(number);
}

/** Assembly number to string. */
public static String assembly(String number) {
return stringify(integer(numberic(number)));
}

/** whether it is empty. */
public static boolean isNull(String value) {
if (value == null || value.length() == 0 || "".equals(value)) {
return true;
}
return false;
}

/** whether list is empty. */
public static boolean isListNull(String value) {
if ("[]".equals(value)) {
return true;
}
return false;
}

/** Convert null to string. */
public static String convertNull(String value) {
if (isNull(value)) {
return "";
}
return value;
}

/** Convert strings to integers. */
public static List<Integer> stringsConvertIntegers(String[] values) {
List<Integer> integers = new ArrayList<Integer>();
for (String value : values) {
integers.add(Integer.parseInt(value.trim()));
}
return integers;
}
private final static long KB_IN_BYTES = 1024;

private final static long MB_IN_BYTES = 1024 * KB_IN_BYTES;

private final static long GB_IN_BYTES = 1024 * MB_IN_BYTES;

private final static long TB_IN_BYTES = 1024 * GB_IN_BYTES;

private final static DecimalFormat df = new DecimalFormat("0.00");

private static String SYSTEM_ENCODING = System.getProperty("file.encoding");

static {
if (SYSTEM_ENCODING == null) {
SYSTEM_ENCODING = "UTF-8";
}
}

private StrUtils() {
}

/**
* Formatter byte to kb,mb or gb etc.
*/
public static String stringify(long byteNumber) {
if (byteNumber / TB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) TB_IN_BYTES) + "TB";
} else if (byteNumber / GB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) GB_IN_BYTES) + "GB";
} else if (byteNumber / MB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) MB_IN_BYTES) + "MB";
} else if (byteNumber / KB_IN_BYTES > 0) {
return df.format((double) byteNumber / (double) KB_IN_BYTES) + "KB";
} else {
return String.valueOf(byteNumber) + "B";
}
}

/**
* Formatter byte to kb,mb or gb etc.
*/
public static JSONObject stringifyByObject(long byteNumber) {
JSONObject object = new JSONObject();
if (byteNumber / TB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) TB_IN_BYTES));
object.put("type", "TB");
return object;
} else if (byteNumber / GB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) GB_IN_BYTES));
object.put("type", "GB");
return object;
} else if (byteNumber / MB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) MB_IN_BYTES));
object.put("type", "MB");
return object;
} else if (byteNumber / KB_IN_BYTES > 0) {
object.put("size", df.format((double) byteNumber / (double) KB_IN_BYTES));
object.put("type", "KB");
return object;
} else {
object.put("size", String.valueOf(byteNumber));
object.put("type", "B");
return object;
}
}

/**
* Formmatter number.
*/
public static double numberic(String number) {
DecimalFormat formatter = new DecimalFormat("###.##");
return Double.valueOf(formatter.format(Double.valueOf(number)));
}

/**
* Formmatter number.
*/
public static double numberic(String number, String format) {
DecimalFormat formatter = new DecimalFormat(format);
return Double.valueOf(formatter.format(Double.valueOf(number)));
}

/**
* Convert string number to double.
*/
public static long integer(double number) {
return Math.round(number);
}

/**
* Assembly number to string.
*/
public static String assembly(String number) {
return stringify(integer(numberic(number)));
}

/**
* whether string is empty.
*/
public static boolean isNull(String value) {
if (value == null || value.length() == 0 || "".equals(value)) {
return true;
}
return false;
}

/**
* whether string[] is empty.
*/
public static boolean isNull(String[] value) {
if (value == null || value.length == 0) {
return true;
}
return false;
}

/**
* whether list is empty.
*/
public static boolean isListNull(String value) {
if ("[]".equals(value)) {
return true;
}
return false;
}

/**
* Convert null to string.
*/
public static String convertNull(String value) {
if (isNull(value)) {
return "";
}
return value;
}

/**
* Convert strings to integers.
*/
public static List<Integer> stringsConvertIntegers(String[] values) {
List<Integer> integers = new ArrayList<Integer>();
for (String value : values) {
integers.add(Integer.parseInt(value.trim()));
}
return integers;
}

}
Loading

0 comments on commit 3b9688d

Please sign in to comment.