Total Pageviews

2014/10/31

2014/10 Travel

億載金城

台南市移民署

四草紅樹林綠色隧道

台南林百貨

善化胡厝寮


鹿耳門天后宮


台南火車站

2014/10/29

How to read Excel file by Apache POI

Requirement
Assume we have an excel file, we would like to read the data which in sheet2.
We only need the amount in Column E and column I, and the value of Column A show be 'XXX年'

Here has sample code

1:  public class POITest {  
2:    private final static Logger LOG = LoggerFactory.getLogger(POITest.class);  
3:    /**  
4:     * The main method.  
5:     *   
6:     * @param args  
7:     *      the arguments  
8:     * @throws IOException  
9:     *       Signals that an I/O exception has occurred.  
10:     */  
11:    public static void main(String[] args) throws IOException {  
12:      String testFile = "D:\\work\\ifms\\TestFiles\\4102294114W75Y7OW0.xls";  
13:      Integer sheetNum = 1;  
14:      FileInputStream file = null;  
15:      try {  
16:        file = new FileInputStream(new File(testFile));  
17:        // Get the workbook instance for XLS file  
18:        HSSFWorkbook workbook = new HSSFWorkbook(file);  
19:        // Get the specific sheet from the workbook  
20:        HSSFSheet sheet = workbook.getSheetAt(sheetNum);  
21:        // Get iterator to all the rows in current sheet  
22:        Iterator rowIterator = sheet.iterator();  
23:        List list = new ArrayList<>();  
24:        while (rowIterator.hasNext()) {  
25:          Row row = rowIterator.next();  
26:          if (StringUtils.indexOf(row.getCell(0).toString(), "年") > 0) {//condition  
27:            Test test = new POITest().new Test();  
28:            test.setYear(row.getCell(0).toString());//column A  
29:            test.setGdp(new BigDecimal(row.getCell(4).getNumericCellValue()));//column E  
30:            test.setGnp(new BigDecimal(row.getCell(8).getNumericCellValue()));//column I  
31:            list.add(test);  
32:          }  
33:        }  
34:        for (Test vo : list) {  
35:          LOG.debug("vo=" + vo.toString());  
36:        }  
37:      } catch (FileNotFoundException e) {  
38:        throw new RuntimeException(e);  
39:      } catch (IOException e) {  
40:        throw new RuntimeException(e);  
41:      } finally {  
42:        file.close();  
43:      }  
44:    }  
45:    private class Test {  
46:      private String year;  
47:      private BigDecimal gdp;  
48:      private BigDecimal gnp;  
49:      public String getYear() {  
50:        return year;  
51:      }  
52:      public void setYear(String year) {  
53:        this.year = year;  
54:      }  
55:      public BigDecimal getGdp() {  
56:        return gdp;  
57:      }  
58:      public void setGdp(BigDecimal gdp) {  
59:        this.gdp = gdp;  
60:      }  
61:      public BigDecimal getGnp() {  
62:        return gnp;  
63:      }  
64:      public void setGnp(BigDecimal gnp) {  
65:        this.gnp = gnp;  
66:      }  
67:      public String toString() {  
68:        return ToStringBuilder.reflectionToString(this);  
69:      }  
70:    }  
71:  }  

Here is the log which print in console
1:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@2d7349d7[year=80年,gdp=4958220,gnp=5093770]  
2:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@6bf4d990[year=81年,gdp=5534544,gnp=5655306]  
3:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@17f7b44f[year=82年,gdp=6110101,gnp=6224145]  
4:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@75ebad4[year=83年,gdp=6685505,gnp=6793022]  
5:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@5c3bb813[year=84年,gdp=7277545,gnp=7388464]  
6:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@54b216b3[year=85年,gdp=7906075,gnp=8015577]  
7:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@77f06d35[year=86年,gdp=8574784,gnp=8664395]  
8:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@4991f017[year=87年,gdp=9204174,gnp=9272725]  
9:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@c299bbd[year=88年,gdp=9649049,gnp=9739567]  
10:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@7faf9b87[year=89年,gdp=10187394,gnp=10326952]  
11:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@620bfd8e[year=90年,gdp=9930387,gnp=10122411]  
12:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@133a7ec[year=91年,gdp=10411639,gnp=10654141]  
13:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@66557791[year=92年,gdp=10696257,gnp=11025130]  
14:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@751d0513[year=93年,gdp=11365292,gnp=11737391]  
15:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@44385e76[year=94年,gdp=11740279,gnp=12031145]  
16:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@50c1b7f7[year=95年,gdp=12243471,gnp=12555170]  
17:  [main] DEBUG g.n.e.w.c.Ets402rReportController - vo=gov.nta.ets.web.controller.Ets402rReportController$Test@5e14e28c[year=96年,gdp=12910511,gnp=13243277] 

