GenericIdentityGenerator.java

biz/hammurapi/sql/GenericIdentityGenerator.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 17:9
Java Inspector 089 Constructor documentation is too short. It is only 1 words. Should be at least 3 words. 2 24:9
Java Inspector 089 Undocumented method 2 33:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 25:45
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 24:9
Java Inspector 054 Discourage usage of instance variables like a, j by enforcing minimal variable name length (3). 3 16:9

Source code

1package biz.hammurapi.sql;
2
3import java.sql.Connection;
4import java.sql.SQLException;
5import java.text.MessageFormat;
6
7/**
8 * Generates identities by executing specified SQL statement
9 * @author Pavel Vlasov
10 *
11 */
12public class GenericIdentityGenerator implements IdentityGenerator {
13
14
15 private boolean invariant;
16 private MessageFormat mf;
17 private Object[] args=new Object[] {null};
18 private String sql;
19
20 /**
21 * Constructor
22 * @param pattern - SQL statement pattern. {0} stands for table name.
23 */
24 public GenericIdentityGenerator(String pattern) {
25 invariant = pattern.indexOf("{0}")==-1;
26 if (invariant) {
27 this.sql=pattern;
28 } else {
29 mf=new MessageFormat(pattern);
30 }
31 }
32
33 public int generate(Connection con, String name) throws SQLException {
34 String esql;
35 if (invariant) {
36 esql=this.sql;
37 } else {
38 esql=mf.format(args, new StringBuffer(), null).toString();
39 }
40
41 return new SQLProcessor(con, null).projectSingleInt(esql, null);
42 }
43
44}
45