TableIdentityGenerator.java

biz/hammurapi/sql/TableIdentityGenerator.java

Violations

Inspector Message Severity Location
Java Inspector 031 Switch statement case without 'break' 1 54:17
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 25:9
Java Inspector 089 Undocumented parameter primaryKeysTable 2 17:9
Java Inspector 089 Constructor documentation is too short. It is only 1 words. Should be at least 3 words. 2 32:9
Java Inspector 089 Undocumented method 2 45:9
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 39:30
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 54:22
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 18:40
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 33:21
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 35:44
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 60:57
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 17:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 32:9
Java Inspector 054 Discourage usage of instance variables like a, j by enforcing minimal variable name length (3). 3 22: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 using SQLProcessor nextPK method
9 * @author Pavel Vlasov
10 *
11 */
12public class TableIdentityGenerator implements IdentityGenerator {
13
14 /**
15 * Default constructor with "{0}" pattern
16 */
17 public TableIdentityGenerator(String primaryKeysTable) {
18 this(primaryKeysTable, "{0}");
19 }
20
21 private int mode; // 0 - passthrough 1 - invariant 2 - format
22 private MessageFormat mf;
23 private String keyName;
24 private String primaryKeysTable;
25 private Object[] args=new Object[] {null};
26
27 /**
28 * Constructor
29 * @param primaryKeysTable - Table which hold primary keys. See SQLProcessor documentation.
30 * @param pattern - Key name pattern. {0} stands for table name.
31 */
32 public TableIdentityGenerator(String primaryKeysTable, String pattern) {
33 if ("{0}".equals(pattern)) {
34 mode=0;
35 } else if (pattern.indexOf("{0}")==-1) {
36 mode=1;
37 this.keyName=pattern;
38 } else {
39 mode=2;
40 mf=new MessageFormat(pattern);
41 }
42 this.primaryKeysTable=primaryKeysTable;
43 }
44
45 public int generate(Connection con, String name) throws SQLException {
46 String ename;
47 switch (mode) {
48 case 0:
49 ename=name;
50 break;
51 case 1:
52 ename=keyName;
53 break;
54 case 2:
55 synchronized (mf) {
56 args[0]=name;
57 ename=mf.format(args, new StringBuffer(), null).toString();
58 }
59 default:
60 throw new IllegalStateException("Invalid mode: "+mode);
61 }
62
63 return new SQLProcessor(con, null).nextPK(primaryKeysTable, ename);
64 }
65
66}
67