Skip to content

Commit

Permalink
Common class
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanglong9344 committed Jan 12, 2018
1 parent fdb81c4 commit bd8b4b7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 20 deletions.
28 changes: 28 additions & 0 deletions src/java_common/CommonMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package java_common;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Common methods
*
* @author ÌÆÁú
*/
public class CommonMethod {
/**get current time in the form of "yyyy-MM-dd HH:mm:ss"*/
public static final String TIME_NOW = getTimeNow();
private static String getTimeNow(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}

/**get current time in the form of nanosecond*/
public static long nano() {
return System.nanoTime();
}

/**get current time in the form of millisecond*/
public static long milli() {
return System.currentTimeMillis();
}
}
48 changes: 46 additions & 2 deletions src/java_common/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
* Common utilities for all classes
Expand All @@ -12,7 +13,12 @@
*
*/
public class CommonUtils {
/**Generate a List*/
/**
* Generate a List
*
* @param size
* @return
*/
public static List<String> getList(int size){
List<String> list = new ArrayList<String>();
for(int i=1;i<=size;i++){
Expand All @@ -21,12 +27,50 @@ public static List<String> getList(int size){
return list;
}

/**Generate a Map*/
/**
* Generate a Map
*
* @param size
* @return
*/
public static Map<String,String> getMap(int size){
Map<String,String> map = new HashMap<String,String>();
for(int i=1;i<=size;i++){
map.put("K"+i,"V"+i);
}
return map;
}

/**
* Generate a String
*
* @param size
* @return
*/
public static String getString(int length){
StringBuilder sb = new StringBuilder();
if(length <=0) {
return sb.toString();
}
Random r = new Random();
sb.append((char)(r.nextInt(26) + 65)); // first letter is upper case
for(int i=1;i<length;i++) {
switch(r.nextInt(4)) {
case 0:
sb.append(r.nextInt(10)); // number
break;
case 1:
sb.append((char)(r.nextInt(26) + 65)); // upper case
break;
case 2:
sb.append((char)(r.nextInt(26) + 97)); // lower case
break;
case 3:
sb.append((char)(r.nextInt(1) + 95)); // underline
break;
default:break;
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @author ÌÆÁú
*/
public class CommonBool {
public class CommonValue {
public final static boolean TRUE = true;
public final static boolean FALSE = false;
}
13 changes: 13 additions & 0 deletions src/java_common/JVMMemory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package java_common;

/**
* The memory of JVM(M)
*
* @author ÌÆÁú
*
*/
public class JVMMemory {
private final static int M = 1024*1024;
static long maxMemory = Runtime.getRuntime().maxMemory()/M;
static long totalUsedMemory = Runtime.getRuntime().totalMemory()/M;
}
17 changes: 0 additions & 17 deletions src/java_common/TimeNow.java

This file was deleted.

0 comments on commit bd8b4b7

Please sign in to comment.