简体   繁体   English

为“空路径”测试 NIO.2 路径实例

[英]Testing a NIO.2 Path instance for “empty path”

So this trivial question has generated a disproportionate amount of discussion here.所以这个微不足道的问题在这里引起了不成比例的讨论。 It feels more like a playful puzzle but not full-on codegolf.感觉更像是一个有趣的谜题,但不是完整的代码高尔夫。

The javadoc for the NIO.2 Path class includes this part: NIO.2 Path class 的 javadoc 包括以下部分:

A Path is considered to be an empty path if it consists solely of one name element that is empty. 如果 Path 仅由一个空的名称元素组成,则 Path 被认为是空路径

followed by the "empty path maps to default directory" clause -- that behavior is well understood and not relevant to the question here.其次是“空路径映射到默认目录”子句——这种行为很好理解,与这里的问题无关。

The discussion arose from some of our junior developers asking: given a Path instance p , how should they test for an empty path condition?我们的一些初级开发人员提出了讨论:给定一个Path实例p ,他们应该如何测试空路径条件? Turns out the rest of their team (with more experience) had each been doing their own thing, and while all of their approaches "worked", they wanted to converge on the officially correct way;原来他们团队的 rest(有更多经验)每个人都在做自己的事情,虽然他们所有的方法都“奏效”了,但他们想集中在官方正确的方法上; I believe there may have been a round of beers at stake.我相信可能有一轮啤酒处于危险之中。

  • Testing for consists solely of one name element is trivial ( p.getNameCount() == 1 ).仅由一个名称元素组成的测试是微不足道的( p.getNameCount() == 1 )。 Testing for that is empty means obtaining that name element ( p.getName(0) or p.getFileName() ), which... is also a Path instance that needs to be tested for emptiness...测试它是否为空意味着获取该名称元素( p.getName(0)p.getFileName() ),这...也是需要测试是否为空的Path实例...

  • Calling p.toString() and then testing for isEmpty() felt distasteful, because the emptiness test is being done on a String representation of the path, not the path instance itself .调用p.toString()然后测试isEmpty()感觉令人反感,因为空性测试是在路径的字符串表示完成的,而不是路径实例本身 This sparked some philosophical debate about the completeness of the Path API and the meaning of canonical representations.这引发了一些关于路径 API 的完整性和规范表示的含义的哲学辩论。 I think they were already two beers in by then.我想那时他们已经是两杯啤酒了。

  • One developer pointed to the Path#resolve(Path other) method's javadocs, which contain the note一位开发人员指出Path#resolve(Path other)方法的 javadocs,其中包含注释

    If other is an empty path then this method trivially returns this path. 如果other空路径,则此方法通常会返回此路径。
    So his emptiness test uses an isolated Path instance, and tests for isolated.resolve(p).equals(isolated) , which seemed suspiciously too clever and apparently led to raised voices.所以他的空性测试使用了一个孤立的Path实例,并测试了isolated.resolve(p).equals(isolated) ,这似乎太聪明了,显然导致了提高的声音。

  • Another developer admitted to testing whether p was an instance of sun.nio.fs.UnixPath and then abusing reflection to accessing its private isEmpty() method.另一位开发人员承认测试p是否是sun.nio.fs.UnixPath的实例,然后滥用反射来访问其私有isEmpty()方法。 I wasn't present to ask what he does for Windows platforms, and suspect this wouldn't work in Java 9+ anyway.我没有在场询问他为 Windows 平台做了什么,并且怀疑这无论如何都不适用于 Java 9+。

In the end, they said they grudgingly settled on p.toString().length() == 0 but nobody was happy about it.最后,他们说他们不情愿地选择了p.toString().length() == 0 ,但没有人对此感到高兴。 None of them like the idea that the Path class depends on an "emptiness" quality that they could only apparently measure using methods of the String class, either before construction or after conversion.他们都不喜欢这样的想法,即路径 class 取决于“空”质量,他们只能在构造之前或转换之后使用字符串 class 的方法来明显地测量这种质量。 Presumably this solution was good enough for them to figure out who bought the beers, anyway.无论如何,这个解决方案大概足以让他们弄清楚谁买了啤酒。

Anyhow, once I heard about it I had to admit I was at a loss as to the best practice.无论如何,一旦我听说了它,我不得不承认我对最佳实践一无所知。 What do the experts do for this case?专家们为这个案子做了什么? Convert to String and be done with it, or stay within the NIO.2 API and take advantage of the resolve behavior, or...?转换为String并完成它,或者留在 NIO.2 API 中并利用resolve行为,或者......? (If you live near our other team, they might buy you a beer.) (如果你住在我们其他团队附近,他们可能会请你喝啤酒。)

Ideally, toString() should not be used for overall comparisons.理想情况下,不应将toString()用于整体比较。 And while you could use the resolve method, you really shouldn't.虽然你可以使用resolve方法,但你真的不应该。 (I won't even address the use of reflection for this.) (我什至不会为此使用反射。)

I believe you all are over-thinking this problem.相信大家都在想这个问题。 Just write what you mean.只写你的意思。 If you want to test if a Path is equal to the empty path, then do exactly that:如果要测试 Path 是否等于空路径,请执行以下操作:

if (somePath.equals(Paths.get("")))

I suppose you could store the empty path in a constant, but it's so trivial that I wouldn't bother.我想你可以将空路径存储在一个常量中,但它是如此微不足道,我不会打扰。 It might even make the code harder to read instead of making it easier.它甚至可能使代码更难阅读,而不是使它更容易。

If you don't want to do that, then your first instinct was correct: test for the conditions described in the documentation.如果您不想这样做,那么您的第一直觉是正确的:测试文档中描述的条件。

if (somePath.getNameCount() == 1 &&
    somePath.getFileName().toString().isEmpty())

Or:或者:

if (somePath.getNameCount() == 1 && somePath.endsWith(""))

I would prefer using equals , because when someone else reads the code, they will see code that shows your intent: to check whether a path is equal to the empty path.我更喜欢使用equals ,因为当其他人阅读代码时,他们会看到显示您意图的代码:检查路径是否等于空路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM