]> source.dussan.org Git - poi.git/commitdiff
Fix some compiler warnings
authorDominik Stadler <centic@apache.org>
Sat, 16 Sep 2017 08:29:28 +0000 (08:29 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 16 Sep 2017 08:29:28 +0000 (08:29 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808523 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/ss/util/BaseTestCellUtil.java
src/testcases/org/apache/poi/ss/util/NumberComparingSpreadsheetGenerator.java
src/testcases/org/apache/poi/ss/util/NumberComparisonExamples.java
src/testcases/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java
src/testcases/org/apache/poi/ss/util/TestCellAddress.java
src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java
src/testcases/org/apache/poi/ss/util/TestCellReference.java
src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java

index 215b64a340c835ddeec615805a390ea85a7bd6eb..dd985df65fe456a1d61269f639dd85dbce4b5087 100644 (file)
@@ -77,16 +77,13 @@ public class BaseTestCellUtil {
     
     @Test(expected=RuntimeException.class)
     public void setCellStylePropertyWithInvalidValue() throws IOException {
-        Workbook wb = _testDataProvider.createWorkbook();
-        try {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
             Sheet s = wb.createSheet();
             Row r = s.createRow(0);
             Cell c = r.createCell(0);
 
             // An invalid BorderStyle constant
             CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42);
-        } finally {
-            wb.close();
         }
     }
     
@@ -213,9 +210,6 @@ public class BaseTestCellUtil {
 
     /**
      * @deprecated by {@link #setAlignmentEnum()}
-     *
-     * @throws IOException
-     * 
      */
     @Deprecated
     @SuppressWarnings("deprecated")
index 8069a16b43f04c0b5bea8c011eb1681168414d75..87c165ab24ed474fdafd46b175a1813864431a20 100644 (file)
@@ -42,111 +42,105 @@ import org.apache.poi.util.HexDump;
  */
 public class NumberComparingSpreadsheetGenerator {
 
-       private static final class SheetWriter {
-
-               private final HSSFSheet _sheet;
-               private int _rowIndex;
-
-               public SheetWriter(HSSFWorkbook wb) {
-                       HSSFSheet sheet = wb.createSheet("Sheet1");
-
-                       writeHeaderRow(wb, sheet);
-                       _sheet = sheet;
-                       _rowIndex = 1;
-               }
-
-               public void addTestRow(double a, double b, int expResult) {
-                       writeDataRow(_sheet, _rowIndex++, a, b, expResult);
-               }
-       }
-
-       private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
-               HSSFCell cell = row.createCell(i);
-               cell.setCellValue(new HSSFRichTextString(text));
-               cell.setCellStyle(style);
-       }
-       static void writeHeaderRow(HSSFWorkbook wb, HSSFSheet sheet) {
-               sheet.setColumnWidth(0, 6000);
-               sheet.setColumnWidth(1, 6000);
-               sheet.setColumnWidth(2, 3600);
-               sheet.setColumnWidth(3, 3600);
-               sheet.setColumnWidth(4, 2400);
-               sheet.setColumnWidth(5, 2400);
-               sheet.setColumnWidth(6, 2400);
-               sheet.setColumnWidth(7, 2400);
-               sheet.setColumnWidth(8, 2400);
-               HSSFRow row = sheet.createRow(0);
-               HSSFCellStyle style = wb.createCellStyle();
-               HSSFFont font = wb.createFont();
-               font.setBold(true);
-               style.setFont(font);
-               writeHeaderCell(row, 0, "Raw Long Bits A", style);
-               writeHeaderCell(row, 1, "Raw Long Bits B", style);
-               writeHeaderCell(row, 2, "Value A", style);
-               writeHeaderCell(row, 3, "Value B", style);
-               writeHeaderCell(row, 4, "Exp Cmp", style);
-               writeHeaderCell(row, 5, "LT", style);
-               writeHeaderCell(row, 6, "EQ", style);
-               writeHeaderCell(row, 7, "GT", style);
-               writeHeaderCell(row, 8, "Check", style);
-       }
-       /**
-        * Fills a spreadsheet row with one comparison example. The two numeric values are written to
-        * columns C and D. Columns (F, G and H) respectively get formulas ("v0<v1", "v0=v1", "v0>v1"),
-        * which will be evaluated by Excel. Column D gets the expected comparison result. Column I
-        * gets a formula to check that Excel's comparison results match that predicted in column D.
-        *
-        * @param v0 the first value to be compared
-        * @param v1 the second value to be compared
-        * @param expRes expected comparison result (-1, 0, or +1)
-        */
-       static void writeDataRow(HSSFSheet sheet, int rowIx, double v0, double v1, int expRes) {
-               HSSFRow row = sheet.createRow(rowIx);
-
-               int rowNum = rowIx + 1;
-
-
-               row.createCell(0).setCellValue(formatDoubleAsHex(v0));
-               row.createCell(1).setCellValue(formatDoubleAsHex(v1));
-               row.createCell(2).setCellValue(v0);
-               row.createCell(3).setCellValue(v1);
-               row.createCell(4).setCellValue(expRes < 0 ? "LT" : expRes > 0 ? "GT" : "EQ");
-               row.createCell(5).setCellFormula("C" + rowNum + "<" + "D" + rowNum);
-               row.createCell(6).setCellFormula("C" + rowNum + "=" + "D" + rowNum);
-               row.createCell(7).setCellFormula("C" + rowNum + ">" + "D" + rowNum);
-               // TODO - bug elsewhere in POI - something wrong with encoding of NOT() function
-               String frm = "if(or(" +
-                       "and(E#='LT', F#      , G#=FALSE, H#=FALSE)," +
-                       "and(E#='EQ', F#=FALSE, G#      , H#=FALSE)," +
-                       "and(E#='GT', F#=FALSE, G#=FALSE, H#      )" +
-                       "), 'OK', 'error')"     ;
-               row.createCell(8).setCellFormula(frm.replaceAll("#", String.valueOf(rowNum)).replace('\'', '"'));
-       }
-
-       private static String formatDoubleAsHex(double d) {
-               long l = Double.doubleToLongBits(d);
-               return HexDump.longToHex(l)+'L';
-       }
-
-       public static void main(String[] args) {
-
-               HSSFWorkbook wb = new HSSFWorkbook();
-               SheetWriter sw = new SheetWriter(wb);
-               ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples();
-               for (ComparisonExample ce : ces) {
-                       sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult());
-               }
-
-
-               File outputFile = new File("ExcelNumberCompare.xls");
-
-               try {
-                       FileOutputStream os = new FileOutputStream(outputFile);
-                       wb.write(os);
-                       os.close();
-               } catch (IOException e) {
-                       throw new RuntimeException(e);
-               }
-               System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
-       }
+    private static final class SheetWriter {
+
+        private final HSSFSheet _sheet;
+        private int _rowIndex;
+
+        public SheetWriter(HSSFWorkbook wb) {
+            HSSFSheet sheet = wb.createSheet("Sheet1");
+
+            writeHeaderRow(wb, sheet);
+            _sheet = sheet;
+            _rowIndex = 1;
+        }
+
+        public void addTestRow(double a, double b, int expResult) {
+            writeDataRow(_sheet, _rowIndex++, a, b, expResult);
+        }
+    }
+
+    private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
+        HSSFCell cell = row.createCell(i);
+        cell.setCellValue(new HSSFRichTextString(text));
+        cell.setCellStyle(style);
+    }
+    static void writeHeaderRow(HSSFWorkbook wb, HSSFSheet sheet) {
+        sheet.setColumnWidth(0, 6000);
+        sheet.setColumnWidth(1, 6000);
+        sheet.setColumnWidth(2, 3600);
+        sheet.setColumnWidth(3, 3600);
+        sheet.setColumnWidth(4, 2400);
+        sheet.setColumnWidth(5, 2400);
+        sheet.setColumnWidth(6, 2400);
+        sheet.setColumnWidth(7, 2400);
+        sheet.setColumnWidth(8, 2400);
+        HSSFRow row = sheet.createRow(0);
+        HSSFCellStyle style = wb.createCellStyle();
+        HSSFFont font = wb.createFont();
+        font.setBold(true);
+        style.setFont(font);
+        writeHeaderCell(row, 0, "Raw Long Bits A", style);
+        writeHeaderCell(row, 1, "Raw Long Bits B", style);
+        writeHeaderCell(row, 2, "Value A", style);
+        writeHeaderCell(row, 3, "Value B", style);
+        writeHeaderCell(row, 4, "Exp Cmp", style);
+        writeHeaderCell(row, 5, "LT", style);
+        writeHeaderCell(row, 6, "EQ", style);
+        writeHeaderCell(row, 7, "GT", style);
+        writeHeaderCell(row, 8, "Check", style);
+    }
+    /**
+     * Fills a spreadsheet row with one comparison example. The two numeric values are written to
+     * columns C and D. Columns (F, G and H) respectively get formulas ("v0<v1", "v0=v1", "v0>v1"),
+     * which will be evaluated by Excel. Column D gets the expected comparison result. Column I
+     * gets a formula to check that Excel's comparison results match that predicted in column D.
+     *
+     * @param v0 the first value to be compared
+     * @param v1 the second value to be compared
+     * @param expRes expected comparison result (-1, 0, or +1)
+     */
+    static void writeDataRow(HSSFSheet sheet, int rowIx, double v0, double v1, int expRes) {
+        HSSFRow row = sheet.createRow(rowIx);
+
+        int rowNum = rowIx + 1;
+
+
+        row.createCell(0).setCellValue(formatDoubleAsHex(v0));
+        row.createCell(1).setCellValue(formatDoubleAsHex(v1));
+        row.createCell(2).setCellValue(v0);
+        row.createCell(3).setCellValue(v1);
+        row.createCell(4).setCellValue(expRes < 0 ? "LT" : expRes > 0 ? "GT" : "EQ");
+        row.createCell(5).setCellFormula("C" + rowNum + "<" + "D" + rowNum);
+        row.createCell(6).setCellFormula("C" + rowNum + "=" + "D" + rowNum);
+        row.createCell(7).setCellFormula("C" + rowNum + ">" + "D" + rowNum);
+        // TODO - bug elsewhere in POI - something wrong with encoding of NOT() function
+        String frm = "if(or(" +
+            "and(E#='LT', F#      , G#=FALSE, H#=FALSE)," +
+            "and(E#='EQ', F#=FALSE, G#      , H#=FALSE)," +
+            "and(E#='GT', F#=FALSE, G#=FALSE, H#      )" +
+            "), 'OK', 'error')"    ;
+        row.createCell(8).setCellFormula(frm.replaceAll("#", String.valueOf(rowNum)).replace('\'', '"'));
+    }
+
+    private static String formatDoubleAsHex(double d) {
+        long l = Double.doubleToLongBits(d);
+        return HexDump.longToHex(l)+'L';
+    }
+
+    public static void main(String[] args) throws IOException {
+        try (HSSFWorkbook wb = new HSSFWorkbook()) {
+            SheetWriter sw = new SheetWriter(wb);
+            ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples();
+            for (ComparisonExample ce : ces) {
+                sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult());
+            }
+
+            File outputFile = new File("ExcelNumberCompare.xls");
+            try (FileOutputStream os = new FileOutputStream(outputFile)) {
+                wb.write(os);
+            }
+            System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
+        }
+    }
 }
