Sunday, October 18, 2009

Amending Code Style & Templates Programmatically via Eclipse PDE


Having a common code style and template in a development team is a good software development practice. Fromatting source code with identical style and template reduces synchronizing issues (such as merge, compare, check-in or check-out from a version controller, SVN/CVS) between developers. Many modern IDE's provide custom code format and template functionalities. In eclipse, under Java > Code Style>Formatter and Code Templates menu, you can amend or create code styles and templates for your wish.

In eclipse, once a template or code formatter is created, it can be exported and then distributed among team members. Then, each developer has to manually import the code style and formatter.
This is a manual task.

In this blog, i will give source code examples how to apply a code style or template programmatically with Eclipse plug-in development (PDE).

Code Style (Formatter)

Let's assume that we have sample code style and template files (exported manually from eclipse and in xml format). For applying code style, we obtains default code style options with JavaCore.getDefaultOptions() function and then ammend or insert new code style with their new/updated values. And finally save back options.

// Get the default options
Hashtable options = JavaCore.getDefaultOptions();
// Amend the value of an option
options.put(JavaCore.COMPILER_PB_DEPRECATION, JavaCore.ERROR);
// Save the new options
JavaCore.setOptions(options);

Following source code shows how a code format file in xml format is applied programmatically with eclipse plug-in. Please note, xmltools.jar library from mycila is used for parsing xml file.

 package com.denizstij.plugins.settings.codeformatter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.core.JavaCore;
import com.mycila.xmltool.XMLDoc;
import com.mycila.xmltool.XMLTag;
/**
* Programmatically apply code styles via eclipse pde
*
* @author deniz.turan (http://denizstij.blogspot.com/)
* Oct-2009
*/
public class CodeFormatter {
private String CODESTYLE_FILE_REL_PATH="configs/CodeStyle.xml";
private Map <String, String> codeStylesMap= new HashMap<String, String>();
private static CodeFormatter instance = new CodeFormatter();
private CodeFormatter() {
}
public static CodeFormatter getInstance() {
return instance;
}
public void populateCodeStyle() throws Exception{
codeStylesMap.clear();
InputStream is= CodeFormatter.class.getResourceAsStream(CODESTYLE_FILE_REL_PATH);
if (is==null){
return ;
}
// xmltool-3.0.jar from mycila needs to be class path
XMLTag from = XMLDoc.from(is, false);
if (from==null){
return;
}
XMLTag profileNode = from.gotoChild("profile");
Iterable<XMLTag> settings = profileNode.getChilds();
Iterator<XMLTag> iterator = settings.iterator();
while (iterator.hasNext()){
XMLTag next = iterator.next();
String id = next.getAttribute("id");
String value = next.getAttribute("value");
codeStylesMap.put(id, value);
}
}
public void applyCodeFormatter() throws Exception {
populateCodeStyle();
Hashtable options = JavaCore.getDefaultOptions();
Set<String> keySet = codeStylesMap.keySet();
for (String id:keySet){
String value= codeStylesMap.get(id);
options.put(id, value);
}
JavaCore.setOptions(options);
}
}

Code Template


Similar to code style, code template can be applied by using TemplateStore and TemplatePersistenceData classes. We can use JavaPlugin.getDefault().getCodeTemplateStore() to obtain current TemplateStore. Please note, these classes are in internal package, therefore, in further version of eclipse pde, it would cause issues (tested on eclipse 3.5)

 package com.denizstij.plugins.settings.codeformatter;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
import org.eclipse.jface.text.templates.persistence.TemplateStore;
/**
* Programmatically apply code templates via eclipse pde
*
* @author deniz.turan (http://denizstij.blogspot.com/)
* Oct-2009
*
*/
public class CodeTemplate {
private String CODE_TEMPLATE_FILE_REL_PATH = "configs/CodeTemplate.xml";
private TemplatePersistenceData[] templateData;
private TemplateStore codeTemplateStore;
private static CodeTemplate instance = new CodeTemplate();
private CodeTemplate() {
init();
}
public static CodeTemplate getInstance() {
return instance;
}
public void applyCodeTemplates() throws Exception {
importCodeTemplate();
}
private void init() {
codeTemplateStore = JavaPlugin.getDefault().getCodeTemplateStore();
templateData = codeTemplateStore.getTemplateData(true);
}
private void importCodeTemplate() throws Exception {
InputStream input = CodeTemplate.class.getResourceAsStream(CODE_TEMPLATE_FILE_REL_PATH) ;
if (input == null) {
return;
}
TemplateReaderWriter reader = new TemplateReaderWriter();
TemplatePersistenceData[] datas = reader.read(input, null);
for (int i = 0; i < datas.length; i++) {
updateTemplate(datas[i]);
}
codeTemplateStore.save();
}
private void updateTemplate(TemplatePersistenceData data) {
for (int i = 0; i < templateData.length; i++) {
String id = templateData[i].getId();
if (id != null && id.equals(data.getId())) {
templateData[i].setTemplate(data.getTemplate());
break;
}
}
}
}

Tuesday, October 13, 2009

Intrinsic Value of Shares

Value Investing is a type of investing in which the trader buys shares in cheap price and sell them in higher price. Sure this is common feature of all tradings. But following differentiate it from other tradings:
  • It is based on fundamental of company rather than technical analysis
  • It is long term investing (more than 3 years)
According to this trading paradigm, intrinsic value of a share is calculated based on fundamentals such as EPS, P/E, Dividend, Dividend Payout Ratio, EPS-Growth 10 year.

Since it is a long term investment, someone can wonder if a share's price is fair at a given time. In this text, i will explain a very simple technique to calculate intrinsic value of a share which is described in Getting Started in Value Investing by Charles Mizrahi. Please note that, this technique is more accurate on shares of which return on equity (ROE) in last 10 years is more than 15 and each year earning is increased around 10%. I will use HMV Group (LSE.HMV) share to explain the steps. Financial of HMV at point of writing this article is as follows:
  • Price (P):112.70p
  • EPS:11.10p
  • PE:10.15
  • Dividend:5.6p
  • Divident Payout Ratio (DPR):66.67%
  • EPS-Growth:20.65 %
1-) Estimate EPS Growth (EPS_G) for the end of next five years.
  • If average EPS Growth is greater than 15% over the past 10 years, then we take EPS_G as 15%.
  • Otherwise use 10% as EPS_G
For HMV, even though EPS_G is around 20.65, i would like to take EPS_G as 10% for bad case scenario.

2-) Estimate P/E (PE_G) for the end of next five years
  • If average PE is above 20 over the past 10 years, then we take PE_G as 17
  • Otherwise use 12 as PE_G
For HMV, I would like to take PE_G as 10 for a more accurate estimate.

3-) Calculate EPS by the end of 5 (N) years
For this, we use simple interest rate equation :

EPS_N= EPS*((1+EPS_G)^N)= 11.10*((1+0.10)^5)=17.88

So by the end of 5 years, we expect EPS will be 17.88 p

4-) Calculate Price by the end of 5 (N) years:
Since Price= PE*EPS, therefore:

P_N=PE_G*EPS_N=10*17.88=178.8p;

which is the price of HMV share by the end of 5 years

5-)Calculate dividends for next five years
We project next five (N) years dividends by using past five years. We sum all dividends and then multiply it with Dividend Payout Ratio (DPR).

D_N= DPR*(d_n+d_n-1+d_n-2+d_n-3+.... d_N-1)

where d_n is the past year's dividend.
So dividend of HMV will be: D_5=0.6657*(5.6+7.4+7.4+7.4+6.9)=23.1

6-) Calculate Total Share
Total share price is:

T_N=P_N+D_N=178.8+23.1= 201.9p

7-) Calculate Present value
Based on a return rate (r) we calculate present value of T_N or in other words intrinsic value of share (IV):

IV_N=TN*((1+r)^-N)

Let say, we would like to have 15 % return on our investment, then intrinsic value of HMV is:

IV=201.9*((1+0.15)^-5)= 100.3800.

At the moment price of HMV is 112.70p. Therefore, we can conclude that HMV is expensive according to its fundamental and return rate. We should wait HMV price goes more down in order to make profitable long term investment.

Please note this is a simple technique to judge if a share has a fair price at a time. There are other fundamental factors have to be analysed before using technique.


Saturday, October 10, 2009

Eligible LSE Equities for Share &Stock ISA

I compiled a list of stocks traded on London Stock Exchange (LSE) which eligible for holding in a share & stock ISA. Please note, since HM Revenue publishes only a guideline for eligibility, some stocks in this would be not eligibible in some ISA management companes ( especially online brokers).

Please click here to download the list which comprimes sector, epic, name,isin, currency and indices of the stocks.