Total Pageviews

2016/10/01

[閱讀筆記] Security Analysis (Part 4)


  1. 自由現金流是一種財務方法,用來衡量企業實際持有的能夠回報股東的現金。指在不危及公司生存與發展的前提下可供分配給股東(和債權人)的最大現金額
  2. Free cash flow 可以用來發放股利給股東,或是用來投資自己如研發、設備等
  3. 公司經營者必須決定所擁有的free cash flow,何時要給股東?何時要用於投資?正確的投資會在未來帶來更多的free cash flow,失敗的投資則會影響公司的價值
  4. 經營掙扎的公司,為了討好股東,往往會把free cash flow用在發放現金給股東,而不是自我投資,這往往是惡性循環
  5. 投資人可以透過股利政策來觀察經營者如何處理free cash flow。一般來說,若投資機會的風險相對低的話,企業會傾向貸款,不會調整股利;若投資機會的風險相對高,則會傾向調整股利發放,而不會去貸款
  6. 高殖利率是評價一檔股票的正面指標,但是有一些公司會操控股利發放來建立未來cash flow的假象,如GM、花旗都曾經有延後削減股利的紀錄。投資人可能會因為其高殖利率的誘因買進股票,隔年才發現股利大幅減少而陷入麻煩
  7. 擁有與能產生豐沛的現金,是找出一家好企業的指標。但是,除非管理者能把這些現金做聰明的投資、回饋現金股利給股東或買回庫藏股,不然對股東沒什麼好處。經營者的天份與意圖是非常重要的
  8. 股票市場中個股的選擇,因為其天生具有投機的成分,故很難有有效的分析技術來做分析,其分析結果常常沒有結論且無法令人滿意,即便有產生分析結論,但或許會有誤導的危險
  9. 傳統的分析方法,有提出投資common stocks的三個concept:(1) 適當與確定的殖利率、(2) 穩定與適當的營收紀錄、(3)擁有令人滿意的有形資產作為後盾 。但是在現代,是否適用?
  10. 投資commons stocks本身就有投機的特性,因為你在預測未知的未來Common stocks的價值完全在於它未來能賺多少錢
  11. Common stocks的new-era theory: 1. 殖利率與其價值只有一點點關係;2. 資產與營收能力毫無關係,故資產價值完全不重要;3. 過去的營收頂多指出未來的營收可能會有這樣的變化,但也不一定絕對如此
  12. 就投資來說,公司過去營收紀錄並不能提供可靠的指引
  13. 企業過去的營收與股利,並不能做為未來的營收與股利的指標。企業未來營收在於該公司在該產業的競爭力,以及公司經營者是否稱職
  14. 過去營收趨勢(trend of earnings),僅能說明未來預期的、可能的走向。當一家公司的營收持續成長,其未來就比較有可能有好的表現;若一家公司的營收持續萎縮或停滯,那未來比較有可能前景堪慮,這時你應該避免去碰這類公司的股票
  15. Investment is successful speculation
  16. 當企業的經營環境越不穩定,average earnings的數字就越不可靠,你就越不能用average earnings的數字來預測未來
  17. 相較於average earnings,trand of earnings (營收趨勢) 反而比較可靠,但是對於投資來說,還是不夠安全,因為未來的競爭可能更加激烈,或者出現經濟蕭條,都有可能造成營收衰退
  18. 企業如何運用多餘的資金(如投資研發、設備廠房等),會影響其未來的earning power
  19. 你很難判斷,現在這個價位是低點或高點,是便宜或昂貴
  20. 公司過往殖利率的記錄是一個很簡單的事實,且毋需做更進一步的分析,但是其重要性與否,很難做評估。它有可能是你選股的一個很重要的因素,也有可能是一個微不足道的因素。因為殖利率多寡端視該公司的股利政策,有些公司不管EPS賺多賺少,都發給你相同的股利
  21. 並非高殖利率的公司才是值得投資的公司,一切要看公司經營者對於資金的運用,若經營者是將資金用於廠房設備投資、研發投資,長期來說對於股東來說反而是有利的
  22. 若兩家公司的產業排名相仿,earning power也差不多,但是當一家給的股利比較多,通常這家的股價就會比較高,因為散戶會因貪圖股利而買這家公司的股票,進一步推升其股價
  23. 有些公司不管盈餘多寡都配與高股息,有些公司的股利政策則較保守,為了預防未來可能的rainy day,這時,當真的rainy day來臨時,後者會讓投資人受的傷小一些
  24. 一家企業的earning power,不會因為累積盈餘的增加而跟著提升,重點是經營決策者怎麼使用這些資金
  25. 股利政策是由董事會決定,所以其決定很有可能會受到私心所影響。如股利分給股東少一點,增加公司資金水位,然後再發獎金給自己
  26. Dividend rate 代表每股所配發的現金,如配發2元;dividend rate / return / yield 代表股利除以市場價格,如配2元除以100元(市場價格) =2%
  27. Earnings rate 代表年度EPS,如6元;Earnings ratio / return / yield 代表 EPS除以市場價格,如EPS 6元除以50(市場價值)= 12%
  28. 強迫的盈餘,只是虛幻的盈餘。為了維持公司的財務健康狀態,故累積盈餘是必須的,並不是累積盈餘就是好的管理的一部分
  29. 投資人並沒有可靠的股利分配來減輕獲利的不確定性,反而常得到的是任意地且不負責任的股利分配政策,反而加深投資人的獲利障礙
  30. 股票市場傾向於懲罰股利政策保守的公司的股價
  31. Financial analysis 是評價一間企業的關鍵,絕對不是CEO's letter
  32. 公司經營者對於一家公司影響甚鉅,從一些細微的觀察可以找到一些蛛絲馬跡,如一個不願意回答tough questions的CEO就是一個警訊;相同的,一個穿著珠光寶氣的CEO,也不是一個執行信賴的傢伙
  33. 有時候,股價大幅滑落,並沒有明顯的理由
  34. 依據個人情感、情緒所做的交易,通常都是錯誤的行為
  35. 不要投資態度不謹慎的經營者的公司股票;了解扣除非經常項目營收後的營收數字;資本結構中的債務可以強化營收,但是有其限度

