* Scala-style switch/case pattern matching for format engine

This commit is contained in:
Reinhard Pointner 2012-04-08 15:52:21 +00:00
parent 4521ee16b4
commit a295f24645
2 changed files with 7 additions and 4 deletions

View File

@ -37,7 +37,7 @@ File.metaClass.hasExtension = { String... ext -> hasExtension(delegate, ext) }
File.metaClass.isDerived = { f -> isDerived(delegate, f) } File.metaClass.isDerived = { f -> isDerived(delegate, f) }
File.metaClass.validateFileName = { validateFileName(delegate) } File.metaClass.validateFileName = { validateFileName(delegate) }
File.metaClass.validateFilePath = { validateFilePath(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())) } File.metaClass.copyTo = { dir -> copyAs(delegate, new File(dir, delegate.getName())) }
List.metaClass.mapByFolder = { mapByFolder(delegate) } List.metaClass.mapByFolder = { mapByFolder(delegate) }
List.metaClass.mapByExtension = { mapByExtension(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 = { 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') } 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) } URL.metaClass.saveAs = { f -> fetch(delegate).saveAs(f) }
String.metaClass.saveAs = { f, csn = "utf-8" -> Charset.forName(csn).encode(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 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] return names == null || names.isEmpty() ? null : names.toList()[0]
} }

View File

@ -1,6 +1,9 @@
// File operations // File operations
import static net.sourceforge.tuned.FileUtilities.*; 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 * 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. * 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") }
/** /**