Reference
[1] http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/

2014/10/27

How to use Google Guava to do Collection Filter

Requirement

We have a collection data (List of value object), we would like to find the objects which fyr is 101. 
Here has the collection data:
1:  dtoList=[  
2:  gov.nta.fms.dto.FmsSumRateDto@1a395e5a[fyr=101,accmon=01,alc=168407392348.53,alcavg=0.0809],   
3:  gov.nta.fms.dto.FmsSumRateDto@2c617429[fyr=101,accmon=02,alc=164834284030.53,alcavg=0.0792],   
4:  gov.nta.fms.dto.FmsSumRateDto@32114682[fyr=101,accmon=03,alc=180965071697.53,alcavg=0.0869],   
5:  gov.nta.fms.dto.FmsSumRateDto@c2c1a7c[fyr=101,accmon=04,alc=165659472439.53,alcavg=0.0796],   
6:  gov.nta.fms.dto.FmsSumRateDto@237ec922[fyr=101,accmon=05,alc=180372232532.53,alcavg=0.0866],   
7:  gov.nta.fms.dto.FmsSumRateDto@58c6e962[fyr=101,accmon=06,alc=176357775471.53,alcavg=0.0847],   
8:  gov.nta.fms.dto.FmsSumRateDto@6b044e76[fyr=101,accmon=07,alc=187368376522.53,alcavg=0.09],   
9:  gov.nta.fms.dto.FmsSumRateDto@67bb5bdd[fyr=101,accmon=08,alc=181824695849.53,alcavg=0.0873],   
10:  gov.nta.fms.dto.FmsSumRateDto@3d8c3f5f[fyr=101,accmon=09,alc=170308922011.45,alcavg=0.0818],   
11:  gov.nta.fms.dto.FmsSumRateDto@4294958a[fyr=101,accmon=10,alc=168308032423.95,alcavg=0.0808],   
12:  gov.nta.fms.dto.FmsSumRateDto@28e5b92c[fyr=101,accmon=11,alc=179491195734.95,alcavg=0.0862],   
13:  gov.nta.fms.dto.FmsSumRateDto@1e00cae[fyr=101,accmon=12,alc=157844063362.95,alcavg=0.0758],   
14:  gov.nta.fms.dto.FmsSumRateDto@1c607478[fyr=102,accmon=01,alc=170969213258.95,alcavg=0.0806],   
15:  gov.nta.fms.dto.FmsSumRateDto@7e80d6[fyr=102,accmon=02,alc=159840653037.95,alcavg=0.0753],   
16:  gov.nta.fms.dto.FmsSumRateDto@713c61da[fyr=102,accmon=03,alc=169579370948.95,alcavg=0.0799],   
17:  gov.nta.fms.dto.FmsSumRateDto@333c694a[fyr=102,accmon=04,alc=169624940547.95,alcavg=0.0799],   
18:  gov.nta.fms.dto.FmsSumRateDto@4324938d[fyr=102,accmon=05,alc=173064277154.95,alcavg=0.0816],   
19:  gov.nta.fms.dto.FmsSumRateDto@1653306b[fyr=102,accmon=06,alc=169616844409.95,alcavg=0.0799],   
20:  gov.nta.fms.dto.FmsSumRateDto@2f8fcc40[fyr=102,accmon=07,alc=175060305269.95,alcavg=0.0825],   
21:  gov.nta.fms.dto.FmsSumRateDto@9aa9625[fyr=102,accmon=08,alc=193638114494.95,alcavg=0.0913],   
22:  gov.nta.fms.dto.FmsSumRateDto@1e675c18[fyr=102,accmon=09,alc=198567712906.95,alcavg=0.0936],   
23:  gov.nta.fms.dto.FmsSumRateDto@13cf3b40[fyr=102,accmon=10,alc=181427982270.95,alcavg=0.0855],   
24:  gov.nta.fms.dto.FmsSumRateDto@a3af0e9[fyr=102,accmon=11,alc=187353709301.95,alcavg=0.0883],   
25:  gov.nta.fms.dto.FmsSumRateDto@239e563e[fyr=102,accmon=12,alc=172980508042.95,alcavg=0.0815]  
26:  ]  


How to Use Google Guava API
1. Create Predicate instance to determines a true or false value for a given input. (Line1~Line7)
2. Utilize Collection2.filter to return the elements of unfiltered that satisfy a predicate.(Line8~line9)

Here has the code snippet:
1:     Predicate predicate = new Predicate() {  
2:        @Override  
3:        public boolean apply(FmsSumRateDto input) {  
4:          //Determines true or false value for a given input.  
5:          return "101".equqls(input.getFyr());  
6:        }  
7:      };  
8:      //Returns the elements of unfiltered that satisfy a predicate.  
9:      Iterator result = Collections2.filter(dtoList, predicate).iterator();  
10:      while(result.hasNext()) {  
11:        log.debug("result="+result.next().toString());  
12:      }  

Here is the result which statisfy the predicate:
1:  result=gov.nta.fms.dto.FmsSumRateDto@1a395e5a[fyr=101,accmon=01,alc=168407392348.53,alcavg=0.0809]  
2:  result=gov.nta.fms.dto.FmsSumRateDto@2c617429[fyr=101,accmon=02,alc=164834284030.53,alcavg=0.0792]  
3:  result=gov.nta.fms.dto.FmsSumRateDto@32114682[fyr=101,accmon=03,alc=180965071697.53,alcavg=0.0869]  
4:  result=gov.nta.fms.dto.FmsSumRateDto@c2c1a7c[fyr=101,accmon=04,alc=165659472439.53,alcavg=0.0796]  
5:  result=gov.nta.fms.dto.FmsSumRateDto@237ec922[fyr=101,accmon=05,alc=180372232532.53,alcavg=0.0866]  
6:  result=gov.nta.fms.dto.FmsSumRateDto@58c6e962[fyr=101,accmon=06,alc=176357775471.53,alcavg=0.0847]  
7:  result=gov.nta.fms.dto.FmsSumRateDto@6b044e76[fyr=101,accmon=07,alc=187368376522.53,alcavg=0.09]  
8:  result=gov.nta.fms.dto.FmsSumRateDto@67bb5bdd[fyr=101,accmon=08,alc=181824695849.53,alcavg=0.0873]  
9:  result=gov.nta.fms.dto.FmsSumRateDto@3d8c3f5f[fyr=101,accmon=09,alc=170308922011.45,alcavg=0.0818]  
10:  result=gov.nta.fms.dto.FmsSumRateDto@4294958a[fyr=101,accmon=10,alc=168308032423.95,alcavg=0.0808]  
11:  result=gov.nta.fms.dto.FmsSumRateDto@28e5b92c[fyr=101,accmon=11,alc=179491195734.95,alcavg=0.0862]  
12:  result=gov.nta.fms.dto.FmsSumRateDto@1e00cae[fyr=101,accmon=12,alc=157844063362.95,alcavg=0.0758] 


Reference


2014/10/23

要去哪裡查詢零股交易的相關資訊


有時候股票除了配現金以外,還會給零股給投資大眾,為了讓買賣比較容易(因為零股只能利用盤後交易),故通常會利用盤後交易來買零股湊成一張

我們如果要去查詢零股最佳一檔買、賣價格的話,可以到基本市況報導網站( http://newmis.twse.com.tw/stock/oddTrade.jsp ) 查詢


 假設如果我要找的是『食品類股』,在上市類股下拉單選取『食品工業』

 該網站就會顯示相關食品類股的零股揭示買價、賣價、成交價以及成交股數


有關零股更多資訊可以參考 http://www.masterlink.com.tw/service/qanda/Q&A-6.htm 

2014/10/21

How to copy text from Notepad++ to Microsoft Word and keep its color to distinguish keywords


Notepad++ can distinguish between different languages source code can be written in. For example, a language could distinguish certain keywords that have to be differently interpreted, and as such it can be useful to distinguish these keywords using another color or font. 


For example.
The language is Java.

The language is SQL.


Scenario
If we would like to copy SQL statement from Notepad++ to Microsoft Word in my system design document, but it seems cannot keep its color to distinguish keywords. 
For example.

Resolution
Step1. Select text you would like to copy.

Step2. Plugins --> NppExport --> Copy RTF to clipboard

Step3. Paste text to Microsoft word


2014/10/16

How to display Tradition Chinese Character in Git Bash

Problem
Why I cannot display Tradition Chinese Character in Git Bash correctly?

Solution
You need to edit three files under git\etc

1. edit gitconfig file and append configuration as following:
1:  [gui]  
2:  encoding = utf-8   
3:  #log编码  
4:  [i18n]  
5:  commitencoding = utf-8   
6:  #支持中文路径  
7:  [svn]  
8:  pathnameencoding = utf-8   

2. edit git-completion.bash file and append configuration  as following:
1:  #正常顯示中文  
2:  alias ls='ls --show-control-chars --color=auto'  

3. edit inputrc file and append configuration  as following:
1:  #bash中可以正常输入中文  
2:  set output-meta on   
3:  set convert-meta off  

Check Result

2014/10/15

JRebel - Reload any changes without restart server

As a developer, we do not want to waste time to restart server again and again to see the implace of code changes.
You can try JRebel to save your time.

If you are using Eclipse, here has good step-by-step installation and configuration guide: http://manuals.zeroturnaround.com/jrebel/ide/eclipse.html

For more information, please check http://zeroturnaround.com/software/jrebel/learn/

Demo

Here has code snippet, and it will print debug message as entering this method
1:    @RequestMapping(value = "/query/tab1", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)  
2:    public @ResponseBody  
3:    List findTab1(@RequestBody Fms420rFormBean formBean, Alerter alerter) {  
4:      log.debug("test");  
5:      List dataList = service.queryTab1(formBean.getYear(), formBean.getMonth());  
6:      if (CollectionUtils.isEmpty(dataList)) {  
7:        alerter.info(Messages.warning_notFound());  
8:      } else {  
9:        alerter.info(Messages.success_find());  
10:      }  
11:      return dataList;  
12:    }  

And the console will print "test" debugging message as entering this method:
1:  19:15:45,493 INFO [stdout]  [20123] DEBUG gov.nta.fms.web.rest.Fms420rResource - test  

In order to test JRebel, I just modified the debug message to "JRebel test" and submit query without restart application server
1:    @RequestMapping(value = "/query/tab1", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)  
2:    public @ResponseBody  
3:    List findTab1(@RequestBody Fms420rFormBean formBean, Alerter alerter) {  
4:      log.debug("JRebel test");  
5:      List dataList = service.queryTab1(formBean.getYear(), formBean.getMonth());  
6:      if (CollectionUtils.isEmpty(dataList)) {  
7:        alerter.info(Messages.warning_notFound());  
8:      } else {  
9:        alerter.info(Messages.success_find());  
10:      }  
11:      return dataList;  
12:    }  

See....I don't redeploy and restart application server, but the console print the up-to-date debug message. 
1:  19:16:19,742 INFO [stdout]  [20123] DEBUG gov.nta.fms.web.rest.Fms420rResource - JRebel test  

It's because of JRebel reload any changes classes automatically. 
When you change any class or resource in your IDE, the change will reflect in you application immediately, skipping the build and redeploy phases.