@@ -47,10 +47,6 @@ object Path {
47
47
else name.substring(i + 1 ).toLowerCase
48
48
}
49
49
50
- // not certain these won't be problematic, but looks good so far
51
- implicit def string2path (s : String ): Path = apply(s)
52
- implicit def jfile2path (jfile : JFile ): Path = apply(jfile.toPath)
53
-
54
50
def onlyDirs (xs : Iterator [Path ]): Iterator [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
55
51
def onlyDirs (xs : List [Path ]): List [Directory ] = xs filter (_.isDirectory) map (_.toDirectory)
56
52
def onlyFiles (xs : Iterator [Path ]): Iterator [File ] = xs filter (_.isFile) map (_.toFile)
@@ -95,6 +91,7 @@ class Path private[io] (val jpath: JPath) {
95
91
/** Creates a new Path with the specified path appended. Assumes
96
92
* the type of the new component implies the type of the result.
97
93
*/
94
+ def / (child : String ): Path = new Path (jpath.resolve(child))
98
95
def / (child : Path ): Path = resolve(child)
99
96
def / (child : Directory ): Directory = / (child : Path ).toDirectory
100
97
def / (child : File ): File = / (child : Path ).toFile
@@ -117,31 +114,24 @@ class Path private[io] (val jpath: JPath) {
117
114
def walk : Iterator [Path ] = walkFilter(_ => true )
118
115
119
116
// identity
120
- def name : String = jpath.getFileName().toString
117
+ def name : String = jpath.getFileName() match {
118
+ case null => " "
119
+ case name => name.toString
120
+ }
121
121
def path : String = jpath.toString
122
- def normalize : Path = Path (jpath.normalize)
122
+ def normalize : Path = new Path (jpath.normalize)
123
123
124
- def resolve (other : Path ) = Path (jpath.resolve(other.jpath))
125
- def relativize (other : Path ) = Path (jpath.relativize(other.jpath))
124
+ def resolve (other : Path ) = new Path (jpath.resolve(other.jpath))
125
+ def relativize (other : Path ) = new Path (jpath.relativize(other.jpath))
126
126
127
127
def segments : List [String ] = (path split separator).toList filterNot (_.length == 0 )
128
128
129
129
/**
130
130
* @return The path of the parent directory, or root if path is already root
131
131
*/
132
- def parent : Directory = path match {
133
- case " " | " ." => Directory (" .." )
134
- case _ =>
135
- // the only solution <-- a comment which could have used elaboration
136
- if (segments.nonEmpty && segments.last == " .." )
137
- (path / " .." ).toDirectory
138
- else jpath.getParent match {
139
- case null =>
140
- if (isAbsolute) toDirectory // it should be a root. BTW, don't need to worry about relative pathed root
141
- else Directory (" ." ) // a dir under pwd
142
- case x =>
143
- Directory (x)
144
- }
132
+ def parent : Directory = jpath.normalize.getParent match {
133
+ case null => Directory (jpath)
134
+ case parent => Directory (parent)
145
135
}
146
136
def parents : List [Directory ] = {
147
137
val p = parent
@@ -164,12 +154,12 @@ class Path private[io] (val jpath: JPath) {
164
154
// returns the filename without the extension.
165
155
def stripExtension : String = name stripSuffix (" ." + extension)
166
156
// returns the Path with the extension.
167
- def addExtension (ext : String ): Path = Path (path + " . " + ext)
157
+ def addExtension (ext : String ): Path = new Path (jpath.resolveSibling(name + ext) )
168
158
// changes the existing extension out for a new one, or adds it
169
159
// if the current path has none.
170
160
def changeExtension (ext : String ): Path =
171
161
if (extension == " " ) addExtension(ext)
172
- else Path (path.stripSuffix(extension) + ext)
162
+ else new Path (jpath.resolveSibling(stripExtension + " . " + ext) )
173
163
174
164
// conditionally execute
175
165
def ifFile [T ](f : File => T ): Option [T ] = if (isFile) Some (f(toFile)) else None
0 commit comments