Skip to content

Commit ac08f0a

Browse files
nowhereslyfpoyer
authored andcommitted
Clean and optimize code
Integrated most of the optimisation/improvements proposed by @nowheresly in PR #23, making use of Java 8 lambdas and streams when possible.
1 parent 876024f commit ac08f0a

4 files changed

Lines changed: 45 additions & 50 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
# properties file holding connection data for integration test
1212
demodb.properties
1313

14+
# Intellij IDEA stuff
15+
.idea/*
16+
*.iml

src/main/java/com/debortoliwines/odoo/api/ObjectAdapter.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import java.util.ArrayList;
2424
import java.util.Date;
2525
import java.util.HashMap;
26+
import java.util.List;
2627
import java.util.Map;
2728
import java.util.TimeZone;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.stream.Stream;
2831

2932
import org.apache.xmlrpc.XmlRpcException;
3033

@@ -50,17 +53,17 @@ public class ObjectAdapter {
5053
// the database for every new object.
5154
// Bulk loads/reads can become very slow if every adapter requires a call
5255
// back to the server
53-
private static ArrayList<String> objectNameCache = new ArrayList<>();
56+
private static final List<String> objectNameCache = new ArrayList<>();
5457

5558
// Object workflow signal cache so the adapter doesn't have to reread signal
5659
// names from the database for every workflow call.
57-
private static ArrayList<String> signalCache = new ArrayList<>();
60+
private static final List<String> signalCache = new ArrayList<>();
5861

5962
// Cache used to store the name_get result of an model to cater for
6063
// many2many relations in the import function
6164
// It is cleared every time the import function is called for a specific
6265
// object
63-
private HashMap<String, HashMap<String, String>> modelNameCache = new HashMap<>();
66+
private final Map<String, Map<String, String>> modelNameCache = new ConcurrentHashMap<>();
6467

6568
/**
6669
* Default constructor
@@ -238,9 +241,7 @@ public RowCollection readObject(Object[] ids, String[] fields) throws XmlRpcExce
238241
* sortedResults[idList.indexOf(id)] = result; }
239242
****/
240243

241-
RowCollection rows = new RowCollection(results, fieldCol);
242-
243-
return rows;
244+
return new RowCollection(results, fieldCol);
244245
}
245246

