Java Scala实现apache commons lang3验证的方法?
Scala实现与apache commons lang3相同的功能的方法是什么?i、 e.验证的目的是用户输入验证,而不是通过断言编码错误,如果条件失败,将导致Java Scala实现apache commons lang3验证的方法?,java,scala,Java,Scala,Scala实现与apache commons lang3相同的功能的方法是什么?i、 e.验证的目的是用户输入验证,而不是通过断言编码错误,如果条件失败,将导致 巧合的是,我刚刚发现require将是Scala中的首选方法 /** * Returns the newly created file only if the user entered a valid path. * @param path input path where to store the new file * @par
巧合的是,我刚刚发现
require
将是Scala中的首选方法
/**
* Returns the newly created file only if the user entered a valid path.
* @param path input path where to store the new file
* @param fileName name of the file to be created in directory path
* @throws IllegalArgumentException when the input path doesn't exist.
*/
def createFile(path: File, fileName: String) : File = {
require(path.exists, s"""Illegal input path "${path.getAbsolutePath()}", it doesn't exist""")
// now it is safe to create the file ...
val result = new File(path, fileName)
// ...
result
}
org.apache.commons.lang3.Validate(path.exists(),“非法输入路径””+path.getAbsolutePath()+“,它不存在”)
甚至有效吗?你打什么电话?无论如何,这个问题还不清楚。你想打电话给commons验证吗?或者您正在尝试在Scala中编写自己的类似验证函数?查看Validate
的构造函数时使用“此类通常不应实例化”。。。它不需要任何参数。因此@vptheron要求澄清您调用的方法是正确的,因为您所拥有的似乎不是链接到的org.apache.commons.lang3.Validate
类中的方法。很抱歉,mea culpa现在已经更新。我太习惯Scala应用程序了。我不想在这个例子中使用第三方库apache commons lang,因为Scala中有一个很好的方法来实现这一点。这是OT,我在OP中没有问抛出异常是否是一个糟糕的主意,我问什么是Scala中apache commons Validate的替代品。该示例只是验证用户输入的示例。
/**
* Returns the newly created file only if the user entered a valid path.
* @param path input path where to store the new file
* @param fileName name of the file to be created in directory path
* @throws IllegalArgumentException when the input path doesn't exist.
*/
def createFile(path: File, fileName: String) : File = {
require(path.exists, s"""Illegal input path "${path.getAbsolutePath()}", it doesn't exist""")
// now it is safe to create the file ...
val result = new File(path, fileName)
// ...
result
}