sundar@1524: /* sundar@1524: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. sundar@1524: * sundar@1524: * Redistribution and use in source and binary forms, with or without sundar@1524: * modification, are permitted provided that the following conditions sundar@1524: * are met: sundar@1524: * sundar@1524: * - Redistributions of source code must retain the above copyright sundar@1524: * notice, this list of conditions and the following disclaimer. sundar@1524: * sundar@1524: * - Redistributions in binary form must reproduce the above copyright sundar@1524: * notice, this list of conditions and the following disclaimer in the sundar@1524: * documentation and/or other materials provided with the distribution. sundar@1524: * sundar@1524: * - Neither the name of Oracle nor the names of its sundar@1524: * contributors may be used to endorse or promote products derived sundar@1524: * from this software without specific prior written permission. sundar@1524: * sundar@1524: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS sundar@1524: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, sundar@1524: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sundar@1524: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR sundar@1524: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sundar@1524: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sundar@1524: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR sundar@1524: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF sundar@1524: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING sundar@1524: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS sundar@1524: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sundar@1524: */ sundar@1524: sundar@1524: // Find the file with maximum number of lines sundar@1524: sundar@1524: var Files = Java.type("java.nio.file.Files"); sundar@1524: var Integer = Java.type("java.lang.Integer"); sundar@1524: var Paths = Java.type("java.nio.file.Paths"); sundar@1524: sundar@1524: var file = arguments.length == 0? "." : arguments[0]; sundar@1524: var ext = arguments.length > 1? arguments[1] : ".java"; sundar@1524: var path = Paths.get(file); sundar@1524: sundar@1524: // return number of lines in given Path (that represents a file) sundar@1524: function lines(p) { sundar@1524: var strm = Files.lines(p); sundar@1524: try { sundar@1524: return strm.count(); sundar@1524: } finally { sundar@1524: strm.close(); sundar@1524: } sundar@1524: } sundar@1524: sundar@1524: // walk files, map to file and lines, find the max sundar@1524: var obj = Files.find(path, Integer.MAX_VALUE, sundar@1524: function(p, a) !p.toFile().isDirectory() && p.toString().endsWith(ext)). sundar@1524: map(function(p) ({ path : p, lines: lines(p) })). sundar@1524: max(function(x, y) x.lines - y.lines).get(); sundar@1524: sundar@1524: // print path and lines of the max line file sundar@1524: print(obj.path, obj.lines);