From a295f24645c4dc7dc86db7c04befe851359b3df5 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 8 Apr 2012 15:52:21 +0000 Subject: [PATCH] * Scala-style switch/case pattern matching for format engine --- source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy | 6 +++--- .../sourceforge/filebot/format/ExpressionFormat.lib.groovy | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy index 6c6a6e4d..d1841747 100644 --- a/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy +++ b/source/net/sourceforge/filebot/cli/ScriptShell.lib.groovy @@ -37,7 +37,7 @@ File.metaClass.hasExtension = { String... ext -> hasExtension(delegate, ext) } File.metaClass.isDerived = { f -> isDerived(delegate, f) } File.metaClass.validateFileName = { validateFileName(delegate) } File.metaClass.validateFilePath = { validateFilePath(delegate) } -File.metaClass.moveTo = { f -> moveRename(delegate, f instanceof File ? f : new File(f.toString())) } +File.metaClass.moveTo = { f -> moveRename(delegate, f as File) } File.metaClass.copyTo = { dir -> copyAs(delegate, new File(dir, delegate.getName())) } List.metaClass.mapByFolder = { mapByFolder(delegate) } List.metaClass.mapByExtension = { mapByExtension(delegate) } @@ -72,7 +72,7 @@ URL.metaClass.post = { Map parameters -> post(delegate.openConnection(), paramet URL.metaClass.post = { byte[] data, contentType = 'application/octet-stream' -> post(delegate.openConnection(), data, contentType) } URL.metaClass.post = { String text, csn = 'utf-8' -> delegate.post(text.getBytes(csn), 'text/plain') } -ByteBuffer.metaClass.saveAs = { f -> f = f instanceof File ? f : new File(f.toString()); writeFile(delegate.duplicate(), f); f.absolutePath }; +ByteBuffer.metaClass.saveAs = { f -> f = f as File; writeFile(delegate.duplicate(), f); f.absoluteFile }; URL.metaClass.saveAs = { f -> fetch(delegate).saveAs(f) } String.metaClass.saveAs = { f, csn = "utf-8" -> Charset.forName(csn).encode(delegate).saveAs(f) } @@ -165,7 +165,7 @@ def parseDate(path) { } def detectSeriesName(files, locale = Locale.ENGLISH) { - def names = MediaDetection.detectSeriesNames(files instanceof Collection ? files : [new File(files.toString())], locale) + def names = MediaDetection.detectSeriesNames(files instanceof Collection ? files : [files as File], locale) return names == null || names.isEmpty() ? null : names.toList()[0] } diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy b/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy index d1580d7d..89a0eceb 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy @@ -1,6 +1,9 @@ // File operations import static net.sourceforge.tuned.FileUtilities.*; +// simplified switch/case pattern matching +Object.metaClass.match = { Map cases -> def val = delegate; cases.findResult { switch(val) { case it.key: return it.value} } } + /** * Allow getAt() for File paths @@ -36,7 +39,7 @@ String.metaClass.pad = Number.metaClass.pad = { length = 2, padding = "0" -> del /** * Return a substring matching the given pattern or break. */ -String.metaClass.match = { def matcher = delegate =~ it; if (matcher.find()) return matcher[0] else throw new Exception("Match failed") } +String.metaClass.match = { String pattern -> def matcher = delegate =~ pattern; if (matcher.find()) return matcher[0] else throw new Exception("Match failed") } /**