index 79fe64cda7c0fb533f84924a2267cf9c72a51f57..325d15dc4abc5dbb44591e6a2a701ae95379dac1 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.ss.util;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -115,7 +116,7 @@ final class NumberComparisonExamples {
                addStepTransition(temp, 0x001000000000001BL);
                addStepTransition(temp, 0x001000000000002FL);
 
-               for(ComparisonExample ce : new ComparisonExample[] {
+               Collections.addAll(temp, new ComparisonExample[]{
                                // negative, and exponents differ by more than 1
                                ce(0xBF30000000000000L, 0xBE60000000000000L, -1),
 
@@ -137,9 +138,7 @@ final class NumberComparisonExamples {
                                ce(0x000FFFFFFFFFFFF9L, 0x0010000000000007L, -1),
                                ce(0x000FFFFFFFFFFFFAL, 0x0010000000000008L, -1),
                                ce(0x000FFFFFFFFFFFFBL, 0x0010000000000008L, -1),
-               }) {
-                       temp.add(ce);
-               }
+               });
 
                ComparisonExample[] result = new ComparisonExample[temp.size()];
                temp.toArray(result);
@@ -151,13 +150,11 @@ final class NumberComparisonExamples {
        }
 
        private static void addStepTransition(List<ComparisonExample> temp, long rawBits) {
-               for(ComparisonExample ce : new ComparisonExample[] {
-                               ce(rawBits-1, rawBits+0, 0),
-                               ce(rawBits+0, rawBits+1, -1),
-                               ce(rawBits+1, rawBits+2, 0),
-               }) {
-                       temp.add(ce);
-               }
+               Collections.addAll(temp, new ComparisonExample[]{
+                               ce(rawBits - 1, rawBits + 0, 0),
+                               ce(rawBits + 0, rawBits + 1, -1),
+                               ce(rawBits + 1, rawBits + 2, 0),
+               });
 
        }
 
index 35ea428c8894646589d5b8e8fe276408a7b99662..fa94e811bf701b3dafc47052de063e00336c6db2 100644 (file)
@@ -136,9 +136,7 @@ public class NumberRenderingSpreadsheetGenerator {
        }
        
        private static String formatLongAsHex(long l) {
-               StringBuilder sb = new StringBuilder(20);
-               sb.append(HexDump.longToHex(l)).append('L');
-               return sb.toString();
+               return HexDump.longToHex(l) + 'L';
        }
 
        public static void main(String[] args) {
index 57d7a1a17e1c9179bcc0795c22224b897baa16e8..96c783f6a7b63dd21f9fc416b36766cc62d34e4a 100644 (file)
@@ -27,8 +27,6 @@ import java.util.Arrays;
 
 /**
  * Tests that the common CellAddress works as we need it to.
- * Note - some additional testing is also done in the HSSF class,
- *  {@link org.apache.poi.hssf.util.TestCellAddress}
  */
 public final class TestCellAddress {
     @Test
@@ -60,6 +58,7 @@ public final class TestCellAddress {
         assertNotEquals(new CellReference(4, 6), new CellReference(6, 4));
     }
     
+    @SuppressWarnings("EqualsWithItself")
     @Test
     public void testCompareTo() {
         final CellAddress A1 = new CellAddress(0, 0);
@@ -89,7 +88,7 @@ public final class TestCellAddress {
         
         CellAddress[] sorted = {A1, B1, A2, B2};
         CellAddress[] unsorted = {B1, B2, A1, A2};
-        assumeTrue(!sorted.equals(unsorted));
+        assumeTrue(!Arrays.equals(sorted, unsorted));
         Arrays.sort(unsorted);
         assertArrayEquals(sorted, unsorted);
     }
index 3f0f4414511b4828a2b27fe314c9ea9a6ef79fd0..557a50df13f5cc43caf4bfefba3c095cab3b5b88 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.poi.ss.util;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -53,8 +52,9 @@ public final class TestCellRangeAddress {
     @Test
     public void testLoadInvalid() {
         try {
-            assertNotNull(new CellRangeAddress(
-                TestcaseRecordInputStream.create(0x000, new byte[] { (byte)0x02 })));
+            new CellRangeAddress(
+                TestcaseRecordInputStream.create(0x000, new byte[] { (byte)0x02 }));
+            fail();
         } catch (RuntimeException e) {
             assertTrue("Had: " + e, e.getMessage().contains("Ran out of data"));
         }
@@ -66,8 +66,7 @@ public final class TestCellRangeAddress {
 
         byte[] recordBytes;
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        LittleEndianOutputStream out = new LittleEndianOutputStream(baos);
-        try {
+        try (LittleEndianOutputStream out = new LittleEndianOutputStream(baos)) {
             // With nothing set
             ref.serialize(out);
             recordBytes = baos.toByteArray();
@@ -91,8 +90,6 @@ public final class TestCellRangeAddress {
             for (int i = 0; i < data.length; i++) {
                 assertEquals("At offset " + i, data[i], recordBytes[i]);
             }
-        } finally {
-            out.close();
         }
     }
     
@@ -100,13 +97,13 @@ public final class TestCellRangeAddress {
     public void testCreateIllegal() throws IOException {
         // for some combinations we expected exceptions
         try {
-            assertNotNull(new CellRangeAddress(1, 0, 0, 0));
+            new CellRangeAddress(1, 0, 0, 0);
             fail("Expect to catch an exception");
         } catch (IllegalArgumentException e) {
             // expected here
         }
         try {
-            assertNotNull(new CellRangeAddress(0, 0, 1, 0));
+            new CellRangeAddress(0, 0, 1, 0);
             fail("Expect to catch an exception");
         } catch (IllegalArgumentException e) {
             // expected here
index 6cfb7d9e702f3368246ab096357a6cc68ba520c7..a39bd0b725c44dcf38a610ba3e9b9107e5ffe1f5 100644 (file)
@@ -250,17 +250,23 @@ public final class TestCellReference {
         try {
             new CellReference("Sheet1!#REF!");
             fail("Shouldn't be able to create a #REF! refence");
-        } catch(IllegalArgumentException expected) {}
+        } catch(IllegalArgumentException expected) {
+            // expected here
+        }
 
         try {
             new CellReference("'MySheetName'!#REF!");
             fail("Shouldn't be able to create a #REF! refence");
-        } catch(IllegalArgumentException expected) {}
+        } catch(IllegalArgumentException expected) {
+            // expected here
+        }
 
         try {
             new CellReference("#REF!");
             fail("Shouldn't be able to create a #REF! refence");
-        } catch(IllegalArgumentException expected) {}
+        } catch(IllegalArgumentException expected) {
+            // expected here
+        }
     }
 
     private static void confirmCrInRange(boolean expResult, String colStr, String rowStr,
@@ -339,9 +345,11 @@ public final class TestCellReference {
         CellReference ref2 = new CellReference("Sheet 1", 4, 0, false, false);
         assertEquals("equals", ref1, ref2);
         assertEquals("hash code", ref1.hashCode(), ref2.hashCode());
-        
+
+        //noinspection ObjectEqualsNull
         assertFalse("null", ref1.equals(null));
         assertFalse("3D vs 2D", ref1.equals(new CellReference("A5")));
+        //noinspection EqualsBetweenInconvertibleTypes
         assertFalse("type", ref1.equals(new Integer(0)));
     }
     
index 2b6e6e31271c060822015962f6b0a37336b24b08..70a90492a8f39cbe3fcf1f3ec5ec3fbd9c4f2181 100644 (file)
@@ -41,11 +41,10 @@ import org.apache.poi.util.TempFile;
 public final class TestDateFormatConverter extends TestCase {
     private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception {
 
-        Workbook workbook = new HSSFWorkbook();
-        try {
+        try (Workbook workbook = new HSSFWorkbook()) {
             String sheetName;
-            if( dates ) {
-                if( times ) {
+            if (dates) {
+                if (times) {
                     sheetName = "DateTimes";
                 } else {
                     sheetName = "Dates";
@@ -62,18 +61,18 @@ public final class TestDateFormatConverter extends TestCase {
             header.createCell(4).setCellValue("Equals");
             header.createCell(5).setCellValue("Java pattern");
             header.createCell(6).setCellValue("Excel pattern");
-    
+
             int rowNum = 1;
-            for( Locale locale : DateFormat.getAvailableLocales() ) {
+            for (Locale locale : DateFormat.getAvailableLocales()) {
                 try {
                     Row row = sheet.createRow(rowNum++);
-        
+
                     row.createCell(0).setCellValue(locale.toString());
                     row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT));
-        
+
                     DateFormat dateFormat;
-                    if( dates ) {
-                        if( times ) {
+                    if (dates) {
+                        if (times) {
                             dateFormat = DateFormat.getDateTimeInstance(style, style, locale);
                         } else {
                             dateFormat = DateFormat.getDateInstance(style, locale);
@@ -81,42 +80,37 @@ public final class TestDateFormatConverter extends TestCase {
                     } else {
                         dateFormat = DateFormat.getTimeInstance(style, locale);
                     }
-        
+
                     Cell cell = row.createCell(2);
-        
+
                     cell.setCellValue(date);
                     CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
-        
-                    String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern();
+
+                    String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
                     String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
-        
+
                     DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
                     cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
                     row.createCell(3).setCellValue(dateFormat.format(date));
-        
+
                     cell.setCellStyle(cellStyle);
-        
+
                     // the formula returns TRUE is the formatted date in column C equals to the string in column D
-                    row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum);
+                    row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum);
                     row.createCell(5).setCellValue(javaDateFormatPattern);
                     row.createCell(6).setCellValue(excelFormatPattern);
                 } catch (Exception e) {
-                    throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + 
+                    throw new RuntimeException("Failed for locale: " + locale + ", having locales: " +
                             Arrays.toString(DateFormat.getAvailableLocales()), e);
                 }
             }
-    
+
             File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx");
-            FileOutputStream outputStream = new FileOutputStream(outputFile);
-            try {
+            try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
                 workbook.write(outputStream);
-            } finally {
-                outputStream.close();
             }
-    
-            System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel");
-        } finally {
-            workbook.close();
+
+            System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel");
         }
     }