2016/09/30

[Travel] 台東

多良車站
DSC00487

鹿野高台
DSC00499


台東太麻里海岸線
DSC00573


台東金針山
DSC00587

DSC00588

DSC00587


台東縣小野柳風景區
DSC00609


台東藝術鐵花村
DSC00646

DSC00650


太麻里 第一道曙光紀念公園
DSC00675


利吉惡地
DSC00699

2016/09/09

[Java] try-with-resources statement

JDK 7 introduces a new version of try statement known as try-with-resources statement. This feature add another way to exception handling with resources management,it is also referred to as automatic resource management.

If this class implements AutoClossable interface, then you can make good use of  try-with-resources statement. Do not need to close manually.
e

As-is (does not apply try-with-resources statement)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  FileOutputStream outputStream = null;
  File tmpFile = null;
  try {
      tmpFile = File.createTempFile("issue", ".xls");
      outputStream = new FileOutputStream(tmpFile);
      workbook.write(outputStream);
  } catch (FileNotFoundException e) {
      log.error(e.getMessage(), e);
  } finally {
      if (outputStream != null) {
          outputStream.close();
      }
      if (workbook != null) {
          workbook.close();
      }
  }


To-be (apply try-with-resources statement)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  File tmpFile = File.createTempFile("issue", ".xls");
  try(FileOutputStream outputStream  = new FileOutputStream(tmpFile)){
      workbook.write(outputStream);
  } catch (FileNotFoundException e) {
      log.error(e.getMessage(), e);
  } finally{
      if (workbook != null) {
          workbook.close();
      }
  }


Reference
[1] http://www.studytonight.com/java/try-with-resource-statement.php

2016/09/08

[Apache POI] How to freeze the first row

Problem
I am using Apache POI to write data into Microsoft Excel. 
If I would like to keep the first row on the top of this document when I scroll down, how to do it?


How-to
You can utilize createFreezePane API which provide by Apache POI to fulfill this requirement. Here has code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 @Override
 public File exportIssuesToXls(List < Issue > issues) throws IOException, RedmineException {
  Workbook workbook = new HSSFWorkbook();
  Sheet sheet = workbook.createSheet("issue list");
  CellStyle style = createCellStyle(workbook);

  //....

  //freeze the first row
  sheet.createFreezePane(0, 1);

  FileOutputStream outputStream = null;
  File tmpFile = null;
  try {
   tmpFile = File.createTempFile("issue", ".xls");
   log.info("tmpFile = " + tmpFile.getCanonicalPath());
   outputStream = new FileOutputStream(tmpFile);
   workbook.write(outputStream);

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   if (outputStream != null) {
    outputStream.close();
   }
   if (workbook != null) {
    workbook.close();
   }
  }
  return tmpFile;
 }



Reference
[1] https://stackoverflow.com/questions/17932575/apache-poi-locking-header-rows

2016/09/07

[Apache POI] How to add new line in cell data

Problem
I am using Apache POI to write data into Microsoft Excel.
I would like to break data into new line in some cell, how to do it?


How-to
You can add \n for the new line. For example,
1
Issue issue1 = new Issue(1, "查不到資料", "新建立", "正常", "蜘蛛人(2016-05-26 17:05:00):\n這個提議不錯,來做吧!\n\n浩克(2016-05-26 17:05:00):\n測試無誤\n\n");




Reference
[1] https://stackoverflow.com/questions/14646349/how-to-add-new-line-character-in-the-cell-comment-using-apache-poi

2016/09/06

[Microsoft Word] 如何新增檔名到頁尾

Problem
如果我要在 word 文件的頁尾插入 word 檔名,在 Microsoft Word 中如何做?

How-to
1. 點選頁尾


2. 插入 => 快速組件 => 功能變數


3. 選擇 file name,按下確定按鈕


4. 此時,就可以看到頁尾出現此檔案的檔名


Reference
[1] https://support.office.com/zh-tw/article/%E6%96%B0%E5%A2%9E%E6%AA%94%E5%90%8D%E5%88%B0%E9%A0%81%E9%A6%96%E6%88%96%E9%A0%81%E5%B0%BE-dc62245f-b6af-45b2-a521-17753fc3539e 

2016/09/05

[Windows 7] 參考到的帳戶目前已鎖定,且可能無法登入

Problem
有次在登入windows 7的帳號密碼時,因故輸入三次錯誤,導致電腦被鎖定,即便是重新開機以後還是出現相同的錯誤訊息


Solution
由於之前已經有過一次經驗,必須要有windows 7光碟進入才有辦法解決,故經過上次以後,有建立一個擁有 administration 權限的另外一個 user

所以在本次,我只要先重開機,按下F8,選擇以安全模式進入Windows,再利用另外一個帳號log in 進去

Steps:
1. 在我的電腦按下右鍵,選擇管理


2 .系統工具=>本機使用者與群組=>使用者


3. 於 lock 的使用者帳號按下滑鼠右鍵,選擇內容



4. 將最後一個 checkbox 勾勾取消,並重新開機即可



2016/09/04

[POI] Write /Read Excel Files in Java using Apache POI

Here has a simple example to demonstrate how to write / read excel file via Apache POI.

Add Apache POI dependency:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.14</version>
</dependency>

Create a POJO class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package albert.practice.poi;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Issue implements Serializable {
    /**
    * 
    */
    private static final long serialVersionUID = 1L;

    private Integer id;
    private String subject;
    private String status;
    private String priority;
}

Sample code is as bellows:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package albert.practice.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import lombok.extern.slf4j.Slf4j;

//http://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi
//http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/
@Slf4j
public class PoiExample {

    private static String excelFile = "D:" + File.separator + "issue.xls";

    public static void main(String[] args) throws IOException {
        PoiExample test = new PoiExample();
        test.writeExcel();

        test.readExcel(excelFile);
    }

    public List<Issue> readExcel(String excelFile) throws IOException {

        List<Issue> issues = new ArrayList<Issue>();

        InputStream inputStream = null;
        Workbook workbook = null;
        try {
            // 1. Create a Workbook.
            inputStream = new FileInputStream(new File(excelFile));
            workbook = new HSSFWorkbook(inputStream);

            // 2. Get first sheet
            Sheet sheet = workbook.getSheetAt(0);

            // 3. Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            int rowCount = 1;
            while (rowIterator.hasNext()) {
                // (1) ignore header row
                if (rowCount == 1) {
                rowIterator.next();
                rowCount++;
                }
                // (2) start to read each row from second row
                else {
                    Row row = rowIterator.next();
                    Integer id = Double.valueOf(row.getCell(0).getNumericCellValue()).intValue();
                    String subject = row.getCell(1).getStringCellValue();
                    String status = row.getCell(2).getStringCellValue();
                    String priority = row.getCell(3).getStringCellValue();

                    Issue issue = new Issue();
                    issue.setId(id);
                    issue.setSubject(subject);
                    issue.setStatus(status);
                    issue.setPriority(priority);

                    issues.add(issue);
                 }
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (workbook != null) {
                workbook.close();
            }
        }

        for (Issue issue : issues) {
            log.debug("issue = " + issue.toString());
        }

        return issues;
    }

    public void writeExcel() throws IOException {
        // 0. prepare dummy data
        List<Issue> issues = createDummyIssues();

        // 1. Create a Workbook.
        Workbook workbook = new HSSFWorkbook();

        // 2. Create a Sheet.
        Sheet sheet = workbook.createSheet("issue list");

        // 3. create cell style
        CellStyle style = createCellStyle(workbook);

        // 4. Repeat the following steps until all data is processed:
        // (1) Create a Row.
        // (2) Create Cells in a Row. Apply formatting using CellStyle.
        int rowCount = 0;
        Row headerRow = sheet.createRow(rowCount);
        writeHeader(headerRow, style);

        for (Issue issue : issues) {
            Row row = sheet.createRow(++rowCount);
            writeDataForEachRow(issue, row, style);
        }

        // 5. auto resize column width
        for (int i = 0; i < 4; i++) {
            sheet.autoSizeColumn(i);
        }

        // 6. Write to an OutputStream.
        // 7. Close the output stream.
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(excelFile);
            workbook.write(outputStream);

            log.debug("write issue data to excel file successfully");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
            if (workbook != null) {
                workbook.close();
            }
        }
    }

    private void writeHeader(Row headerRow, CellStyle style) {
        Cell cell = headerRow.createCell(0);
        cell.setCellValue("編號");
        cell.setCellStyle(style);

        cell = headerRow.createCell(1);
        cell.setCellValue("主旨");
        cell.setCellStyle(style);

        cell = headerRow.createCell(2);
        cell.setCellValue("狀態");
        cell.setCellStyle(style);

        cell = headerRow.createCell(3);
        cell.setCellValue("優先");
        cell.setCellStyle(style);
    }

    private void writeDataForEachRow(Issue issue, Row row, CellStyle style) {
        Cell cell = row.createCell(0);
        cell.setCellValue(issue.getId());
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue(issue.getSubject());
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue(issue.getStatus());
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue(issue.getPriority());
        cell.setCellStyle(style);
    }

    private CellStyle createCellStyle(Workbook workbook) {
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom((short) 1);
        cellStyle.setBorderTop((short) 1);
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setBorderRight((short) 1);
        cellStyle.setWrapText(true);

        return cellStyle;
    }

    private List<Issue> createDummyIssues() {
        Issue issue1 = new Issue(1, "查不到資料", "新建立", "正常");
        Issue issue2 = new Issue(2, "新增時發生錯誤", "新建立", "高");
        Issue issue3 = new Issue(3, "刪除失敗", "處理中", "高");

        return Arrays.asList(issue1, issue2, issue3);
    }

}


Excel file looks like: https://github.com/junyuo/AlbertGitProject/blob/master/src/albert/practice/poi/issue.xls


Reference
[1] http://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi
[2] http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/

2016/09/03

[jackson] How to generate json from java bean and vice versa

Problem
If I would like to do conversation with Java Bean to JSON and JSON to Java Bean, any library can be used?

How-To
Add dependency to your pom.xml
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

Create an User bean for test:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package albert.practice.json;

import java.io.Serializable;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private String id;
    private String login;
    private String mail;
}

Test code is as bellows:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package albert.practice.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JacksonTest {

    public static void main(String[] args) {
        JacksonTest test = new JacksonTest();
        String jsonString = test.convertOjbectToJson();

        List<User> users = test.convertJsonToObject(jsonString);
        for (User user : users) {
           log.debug("user = " + user.toString());
        }
    }

    public List<User> convertJsonToObject(String jsonString) {
        ObjectMapper mapper = new ObjectMapper();
        List<User> users = new ArrayList<User>();
        try {
            users = mapper.readValue(jsonString, new TypeReference<List<User>>() {
        });
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
        return users;
    }

    public String convertOjbectToJson() {
        List<User> users = createUsers();
        String jsonString = "";

        ObjectMapper mapper = new ObjectMapper();

        try {
            jsonString = mapper.writeValueAsString(users);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.debug("jsonString = " + jsonString);
        return jsonString;
    }

    private List<User> createUsers() {
        User albert = new User();
        albert.setId("1");
        albert.setLogin("albert");
        albert.setMail("albert@gmail.com");

        User mandy = new User();
        mandy.setId("2");
        mandy.setLogin("mandy");
        mandy.setMail("mandy@gmail.com");

        User verio = new User();
        verio.setId("3");
        verio.setLogin("verio");
        verio.setMail("verio@gmail.com");

        List<User> users = new ArrayList<User>();
        users.add(albert);
        users.add(mandy);
        users.add(verio);

        return users;
    }

}

print log:
1
2
3
4
2016-06-01 15:35:39.630 [main] DEBUG albert.practice.json.JacksonTest - jsonString = [{"id":"1","login":"albert","mail":"albert@gmail.com"},{"id":"2","login":"mandy","mail":"mandy@gmail.com"},{"id":"3","login":"verio","mail":"verio@gmail.com"}]
2016-06-01 15:35:39.670 [main] DEBUG albert.practice.json.JacksonTest - user = User(id=1, login=albert, mail=albert@gmail.com)
2016-06-01 15:35:39.670 [main] DEBUG albert.practice.json.JacksonTest - user = User(id=2, login=mandy, mail=mandy@gmail.com)
2016-06-01 15:35:39.670 [main] DEBUG albert.practice.json.JacksonTest - user = User(id=3, login=verio, mail=verio@gmail.com)

Reference
[1] http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
[2] https://stackoverflow.com/questions/28821715/java-lang-classcastexception-java-util-linkedhashmap-cannot-be-cast-to-com-test