Issue
How can one create a java.nio.Path that represents no path no matter the environment?
I cannot call Path.of(<String>)
and guarantee the resulting path to point to an invalid file or directory.
What I'm looking for is if there is a safer way to represent an empty / invalid path than using a null
value.
Solution
Depending on your use case, there are a couple options:
- If you are trying to return the path from a method, you can use
Optional<Path>
and returnOptional.empty()
to denote no path. - If this is a field in a class and that class implements some behavior, you can use the null object pattern to implement another class with the same interface that does "nothing" (whatever your application needs to do if there is no path).
Answered By - casablanca Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.