246247
/***
@@ -487,9 +488,9 @@ private Object[] fixImportData(Row inputRow) throws OpeneERPApiException, XmlRpc
487488
* import. Replace the ID list passed in with a Name list for
488489
* the import_data function that we are about to call
489490
*/
490-
HashMap<String, String> idToName = null;
491+
Map<String, String> idToName;
491492
if (!modelNameCache.containsKey(fld.getRelation())) {
492-
idToName = new HashMap<String, String>();
493+
idToName = new HashMap<>();
493494
Object[] ids = command.searchObject(fld.getRelation(), new Object[] {});
494495
Object[] names = command.nameGet(fld.getRelation(), ids);
495496
for (int j = 0; j < ids.length; j++) {
@@ -533,19 +534,17 @@ private Object[] fixImportData(Row inputRow) throws OpeneERPApiException, XmlRpc
533534
}
534535

535536
private String[] getFieldListForImport(FieldCollection currentFields) {
537+
return Stream
538+
.concat(Stream.of(".id"),
539+
currentFields.stream().map(ObjectAdapter::getFieldNameForImport))
540+
.toArray(String[]::new);
536541

537-
ArrayList<String> fieldList = new ArrayList<String>();
538-
fieldList.add(".id");
539-
540-
for (Field field : currentFields) {
541-
if (field.getType() == FieldType.MANY2ONE)
542-
fieldList.add(field.getName() + ".id");
543-
else
544-
fieldList.add(field.getName());
545-
}
546-
547-
return fieldList.toArray(new String[fieldList.size()]);
542+
}
548543

544+
private static String getFieldNameForImport(Field field) {
545+
// Return field name, adding ".id" if type is MANY2ONE
546+
return field.getType() == FieldType.MANY2ONE ? field.getName() + ".id"
547+
: field.getName();
549548
}
550549

551550
/**
@@ -723,7 +722,7 @@ public RowCollection searchAndReadObject(FilterCollection filter, String[] field
723722
public RowCollection searchAndReadObject(final FilterCollection filter, final String[] fields, int offset,
724723
int limit, String order) throws XmlRpcException, OpeneERPApiException {
725724

726-
String[] fieldArray = (fields == null ? new String[] {} : fields);
725+
String[] fieldArray = fields == null ? new String[] {} : fields;
727726
Object[] preparedFilters = validateFilters(filter);
728727
Object[] idList = (Object[]) command.searchObject(modelName, preparedFilters, offset, limit, order, false);
729728

@@ -938,7 +937,7 @@ public FieldCollection callFieldsFunction(String functionName, Object[] paramete
938937
}
939938

940939
if (fldDetails == null)
941-
fldDetails = new HashMap<String, Object>();
940+
fldDetails = new HashMap<>();
942941

943942
if (!fldDetails.containsKey("name"))
944943
fldDetails.put("name", field);
@@ -991,9 +990,7 @@ public RowCollection callFunction(String functionName, Object[] parameters, Fiel
991990
if (fieldCol == null)
992991
fieldCol = callFieldsFunction(functionName, parameters);
993992

994-
RowCollection rows = new RowCollection(results, fieldCol);
995-
996-
return rows;
993+
return new RowCollection(results, fieldCol);
997994
}
998995

999996
/**

src/main/java/com/debortoliwines/odoo/api/Session.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
*/
3737
public class Session {
3838

39+
private static final String LINE_SEPARATOR_SYSTEM_PROPERTY = "line.separator";
40+
private static final String LINE_SEPARATOR = System.getProperty(LINE_SEPARATOR_SYSTEM_PROPERTY);
3941
private String host;
4042
private int port;
4143
private String databaseName;
@@ -162,24 +164,23 @@ void checkDatabasePresenceSafe() {
162164
}
163165
}
164166

165-
void checkDatabasePresence() throws XmlRpcException, Exception {
167+
void checkDatabasePresence() throws XmlRpcException {
166168
ArrayList<String> dbList = getDatabaseList(protocol, host, port);
167-
if (dbList.indexOf(databaseName) < 0) {
168-
StringBuffer dbListBuff = new StringBuffer();
169-
for (String dbName : dbList)
170-
dbListBuff.append(dbName + System.getProperty("line.separator"));
169+
if (!dbList.contains(databaseName)) {
170+
StringBuilder messageBuilder = new StringBuilder("Error while connecting to Odoo. Database [")
171+
.append(databaseName).append("] was not found in the following list: ").append(LINE_SEPARATOR)
172+
.append(LINE_SEPARATOR).append(String.join(LINE_SEPARATOR, dbList)).append(LINE_SEPARATOR);
171173

172-
throw new Exception("Error while connecting to Odoo. Database [" + databaseName + "] "
173-
+ " was not found in the following list: " + System.getProperty("line.separator")
174-
+ System.getProperty("line.separator") + dbListBuff.toString());
174+
throw new IllegalStateException(messageBuilder.toString());
175175
}
176176
}
177177

178178
private synchronized static void startConnecting(){
179179
while (Session.connecting){
180180
try {
181181
Thread.sleep(100);
182-
} catch (Exception e) {
182+
} catch (InterruptedException e) {
183+
e.printStackTrace();
183184
}
184185
}
185186
Session.connecting = true;

src/main/java/com/debortoliwines/odoo/api/helpers/FlatViewHelper.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Collections;
2424
import java.util.Comparator;
2525
import java.util.Date;
26+
import java.util.stream.Collectors;
2627

2728
import org.apache.xmlrpc.XmlRpcException;
2829

@@ -112,31 +113,24 @@ public static FlatViewFieldCollection getFields(String objectName, FieldCollecti
112113
*/
113114
public static String[] getFieldNames(String objectName, FieldCollection fields) throws XmlRpcException {
114115

115-
ArrayList<String> fieldNames = new ArrayList<String>();
116-
117-
for (FlatViewField fld : getFields(objectName, fields)){
118-
fieldNames.add(fld.getName());
119-
}
120-
121-
return fieldNames.toArray(new String[0]);
116+
return getFields(objectName, fields).stream()
117+
.map(f -> f.getName())
118+
.toArray(String[]::new);
122119
}
123120

124121
/**
125122
* Returns the original field names from a flattened out view collection
126-
* @param objectName Object that the fields are for, for eg res.partner
127123
* @param fields Flattened out field collection
128124
* @return Original list of fields that the flattened out collection was built on
129125
*/
130-
public static String[] getOriginalFieldNames(String objectName, FlatViewFieldCollection fields){
131-
ArrayList<String> fieldNames = new ArrayList<String>();
132-
133-
for (FlatViewField fld : fields){
134-
if (fieldNames.indexOf(fld.getSourceField().getName()) < 0){
135-
fieldNames.add(fld.getName());
136-
}
137-
}
126+
public static String[] getOriginalFieldNames(FlatViewFieldCollection fields){
138127

139-
return fieldNames.toArray(new String[0]);
128+
return fields.stream()
129+
.collect(Collectors.toMap(
130+
f -> f.getSourceField().getName(),
131+
f -> f.getName(),
132+
(f1, f2) -> f1))
133+
.values().toArray(new String[0]);
140134
}
141135

142136
/**

0 commit comments

Comments
 (0)