[Java] Binary numbers in enum type.

public enum Type {
  /** none */
  NONE(0),
  /** ONE */
  ONE(1),
  /** TWO */
  TWO(2),
  /** FOUR */
  FOUR(4),
  /** EIGHT */
  EIGHT(8),
  /** SIXTEEN */
  SIXTEEN(16);

  private int key;
  private static int[] binaryArray = { 0, 1, 2, 4, 8, 16 };

  Type(int code) {
    this.key = code;
  }

  public int getKey() {
    return key;
  }

  public static void main(String[] args) {
    System.out.println(findListByKey(60));
  }

  public static List<Type> findListByKey(int key) {
    List<Type> matchList = new ArrayList<>();

    List<Integer> binarySequence = getBinarySequence(key);

    for (Integer code:binarySequence) {
        matchList.add(findByKey(code));
    }

    return matchList;
  }

  public static Type findByKey(int key) {
    Type match;
    for (Type e : values()) {
        if (e.getKey() == key) {
            match = e;
            return match;
        }
    }

    return NONE;
  }

  private static List<Integer> getBinaryNumberSequence(int target){
    List<Integer> result = new ArrayList<>();

    boolean flag = true;
    while (flag == true) {
        int code = findCloseDigit(target);
        target = target - code;
        result.add(code);
        if (target == 0) {
            flag = false;
        }
    }

    return result;
  }

  private static int findCloseDigit(int target) {
    int result = 0;
    for (int code:binaryArray) {
        if (code <= target) {
            result = code;
        } else {
            break;
        }
    }
    return result;
  }
}
Posted in Java | Leave a comment

[Java] String.format

private String GroupMessageAuto = "已完成自動同步動作,共計新增 %s 筆、刪除 %s 筆、同步 %s 筆”;
groupMsg = String.format(GroupMessageAuto, syncCount.getAdd(), syncCount.getDel(), syncCount.getSync());
Posted in Java | Leave a comment

[Intellij] Can’t start git

After Mac OS X upgrading, something happen in the Intellij.

Screenshot_2015-09-21_9.54.11screenshot

Then we should enable the Xcode license again.

sudo xcodebuild -license

Ref. OSX Mavericks and Git Error

Posted in Intellij, Mac | Tagged , | Leave a comment

[Vim] Turn on/off line numbers

Ref. stackoverflow: how to take off line numbers in vi

  1. turn on

    :set nu

  2. turn off

    :set nu!

Posted in Vim | Tagged | Leave a comment

[Java、HBase] Retrieving timestamp form HBase row

Ref. stackoverflow: retrieving timestamp from hbase row

HTableInterface hTableInterface = CONNECTION.getTable(Table.SCHEMA_NAME);
Result result = hTableInterface.get(get);
Date date = new Date(result.raw()[0].getTimestamp());
System.out.println("DateTime: " + sdf.format(date));

we must edit HBaseSettings to set hbaseConf.set(HBASE_ENV_KEY_ZOOKEEPER_QUORUM, “target_zookeeper_address”);

Posted in HBase, Java | Tagged , | Leave a comment

[Java] Transform currentTimeMillis to readable date format

Ref. stackoverflow: how-to-transform-currenttimemillis-to-a-readable-date-format

        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
        Date date = new Date(System.currentTimeMillis());
        System.out.println("DateTime: " + sdf.format(date));
Posted in Java | Tagged | Leave a comment

[Java] Check UUID format

Ref. BUKKIT: Best way to check if a String is a UUID

    private static boolean checkUUIDFromat(String uuid) {
        if (uuid.matches("[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}")) {
            return true;
        }
        return false;
    }
Posted in Java | Leave a comment

[Java] Check string is a messy code?

Ref: Java判断字符串是否是乱码(亲测可用)

public static boolean isChinese(char c) {
    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
    if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
            || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
        return true;
    }
    return false;
}

public static boolean isMessyCode(String strName) {
    Pattern p = Pattern.compile("\s*|t*|r*|n*");
    Matcher m = p.matcher(strName);
    String after = m.replaceAll("");
    String temp = after.replaceAll("\p{P}", "");
    char[] ch = temp.trim().toCharArray();
    float chLength = ch.length;
    float count = 0;
    for (int i = 0; i < ch.length; i++) {
        char c = ch[i];
        if (!Character.isLetterOrDigit(c)) {
            if (!isChinese(c)) {
                count = count + 1;
            }
        }
    }
    float result = count / chLength;
    if (result > 0.4) {
        return true;
    } else {
        return false;
    }

}
Posted in Java | Leave a comment

Parse JSON string via command line

Ref.:How to parse JSON string via command line on Linux

Ubuntu: apt-get install jq

Mac: brew install jq

Posted in Shell Script | Leave a comment

[.Net] OLEDB in Windows x64 error.

Ref. 解決64位元主機上IIS無法跑32位元的元件:Jet OLEDB 4

Posted in Visual Studio | Leave a comment