fixing a bunch of inconsistencies and potential bugs as indicated by findbugs, pmd and eclipse

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2301 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2011-10-07 21:23:23 +00:00
parent df29c0b281
commit 1f67866258
24 changed files with 46 additions and 67 deletions

View File

@ -78,7 +78,7 @@ public class RefineServlet extends Butterfly {
static final private Map<String, Command> commands = new HashMap<String, Command>();
// timer for periodically saving projects
static private Timer _timer;
static private Timer _timer = new Timer("autosave");
static final Logger logger = LoggerFactory.getLogger("refine");
@ -132,10 +132,7 @@ public class RefineServlet extends Butterfly {
FileProjectManager.initialize(s_dataDir);
ImportingManager.initialize(this);
if (_timer == null) {
_timer = new Timer("autosave");
_timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod);
}
_timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod);
logger.trace("< initialize");
}

View File

@ -56,7 +56,6 @@ public class ScatterplotDrawingRowVisitor implements RowVisitor, RecordVisitor {
int col_y;
int dim_x;
int dim_y;
int rotation;
double l;
double dot;
@ -84,7 +83,6 @@ public class ScatterplotDrawingRowVisitor implements RowVisitor, RecordVisitor {
this.dot = dot;
this.dim_x = dim_x;
this.dim_y = dim_y;
this.rotation = rotation;
l = size;
r = ScatterplotFacet.createRotationMatrix(rotation, l);

View File

@ -170,7 +170,7 @@ public class ExpressionTimeValueBinner implements RowVisitor, RecordVisitor {
long t = ((Date) value).getTime();
hasTime = true;
int bin = (int) Math.floor((t - _index.getMin()) / _index.getStep());
int bin = (int) Math.floor((double) (t - _index.getMin()) / (double) _index.getStep());
if (bin >= 0 && bin < bins.length) { // as a precaution
bins[bin]++;
}

View File

@ -67,7 +67,7 @@ public class CsvExporter implements WriterExporter{
public void export(Project project, Properties params, Engine engine, final Writer writer)
throws IOException {
String optionsString = params == null ? null : params.getProperty("options");
String optionsString = (params == null) ? null : params.getProperty("options");
JSONObject options = null;
if (optionsString != null) {
try {
@ -115,8 +115,7 @@ public class CsvExporter implements WriterExporter{
}
};
CustomizableTabularExporterUtilities.exportRows(
project, engine, params, serializer);
CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer);
csvWriter.close();
}

View File

@ -70,7 +70,7 @@ abstract public class CustomizableTabularExporterUtilities {
Properties params,
final TabularSerializer serializer) {
String optionsString = params.getProperty("options");
String optionsString = (params != null) ? params.getProperty("options") : null;
JSONObject optionsTemp = null;
if (optionsString != null) {
try {

View File

@ -50,7 +50,8 @@ import com.google.refine.model.Project;
import com.google.refine.model.Row;
public class ExpressionUtils {
static protected Set<Binder> s_binders = new HashSet<Binder>();
static final protected Set<Binder> s_binders = new HashSet<Binder>();
static public void registerBinder(Binder binder) {
s_binders.add(binder);

View File

@ -44,6 +44,7 @@ import clojure.lang.IFn;
import com.google.refine.grel.Parser;
abstract public class MetaParser {
static public class LanguageInfo {
final public String name;
final public LanguageSpecificParser parser;
@ -56,10 +57,9 @@ abstract public class MetaParser {
}
}
static protected Map<String, LanguageInfo> s_languages;
static {
s_languages = new HashMap<String, LanguageInfo>();
static final protected Map<String, LanguageInfo> s_languages = new HashMap<String, LanguageInfo>();
static {
registerLanguageParser("grel", "Google Refine Expression Language (GREL)", new LanguageSpecificParser() {
@Override

View File

@ -103,7 +103,7 @@ public class Get implements Function {
return ExpressionUtils.toObjectList(v).get(start);
}
} else {
int end = (to != null) ? ((Number) to).intValue() : length;
int end = ((Number) to).intValue();
if (end < 0) {
end = length + end;

View File

@ -56,10 +56,10 @@ public class Combin implements Function {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects the second argument to be a number");
}
return Combin.Combination(((Number) args[0]).intValue(), ((Number) args[1]).intValue());
return Combin.combination(((Number) args[0]).intValue(), ((Number) args[1]).intValue());
}
public static int Combination(int n, int k){
public static int combination(int n, int k){
if (k > n) {
throw new IllegalArgumentException ("the number of elements, n, should be larger than the number of combinations, k");
}

View File

@ -47,12 +47,12 @@ public class Even implements Function {
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return Even.RoundUpToEven(((Number) args[0]).doubleValue());
return Even.roundUpToEven(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public static double RoundUpToEven(double d){
public static double roundUpToEven(double d){
double temp = Math.ceil(d);
return ((temp % 2) == 0) ? temp : temp+1;
}

View File

@ -49,10 +49,10 @@ public class FactN implements Function {
if (args.length != 2) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two numbers");
}
if(args[0] == null || !(args[0] instanceof Number)) {
if (args[0] == null || !(args[0] instanceof Number)) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects the first parameter to be a number");
}
if(args[1] == null && !(args[1] instanceof Number)) {
if (args[1] == null || !(args[1] instanceof Number)) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects the second parameter to be a number");
}

View File

@ -51,8 +51,8 @@ public class Multinomial implements Function {
}
int sum = 0;
int product = 1;
for(int i = 0; i < args.length; i++){
if(args[i] == null && !(args[i] instanceof Number)) {
for (int i = 0; i < args.length; i++){
if (args[i] == null || !(args[i] instanceof Number)) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects parameter " + (i + 1) + " to be a number");
}
int num = ((Number) args[i]).intValue();

View File

@ -47,12 +47,12 @@ public class Odd implements Function {
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] != null && args[0] instanceof Number) {
return Odd.RoundUpToOdd(((Number) args[0]).doubleValue());
return Odd.roundUpToOdd(((Number) args[0]).doubleValue());
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number");
}
public static double RoundUpToOdd(double d){
public static double roundUpToOdd(double d){
double temp = Math.ceil(d);
return ((temp % 2) == 0) ? temp + 1 : temp;
}

View File

@ -46,7 +46,8 @@ import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
public class SmartSplit implements Function {
static protected CSVParser s_tabParser = new CSVParser(
static final protected CSVParser s_tabParser = new CSVParser(
'\t',
CSVParser.DEFAULT_QUOTE_CHARACTER,
CSVParser.DEFAULT_ESCAPE_CHARACTER,
@ -54,7 +55,8 @@ public class SmartSplit implements Function {
CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE,
false
);
static protected CSVParser s_commaParser = new CSVParser(
static final protected CSVParser s_commaParser = new CSVParser(
',',
CSVParser.DEFAULT_QUOTE_CHARACTER,
CSVParser.DEFAULT_ESCAPE_CHARACTER,
@ -62,6 +64,7 @@ public class SmartSplit implements Function {
CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE,
false
);
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length >= 1 && args.length <= 2) {

View File

@ -154,10 +154,10 @@ public class FixedWidthImporter extends TabularImportingParserBase {
static public int[] guessColumnWidths(File file, String encoding) {
try {
InputStream is = new FileInputStream(file);
try {
Reader reader = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
LineNumberReader lineNumberReader = new LineNumberReader(reader);
Reader reader = (encoding != null) ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
LineNumberReader lineNumberReader = new LineNumberReader(reader);
try {
int[] counts = null;
int totalBytes = 0;
int lineCount = 0;
@ -213,6 +213,8 @@ public class FixedWidthImporter extends TabularImportingParserBase {
return widthA;
}
} finally {
lineNumberReader.close();
reader.close();
is.close();
}
} catch (UnsupportedEncodingException e) {

View File

@ -108,15 +108,12 @@ public class SeparatorBasedImporter extends TabularImportingParserBase {
final LineNumberReader lnReader = new LineNumberReader(reader);
TableDataReader dataReader = new TableDataReader() {
long bytesRead = 0;
@Override
public List<Object> getNextRowOfCells() throws IOException {
String line = lnReader.readLine();
if (line == null) {
return null;
} else {
bytesRead += line.length();
return getCells(line, parser, lnReader);
}
}
@ -172,10 +169,10 @@ public class SeparatorBasedImporter extends TabularImportingParserBase {
static public Separator guessSeparator(File file, String encoding) {
try {
InputStream is = new FileInputStream(file);
try {
Reader reader = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
LineNumberReader lineNumberReader = new LineNumberReader(reader);
Reader reader = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
LineNumberReader lineNumberReader = new LineNumberReader(reader);
try {
List<Separator> separators = new ArrayList<SeparatorBasedImporter.Separator>();
Map<Character, Separator> separatorMap = new HashMap<Character, SeparatorBasedImporter.Separator>();
@ -236,6 +233,8 @@ public class SeparatorBasedImporter extends TabularImportingParserBase {
}
}
} finally {
lineNumberReader.close();
reader.close();
is.close();
}
} catch (UnsupportedEncodingException e) {

View File

@ -16,9 +16,9 @@ public class TextFormatGuesser implements FormatGuesser {
public String guess(File file, String encoding, String seedFormat) {
try {
InputStream is = new FileInputStream(file);
try {
Reader reader = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
Reader reader = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
try {
int totalBytes = 0;
int openBraces = 0;
int closeBraces = 0;
@ -63,6 +63,7 @@ public class TextFormatGuesser implements FormatGuesser {
}
return "text/line-based";
} finally {
reader.close();
is.close();
}
} catch (UnsupportedEncodingException e) {

View File

@ -40,8 +40,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -163,26 +161,13 @@ public class XmlImportUtilities extends TreeImportUtilities {
List<RecordElementCandidate> descendantCandidates = new ArrayList<RecordElementCandidate>();
Map<String, Integer> immediateChildCandidateMap = new HashMap<String, Integer>();
int textNodeCount = 0;
int childElementNodeCount = 0;
try {
while (parser.hasNext()) {
Token eventType = parser.next();
if (eventType == Token.EndEntity ) {
break;
} else if (eventType == Token.Value) {
try{
if (parser.getFieldValue().trim().length() > 0) {
textNodeCount++;
}
}catch(TreeReaderException e){
e.printStackTrace();
//silent
}
} else if (eventType == Token.StartEntity) {
childElementNodeCount++;
String tagName = parser.getFieldName();
immediateChildCandidateMap.put(

View File

@ -169,7 +169,7 @@ public class ImportingManager {
controller.init(servlet);
}
static public File getImportDir() {
static synchronized public File getImportDir() {
if (importDir == null) {
File tempDir = servlet.getTempDir();
importDir = tempDir == null ? new File(".import-temp") : new File(tempDir, "import");

View File

@ -371,7 +371,7 @@ public class FileProjectManager extends ProjectManager {
logger.warn("Error reading file", e);
} finally {
try {
reader.close();
if (reader != null) reader.close();
} catch (IOException e) {
logger.warn("Exception closing file",e);
}

View File

@ -70,7 +70,7 @@ public class ColumnModel implements Jsonizable {
this._maxCellIndex = Math.max(this._maxCellIndex, maxCellIndex);
}
public int getMaxCellIndex() {
synchronized public int getMaxCellIndex() {
return _maxCellIndex;
}
@ -78,12 +78,12 @@ public class ColumnModel implements Jsonizable {
return _maxCellIndex++;
}
public void setKeyColumnIndex(int keyColumnIndex) {
synchronized public void setKeyColumnIndex(int keyColumnIndex) {
// TODO: check validity of new cell index, e.g., it's not in any group
this._keyColumnIndex = keyColumnIndex;
}
public int getKeyColumnIndex() {
synchronized public int getKeyColumnIndex() {
return _keyColumnIndex;
}

View File

@ -240,7 +240,7 @@ public class Row implements HasFields, Jsonizable {
StringBuffer result = new StringBuffer();
for (Cell cell : cells) {
result.append(cell == null ? "null" : cell.toString());
result.append(",");
result.append(',');
}
return result.toString();
}

View File

@ -46,9 +46,7 @@ import org.json.JSONWriter;
import com.google.refine.Jsonizable;
public class TopList implements Jsonizable, Iterable<String> {
private static final long serialVersionUID = 2666669643063493350L;
final protected int _top;
final protected List<String> _list = new ArrayList<String>();

View File

@ -49,10 +49,6 @@ import com.google.refine.model.Project;
*/
public class ProjectManagerStub extends ProjectManager {
public ProjectManagerStub(){
super();
}
@Override
public void deleteProject(long projectID) {
// empty