Skip to content

Commit cf16361

Browse files
committed
Make DoubleMatrixReader thread-safe
The NumberFormat parsing is not synchronized, so it is not safe to use from multiple threads.
1 parent ca4f0c3 commit cf16361

3 files changed

Lines changed: 27 additions & 47 deletions

File tree

src/ubic/basecode/io/reader/DoubleMatrixReader.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2006 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
@@ -37,18 +37,12 @@
3737

3838
/**
3939
* Reader for {@link basecode.dataStructure.matrix.DoubleMatrix}. Lines beginning with "#" or "!" will be ignored.
40-
*
40+
*
4141
* @author Paul Pavlidis
42-
*
42+
*
4343
*/
4444
public class DoubleMatrixReader extends AbstractMatrixReader<DoubleMatrix<String, String>, Double> {
4545

46-
private static NumberFormat nf = NumberFormat.getInstance( Locale.ENGLISH );
47-
static {
48-
if ( nf instanceof DecimalFormat ) {
49-
// ( ( DecimalFormat ) nf ).setDecimalSeparatorAlwaysShown( true );
50-
}
51-
}
5246
private List<String> colNames;
5347
private int numHeadings;
5448

@@ -87,6 +81,11 @@ public DoubleMatrix<String, String> read( InputStream stream, Collection<String>
8781
public DoubleMatrix<String, String> read( InputStream stream, Collection<String> wantedRowNames,
8882
boolean createEmptyRows, int skipColumns, int maxRows ) throws IOException {
8983

84+
NumberFormat nf = NumberFormat.getInstance( Locale.ENGLISH );
85+
if ( nf instanceof DecimalFormat ) {
86+
// ( ( DecimalFormat ) nf ).setDecimalSeparatorAlwaysShown( true );
87+
}
88+
9089
BufferedReader dis = new BufferedReader( new InputStreamReader( stream ) );
9190

9291
List<DoubleArrayList> MTemp = new Vector<DoubleArrayList>();
@@ -113,7 +112,7 @@ public DoubleMatrix<String, String> read( InputStream stream, Collection<String>
113112
continue;
114113
}
115114

116-
String rowName = parseRow( row, rowNames, MTemp, wantedRowNames, skipColumns );
115+
String rowName = parseRow( row, rowNames, MTemp, wantedRowNames, skipColumns, nf );
117116

118117
if ( rowName == null ) {
119118
// signals a blank or skipped row.
@@ -183,7 +182,7 @@ public DoubleMatrix<String, String> read( String filename ) throws IOException {
183182

184183
/**
185184
* Read a matrix from a file, subject to filtering criteria.
186-
*
185+
*
187186
* @param filename data file to read from (can be compressed)
188187
* @param wantedRowNames contains names of rows we want to get
189188
* @return NamedMatrix object constructed from the data file
@@ -277,7 +276,7 @@ protected DoubleMatrix<String, String> createMatrix( List<DoubleArrayList> MTemp
277276
* @throws IOException
278277
*/
279278
private String parseRow( String row, Collection<String> rowNames, List<DoubleArrayList> MTemp,
280-
Collection<String> wantedRowNames, int skipColumns ) throws IOException {
279+
Collection<String> wantedRowNames, int skipColumns, NumberFormat nf ) throws IOException {
281280

282281
if ( row.startsWith( "#" ) || row.startsWith( "!" ) ) {
283282
return null;

test/ubic/basecode/math/linearmodels/LeastSquaresFitTest.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,26 @@
1414
*/
1515
package ubic.basecode.math.linearmodels;
1616

17-
import static org.junit.Assert.assertArrayEquals;
18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertNull;
21-
import static org.junit.Assert.assertTrue;
22-
23-
import java.text.DecimalFormat;
24-
import java.text.NumberFormat;
25-
import java.util.List;
26-
import java.util.Locale;
27-
import java.util.Map;
28-
import java.util.zip.GZIPInputStream;
29-
30-
import org.apache.commons.math3.distribution.FDistribution;
31-
import org.junit.Test;
32-
3317
import cern.colt.list.DoubleArrayList;
3418
import cern.colt.matrix.DoubleMatrix1D;
3519
import cern.colt.matrix.DoubleMatrix2D;
3620
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
3721
import cern.colt.matrix.linalg.Algebra;
3822
import cern.jet.math.Functions;
39-
import ubic.basecode.dataStructure.matrix.DenseDoubleMatrix;
40-
import ubic.basecode.dataStructure.matrix.DenseDoubleMatrix1D;
41-
import ubic.basecode.dataStructure.matrix.DoubleMatrix;
42-
import ubic.basecode.dataStructure.matrix.ObjectMatrix;
43-
import ubic.basecode.dataStructure.matrix.ObjectMatrixImpl;
44-
import ubic.basecode.dataStructure.matrix.StringMatrix;
23+
import org.apache.commons.math3.distribution.FDistribution;
24+
import org.junit.Test;
25+
import ubic.basecode.dataStructure.matrix.*;
4526
import ubic.basecode.io.reader.DoubleMatrixReader;
4627
import ubic.basecode.io.reader.StringMatrixReader;
47-
import ubic.basecode.io.writer.MatrixWriter;
4828
import ubic.basecode.math.DescriptiveWithMissing;
4929
import ubic.basecode.math.MatrixStats;
5030

31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.zip.GZIPInputStream;
34+
35+
import static org.junit.Assert.*;
36+
5137
/**
5238
* @author paul
5339
*/

test/ubic/basecode/math/linearmodels/LeastSquaresFitTest2.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,21 @@
1919

2020
package ubic.basecode.math.linearmodels;
2121

22-
import static org.junit.Assert.assertArrayEquals;
23-
import static org.junit.Assert.assertEquals;
24-
25-
import java.text.DecimalFormat;
26-
import java.text.NumberFormat;
27-
import java.util.List;
28-
import java.util.Locale;
29-
30-
import org.junit.Test;
31-
3222
import cern.colt.matrix.DoubleMatrix1D;
3323
import cern.colt.matrix.DoubleMatrix2D;
24+
import org.junit.Test;
3425
import ubic.basecode.dataStructure.matrix.DenseDoubleMatrix;
3526
import ubic.basecode.dataStructure.matrix.DoubleMatrix;
3627
import ubic.basecode.dataStructure.matrix.StringMatrix;
3728
import ubic.basecode.io.reader.DoubleMatrixReader;
3829
import ubic.basecode.io.reader.StringMatrixReader;
39-
import ubic.basecode.io.writer.MatrixWriter;
4030
import ubic.basecode.math.MatrixStats;
4131

32+
import java.util.List;
33+
34+
import static org.junit.Assert.assertArrayEquals;
35+
import static org.junit.Assert.assertEquals;
36+
4237
/**
4338
* @author paul
4439
*/

0 commit comments

Comments
 (0)