/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.util.test;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import java.io.*;

/**
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 */
public class LineCountMatcher extends BaseMatcher<File> {
    private final Matcher<? extends Integer> lineCount;

    public LineCountMatcher(Matcher<? extends Integer> m) {
        lineCount = m;
    }

    @Override
    public boolean matches(Object o) {
        if (o instanceof File) {
            try {
                Reader reader = new FileReader((File) o);

                try {
                    BufferedReader lines = new BufferedReader(reader);
                    String line;
                    int n = 0;
                    while ((line = lines.readLine()) != null) {
                        n++;
                    }
                    return lineCount.matches(n);
                } finally {
                    reader.close();
                }
            } catch (FileNotFoundException ex) {
                return false;
            } catch (IOException ex) {
                throw new RuntimeException("error reading file " + o, ex);
            }
        } else {
            return false;
        }
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("file with line count ")
                   .appendDescriptionOf(lineCount);
    }
}

@@ -1489,16 +1489,18 @@
      if 
+(!
 (o insta
@@ -1510,16 +1510,17 @@
 of File)
+)
  %7B%0A     
@@ -1518,37 +1518,45 @@
 ) %7B%0A            
-try %7B
+return false;
 %0A               
@@ -1548,24 +1548,27 @@
         
+%7D%0A%0A
         
 Reader r
@@ -1559,16 +1559,21 @@
         
+try (
 Reader r
@@ -1610,38 +1610,8 @@
 o);%0A
-%0A                try %7B%0A       
     
@@ -1672,47 +1672,12 @@
 der)
-;%0A                    String line;%0A    
+) %7B%0A
     
@@ -1684,20 +1684,16 @@
         
-    
 int n = 
@@ -1711,31 +1711,15 @@
     
-     
-   
 while (
-(line = 
 line
@@ -1730,17 +1730,16 @@
 adLine()
-)
  != null
@@ -1758,24 +1758,16 @@
         
-      
-  
 n++;%0A   
@@ -1775,34 +1775,18 @@
         
-       
- 
 %7D%0A
-        
         
@@ -1822,94 +1822,8 @@
 n);%0A
-                %7D finally %7B%0A                    reader.close();%0A                %7D%0A    
     
@@ -1867,36 +1867,32 @@
 ) %7B%0A            
-    
 return false;%0A  
@@ -1893,28 +1893,24 @@
 se;%0A        
-  
-  
 %7D catch (IOE
@@ -1916,36 +1916,32 @@
 Exception ex) %7B%0A
-    
             thro
@@ -1999,65 +1999,8 @@
 x);%0A
-            %7D%0A        %7D else %7B%0A            return false;%0A
     

Clean up warnings in line count matcher