From 18ea2f7faa3354c8f8282f15a16510a2000f56e2 Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Sun, 29 Jun 2008 18:47:15 +0000 Subject: [PATCH] Generics, enhanced-for, Number.valueOf. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@672660 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/ofbiz/datafile/DataFile.java | 9 +++-- .../ofbiz/datafile/DataFile2EntityXml.java | 14 +++----- .../src/org/ofbiz/datafile/ModelDataFile.java | 9 +++-- .../ofbiz/datafile/ModelDataFileReader.java | 35 +++++++++---------- .../src/org/ofbiz/datafile/ModelRecord.java | 11 +++--- .../src/org/ofbiz/datafile/Record.java | 31 ++++++++-------- .../org/ofbiz/datafile/RecordIterator.java | 7 ++-- 7 files changed, 51 insertions(+), 65 deletions(-) diff --git a/framework/datafile/src/org/ofbiz/datafile/DataFile.java b/framework/datafile/src/org/ofbiz/datafile/DataFile.java index b47b1b5052..6932e9f274 100644 --- a/framework/datafile/src/org/ofbiz/datafile/DataFile.java +++ b/framework/datafile/src/org/ofbiz/datafile/DataFile.java @@ -44,7 +44,7 @@ public class DataFile { public static final String module = DataFile.class.getName(); /** List of record in the file, contains Record objects */ - protected List records = new ArrayList(); + protected List records = new ArrayList(); /** Contains the definition for the file */ protected ModelDataFile modelDataFile; @@ -98,7 +98,7 @@ public class DataFile { return modelDataFile; } - public List getRecords() { + public List getRecords() { return records; } @@ -221,9 +221,8 @@ public class DataFile { writeRecords(outStream, this.records); } - protected void writeRecords(OutputStream outStream, List records) throws DataFileException { - for (int r = 0; r < records.size(); r++) { - Record record = (Record) records.get(r); + protected void writeRecords(OutputStream outStream, List records) throws DataFileException { + for (Record record: records) { String line = record.writeLineString(modelDataFile); try { diff --git a/framework/datafile/src/org/ofbiz/datafile/DataFile2EntityXml.java b/framework/datafile/src/org/ofbiz/datafile/DataFile2EntityXml.java index dc8b2f285b..ad0c57b841 100644 --- a/framework/datafile/src/org/ofbiz/datafile/DataFile2EntityXml.java +++ b/framework/datafile/src/org/ofbiz/datafile/DataFile2EntityXml.java @@ -50,13 +50,10 @@ public class DataFile2EntityXml { outFile.newLine(); outFile.write(""); outFile.newLine(); - List records = dataFile.getRecords(); - for(int r=0; r records = new ArrayList(); public ModelRecord getModelRecord(String recordName) { - for (int i = 0; i < records.size(); i++) { - ModelRecord curRecord = (ModelRecord) records.get(i); + for (ModelRecord curRecord: records) { if (curRecord.name.equals(recordName)) { return curRecord; @@ -134,11 +133,11 @@ public class ModelDataFile { this.description = description; } - public List getRecords() { + public List getRecords() { return records; } - public void setRecords(List records) { + public void setRecords(List records) { this.records = records; } } diff --git a/framework/datafile/src/org/ofbiz/datafile/ModelDataFileReader.java b/framework/datafile/src/org/ofbiz/datafile/ModelDataFileReader.java index 1a939cd79f..356fc17c37 100644 --- a/framework/datafile/src/org/ofbiz/datafile/ModelDataFileReader.java +++ b/framework/datafile/src/org/ofbiz/datafile/ModelDataFileReader.java @@ -47,19 +47,19 @@ public class ModelDataFileReader { public static final String module = ModelDataFileReader.class.getName(); - public static UtilCache readers = new UtilCache("ModelDataFile", 0, 0); + public static UtilCache readers = new UtilCache("ModelDataFile", 0, 0); public URL readerURL = null; - public Map modelDataFiles = null; + public Map modelDataFiles = null; public static ModelDataFileReader getModelDataFileReader(URL readerURL) { ModelDataFileReader reader = null; - reader = (ModelDataFileReader) readers.get(readerURL); + reader = readers.get(readerURL); if (reader == null) { // don't want to block here synchronized (ModelDataFileReader.class) { // must check if null again as one of the blocked threads can still enter - reader = (ModelDataFileReader) readers.get(readerURL); + reader = readers.get(readerURL); if (reader == null) { if (Debug.infoOn()) Debug.logInfo("[ModelDataFileReader.getModelDataFileReader] : creating reader.", module); reader = new ModelDataFileReader(readerURL); @@ -82,12 +82,12 @@ public class ModelDataFileReader { getModelDataFiles(); } - public Map getModelDataFiles() { + public Map getModelDataFiles() { if (modelDataFiles == null) { // don't want to block here synchronized (ModelDataFileReader.class) { // must check if null again as one of the blocked threads can still enter if (modelDataFiles == null) { // now it's safe - modelDataFiles = new HashMap(); + modelDataFiles = new HashMap(); UtilTimer utilTimer = new UtilTimer(); @@ -153,10 +153,10 @@ public class ModelDataFileReader { * @return An DataFile object describing the specified dataFile of the specified descriptor file. */ public ModelDataFile getModelDataFile(String dataFileName) { - Map ec = getModelDataFiles(); + Map ec = getModelDataFiles(); if (ec != null) { - return (ModelDataFile) ec.get(dataFileName); + return ec.get(dataFileName); } else { return null; } @@ -165,8 +165,8 @@ public class ModelDataFileReader { /** Creates a Iterator with the dataFileName of each DataFile defined in the specified XML DataFile Descriptor file. * @return A Iterator of dataFileName Strings */ - public Iterator getDataFileNamesIterator() { - Collection collection = getDataFileNames(); + public Iterator getDataFileNamesIterator() { + Collection collection = getDataFileNames(); if (collection != null) { return collection.iterator(); @@ -178,8 +178,8 @@ public class ModelDataFileReader { /** Creates a Collection with the dataFileName of each DataFile defined in the specified XML DataFile Descriptor file. * @return A Collection of dataFileName Strings */ - public Collection getDataFileNames() { - Map ec = getModelDataFiles(); + public Collection getDataFileNames() { + Map ec = getModelDataFiles(); return ec.keySet(); } @@ -218,8 +218,7 @@ public class ModelDataFileReader { } } - for (int i = 0; i < dataFile.records.size(); i++) { - ModelRecord modelRecord = (ModelRecord) dataFile.records.get(i); + for (ModelRecord modelRecord: dataFile.records) { if (modelRecord.parentName.length() > 0) { ModelRecord parentRecord = dataFile.getModelRecord(modelRecord.parentName); @@ -250,7 +249,7 @@ public class ModelDataFileReader { tempStr = UtilXml.checkEmpty(recordElement.getAttribute("tc-isnum")); if (tempStr != null && tempStr.length() > 0) { - record.tcIsNum = Boolean.valueOf(tempStr).booleanValue(); + record.tcIsNum = Boolean.parseBoolean(tempStr); } tempStr = UtilXml.checkEmpty(recordElement.getAttribute("tc-position")); @@ -313,17 +312,17 @@ public class ModelDataFileReader { tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("prim-key")); if (tempStr != null && tempStr.length() > 0) { - field.isPk = Boolean.valueOf(tempStr).booleanValue(); + field.isPk = Boolean.parseBoolean(tempStr); } tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("ignored")); if (tempStr != null && tempStr.length() > 0) { - field.ignored = Boolean.valueOf(tempStr).booleanValue(); + field.ignored = Boolean.parseBoolean(tempStr); } tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("expression")); if (tempStr != null && tempStr.length() > 0) { - field.expression = Boolean.valueOf(tempStr).booleanValue(); + field.expression = Boolean.parseBoolean(tempStr); } return field; diff --git a/framework/datafile/src/org/ofbiz/datafile/ModelRecord.java b/framework/datafile/src/org/ofbiz/datafile/ModelRecord.java index 3e131e5ff8..f82551fd69 100644 --- a/framework/datafile/src/org/ofbiz/datafile/ModelRecord.java +++ b/framework/datafile/src/org/ofbiz/datafile/ModelRecord.java @@ -65,14 +65,13 @@ public class ModelRecord { public String limit = ""; public ModelRecord parentRecord = null; - public List childRecords = new ArrayList(); + public List childRecords = new ArrayList(); /** List of the fields that compose this record */ - public List fields = new ArrayList(); + public List fields = new ArrayList(); ModelField getModelField(String fieldName) { - for (int i = 0; i < fields.size(); i++) { - ModelField curField = (ModelField) fields.get(i); + for (ModelField curField: fields) { if (curField.name.equals(fieldName)) { return curField; @@ -105,11 +104,11 @@ public class ModelRecord { this.parentName = parentName; } - public List getFields() { + public List getFields() { return fields; } - public void setFields(List fields) { + public void setFields(List fields) { this.fields = fields; } } diff --git a/framework/datafile/src/org/ofbiz/datafile/Record.java b/framework/datafile/src/org/ofbiz/datafile/Record.java index 4a8e7d7956..35fcf7270d 100644 --- a/framework/datafile/src/org/ofbiz/datafile/Record.java +++ b/framework/datafile/src/org/ofbiz/datafile/Record.java @@ -36,7 +36,7 @@ import java.util.NoSuchElementException; public class Record implements Serializable { /** Contains a map with field data by name */ - protected Map fields; + protected Map fields; /** Contains the name of the record definition */ protected String recordName; @@ -45,7 +45,7 @@ public class Record implements Serializable { protected transient ModelRecord modelRecord; protected Record parentRecord = null; - protected List childRecords = new ArrayList(); + protected List childRecords = new ArrayList(); /** Creates new Record */ protected Record(ModelRecord modelRecord) { @@ -53,16 +53,16 @@ public class Record implements Serializable { throw new IllegalArgumentException("Cannont create a Record with a null modelRecord parameter"); this.recordName = modelRecord.name; this.modelRecord = modelRecord; - this.fields = new HashMap(); + this.fields = new HashMap(); } /** Creates new Record from existing Map */ - protected Record(ModelRecord modelRecord, Map fields) { + protected Record(ModelRecord modelRecord, Map fields) { if (modelRecord == null) throw new IllegalArgumentException("Cannont create a Record with a null modelRecord parameter"); this.recordName = modelRecord.name; this.modelRecord = modelRecord; - this.fields = (fields == null ? new HashMap() : new HashMap(fields)); + this.fields = (fields == null ? new HashMap() : new HashMap(fields)); } public String getRecordName() { @@ -259,7 +259,7 @@ public class Record implements Serializable { double divisor = Math.pow(10.0, decimalPlaces); number = number / divisor; - set(name, new Double(number)); + set(name, Double.valueOf(number)); } // standard types else if (fieldType.equals("java.lang.String") || fieldType.equals("String")) set(name, value); @@ -281,11 +281,11 @@ public class Record implements Serializable { else if (fieldType.equals("java.lang.Double") || fieldType.equals("Double")) set(name, Double.valueOf(value)); else if (fieldType.equals("LEShort")) - set(name, new Short(readLEShort(value.getBytes()))); + set(name, Short.valueOf(readLEShort(value.getBytes()))); else if (fieldType.equals("LEInteger")) - set(name, new Integer(readLEInt(value.getBytes()))); + set(name, Integer.valueOf(readLEInt(value.getBytes()))); else if (fieldType.equals("LELong")) - set(name, new Long(readLELong(value.getBytes()))); + set(name, Long.valueOf(readLELong(value.getBytes()))); else { throw new IllegalArgumentException("Field type " + fieldType + " not currently supported. Sorry."); } @@ -380,8 +380,7 @@ public class Record implements Serializable { StringBuilder lineBuf = new StringBuilder(); - for (int f = 0; f < modelRecord.fields.size(); f++) { - ModelField modelField = (ModelField) modelRecord.fields.get(f); + for (ModelField modelField: modelRecord.fields) { String data = this.getFixedString(modelField.name); // if field is null (not set) then assume we want to pad the field @@ -444,7 +443,7 @@ public class Record implements Serializable { return parentRecord; } - public List getChildRecords() { + public List getChildRecords() { return childRecords; } @@ -469,7 +468,7 @@ public class Record implements Serializable { * @throws DataFileException Exception thown for various errors, generally has a nested exception * @return */ - public static Record createRecord(ModelRecord modelRecord, Map fields) throws DataFileException { + public static Record createRecord(ModelRecord modelRecord, Map fields) throws DataFileException { Record record = new Record(modelRecord, fields); return record; @@ -485,8 +484,7 @@ public class Record implements Serializable { public static Record createRecord(String line, int lineNum, ModelRecord modelRecord) throws DataFileException { Record record = new Record(modelRecord); - for (int i = 0; i < modelRecord.fields.size(); i++) { - ModelField modelField = (ModelField) modelRecord.fields.get(i); + for (ModelField modelField: modelRecord.fields) { String strVal = null; @@ -528,8 +526,7 @@ public class Record implements Serializable { else { st = new StringTokenizer(line, "" + delimiter, true); } - for (int i = 0; i < modelRecord.fields.size(); i++) { - ModelField modelField = (ModelField) modelRecord.fields.get(i); + for (ModelField modelField: modelRecord.fields) { String strVal = null; if (modelField.expression) { diff --git a/framework/datafile/src/org/ofbiz/datafile/RecordIterator.java b/framework/datafile/src/org/ofbiz/datafile/RecordIterator.java index ddfb3476a9..e15e46d524 100644 --- a/framework/datafile/src/org/ofbiz/datafile/RecordIterator.java +++ b/framework/datafile/src/org/ofbiz/datafile/RecordIterator.java @@ -163,7 +163,7 @@ public class RecordIterator { // if this record has children, put it on the parentStack and get/check the children now if (this.curRecord.getModelRecord().childRecords.size() > 0) { - Stack parentStack = new Stack(); + Stack parentStack = new Stack(); parentStack.push(curRecord); while (this.nextRecord != null && this.nextRecord.getModelRecord().parentRecord != null) { @@ -171,7 +171,7 @@ public class RecordIterator { Record parentRecord = null; while (parentStack.size() > 0) { - parentRecord = (Record) parentStack.peek(); + parentRecord = parentStack.peek(); if (parentRecord.recordName.equals(this.nextRecord.getModelRecord().parentName)) { break; } else { @@ -226,8 +226,7 @@ public class RecordIterator { // if (Debug.infoOn()) Debug.logInfo("[DataFile.findModelForLine] line: " + line, module); ModelRecord modelRecord = null; - for (int i = 0; i < modelDataFile.records.size(); i++) { - ModelRecord curModelRecord = (ModelRecord) modelDataFile.records.get(i); + for (ModelRecord curModelRecord: modelDataFile.records) { if (curModelRecord.tcPosition < 0) { modelRecord = curModelRecord;