マイツールクラス
8180 ワード
1:帯域幅トラフィックを単位に変換
2:listグループ(前のn個のlistサイズはgroupSize)
3:javaタイムゾーン
4:propertiesプロファイルの定数クラスを読み込む
親:
サブクラス(プロファイルごとにサブクラス)
propertiesプロファイル
report.threshold=0
channel.top.n=100000000
#channel.state=COMMERCIAL,TRANSFER
channel.state=TEST,COMMERCIAL,TRANSFER
TEST=テスト
TRANSFER=ターン
COMMERCIAL=商用
email.server=corp.chinacache.com
addresser.user=
addresser.pass=
[email protected]
package com.chinacache.snp.TransferChannelAlarm.util;
import java.text.DecimalFormat;
import java.text.Format;
public class FormatToSuitableFluxUnit {
private static final long K_BIT = 1000;
private static final long M_BIT = 1000 * 1000;
private static final long G_BIT = 1000 * 1000 * 1000;
private static final long T_BIT = 1000 * 1000 * 1000 * 1000L;
private static final long P_BIT = 1000 * 1000 * 1000 * 1000L * 1000L;
private static final long E_BIT = 1000 * 1000 * 1000 * 1000L * 1000L * 1000L;
public static final String BIT_STR = "Byte";
public static final String K_BIT_STR = "KB";
public static final String M_BIT_STR = "MB";
public static final String G_BIT_STR = "GB";
public static final String T_BIT_STR = "TB";
public static final String P_BIT_STR = "PB";
public static final String E_BIT_STR = "EB";
/**
* bit , ,
*/
public static String getSuitableFluxRepresentation(long fluxInBit) {
//
Format format = new DecimalFormat("#.00");
if (fluxInBit >= E_BIT) {
return format.format((double) fluxInBit / E_BIT) + " " + E_BIT_STR;
} else if (fluxInBit >= P_BIT) {
return format.format((double) fluxInBit / P_BIT) + " " + P_BIT_STR;
} else if (fluxInBit >= T_BIT) {
return format.format((double) fluxInBit / T_BIT) + " " + T_BIT_STR;
} else if (fluxInBit >= G_BIT) {
return format.format((double) fluxInBit / G_BIT) + " " + G_BIT_STR;
} else if (fluxInBit >= M_BIT) {
return format.format((double) fluxInBit / M_BIT) + " " + M_BIT_STR;
} else if (fluxInBit >= K_BIT) {
return format.format((double) fluxInBit / K_BIT) + " " + K_BIT_STR;
} else {
return fluxInBit +" "+ BIT_STR;
}
}
/**
*
*/
public static String getSuitableFluxUnit(long fluxInBit) {
if (fluxInBit >= E_BIT) {
return E_BIT_STR;
} else if (fluxInBit >= P_BIT) {
return P_BIT_STR;
} else if (fluxInBit >= T_BIT) {
return T_BIT_STR;
} else if (fluxInBit >= G_BIT) {
return G_BIT_STR;
} else if (fluxInBit >= M_BIT) {
return M_BIT_STR;
} else if (fluxInBit >= K_BIT) {
return K_BIT_STR;
} else {
return BIT_STR;
}
}
}
2:listグループ(前のn個のlistサイズはgroupSize)
package com.chinacache.snp.bill.datacontrast.util;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
public class GroupUtil {
public static List<List<String>> divideIntoGroups(List<String> list, int groupSize) {
if (CollectionUtils.isEmpty(list)) {
return null;
}
List<List<String>> groups = new ArrayList<List<String>>();
List<String> group = null;
int totalSize = list.size();
for (int i = 0; i < totalSize; i++) {
if (i % groupSize == 0) {
group = new ArrayList<String>();
groups.add(group);
}
group.add(list.get(i));
}
return groups;
}
}
3:javaタイムゾーン
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //
TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai"); //
sdf.setTimeZone(timezone );
System.out.println(sdf.format(new Date()));
4:propertiesプロファイルの定数クラスを読み込む
親:
package com.chinacache.snp.TransferChannelAlarm.util;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;
/**
* ,
*
*/
public class ConfigurableContants {
private static final Logger logger = Logger.getLogger(ConfigurableContants.class);
protected static Properties p = new Properties();
protected static void init(String propertyFileName) {
InputStream in = null;
try {
in = ConfigurableContants.class.getResourceAsStream(propertyFileName);
if (in != null)
p.load(in);
} catch (IOException e) {
logger.error("load " + propertyFileName + " into Contants error");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
protected static String getProperty(String key, String defaultValue) {
return p.getProperty(key, defaultValue);
}
protected static String getProperty(String key) {
return getProperty(key, "");
}
/**
* ","
*
* @param key
* @param defaultValue
* @return
*/
protected static Set<String> getSetProperty(String key, String defaultValue) {
String[] strings = p.getProperty(key, defaultValue).split(",");
HashSet<String> hashSet = new HashSet<String>(strings.length);
for (String string : strings) {
hashSet.add(string.trim());
}
return hashSet;
}
protected static Set<String> getSetProperty(String key) {
return getSetProperty(key, "");
}
protected static Date getPropertyWithDateFormate(String key, String dateFormate) {
String str = null;
Date date = null;
try {
str = getProperty(key, "");
date = new SimpleDateFormat(dateFormate).parse(str);
} catch (ParseException e) {
logger.error("Date [" + str + "] Error in Contants");
}
return date;
}
}
サブクラス(プロファイルごとにサブクラス)
package com.chinacache.snp.TransferChannelAlarm.util;
/**
* ,
*
*/
public final class SystemConstants extends ConfigurableContants {
static {
init("/system.properties");
}
public static final String thresholdValue = getProperty("report.threshold", "1");
public static final String channelState = getProperty("channel.state", "TEST,TRANSFER");
public static final String topSize = getProperty("channel.top.n", "1000");
public static final String mailServer = getProperty("email.server", "");
public static final String addresserUser = getProperty("addresser.user", "");
public static final String addresserPass = getProperty("addresser.pass", "");
public static final String cc = getProperty("cc", "[email protected]");
public static String getStringByKey(String key) {
return getProperty(key);
}
}
propertiesプロファイル
report.threshold=0
channel.top.n=100000000
#channel.state=COMMERCIAL,TRANSFER
channel.state=TEST,COMMERCIAL,TRANSFER
TEST=テスト
TRANSFER=ターン
COMMERCIAL=商用
email.server=corp.chinacache.com
addresser.user=
addresser.pass=
[email protected]