Quantcast

DGM proposal: File.mkParentDirs()

classic Classic list List threaded Threaded
12 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

DGM proposal: File.mkParentDirs()

Andre Steingress
Hi,

Based on

https://jira.codehaus.org/browse/GROOVY-4990

I'd like to propose a new DGM for java.io.File:

def f = new File('/some/dir/subdir/subsubdir/file.txt')
f.mkParentDirs().write('test')

mkParentDirs() will recursively create all non-existent parent directories. In the example above, if /some/dir would exist, it would create subdir/subsubdir/. If the parent path exists/has been created successfully or no path has been specified, mkParentDirs() returns the actual java.io.File instance. If not, an IOException is thrown.

Opinions? Suggestions?

Cheers, André
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

tim yates-2

Would an overloaded

  static File mkdirs( File self, boolean parents )

Be better than introducing a new method name?

The functionality would be really useful though... Encountered a need for it only today!

Tim

On 22 Feb 2012 21:04, "Andre Steingress" <[hidden email]> wrote:
Hi,

Based on

https://jira.codehaus.org/browse/GROOVY-4990

I'd like to propose a new DGM for java.io.File:

def f = new File('/some/dir/subdir/subsubdir/file.txt')
f.mkParentDirs().write('test')

mkParentDirs() will recursively create all non-existent parent directories.
In the example above, if /some/dir would exist, it would create
subdir/subsubdir/. If the parent path exists/has been created successfully
or no path has been specified, mkParentDirs() returns the actual
java.io.File instance. If not, an IOException is thrown.

Opinions? Suggestions?

Cheers, André

--
View this message in context: http://groovy.329449.n5.nabble.com/DGM-proposal-File-mkParentDirs-tp5506254p5506254.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

Andre Steingress
Tim Yates wrote
Would an overloaded

  static File mkdirs( File self, boolean parents )

Be better than introducing a new method name?
What do others think?

If parents == true we would need something like

self.exists() || self.getParentFile() == null || self.getParentFile().exists() || self.getParentFile().mkdirs()

else we would default to self.mkdirs().

Cheers, André
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

Remi Forax
In reply to this post by tim yates-2
On 02/22/2012 10:35 PM, Tim Yates wrote:

>
> Would an overloaded
>
>   static File mkdirs( File self, boolean parents )
>
> Be better than introducing a new method name?
>
> The functionality would be really useful though... Encountered a need
> for it only today!
>
> Tim
>

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createDirectories%28java.nio.file.Path,%20java.nio.file.attribute.FileAttribute...%29

Rémi

> On 22 Feb 2012 21:04, "Andre Steingress" <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi,
>
>     Based on
>
>     https://jira.codehaus.org/browse/GROOVY-4990
>
>     I'd like to propose a new DGM for java.io.File:
>
>     def f = new File('/some/dir/subdir/subsubdir/file.txt')
>     f.mkParentDirs().write('test')
>
>     mkParentDirs() will recursively create all non-existent parent
>     directories.
>     In the example above, if /some/dir would exist, it would create
>     subdir/subsubdir/. If the parent path exists/has been created
>     successfully
>     or no path has been specified, mkParentDirs() returns the actual
>     java.io.File instance. If not, an IOException is thrown.
>
>     Opinions? Suggestions?
>
>     Cheers, André
>
>     --
>     View this message in context:
>     http://groovy.329449.n5.nabble.com/DGM-proposal-File-mkParentDirs-tp5506254p5506254.html
>     Sent from the groovy - dev mailing list archive at Nabble.com.
>
>     ---------------------------------------------------------------------
>     To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

tim yates-2

Think we have to currently stick to java 5 as the lowest common version

On 22 Feb 2012 21:47, "Rémi Forax" <[hidden email]> wrote:
On 02/22/2012 10:35 PM, Tim Yates wrote:

Would an overloaded

 static File mkdirs( File self, boolean parents )

Be better than introducing a new method name?

The functionality would be really useful though... Encountered a need for it only today!

Tim


http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createDirectories%28java.nio.file.Path,%20java.nio.file.attribute.FileAttribute...%29

Rémi

On 22 Feb 2012 21:04, "Andre Steingress" <[hidden email] <mailto:[hidden email]>> wrote:

   Hi,

   Based on

   https://jira.codehaus.org/browse/GROOVY-4990

   I'd like to propose a new DGM for java.io.File:

   def f = new File('/some/dir/subdir/subsubdir/file.txt')
   f.mkParentDirs().write('test')

   mkParentDirs() will recursively create all non-existent parent
   directories.
   In the example above, if /some/dir would exist, it would create
   subdir/subsubdir/. If the parent path exists/has been created
   successfully
   or no path has been specified, mkParentDirs() returns the actual
   java.io.File instance. If not, an IOException is thrown.

   Opinions? Suggestions?

   Cheers, André

   --
   View this message in context:
   http://groovy.329449.n5.nabble.com/DGM-proposal-File-mkParentDirs-tp5506254p5506254.html
   Sent from the groovy - dev mailing list archive at Nabble.com.

   ---------------------------------------------------------------------
   To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

tim yates-2
In reply to this post by Andre Steingress

Should be able to use:

if( parent ) {
    File p = self.getParentFile() ;
    if( p != null ) {
        p.mkdirs() ;
    }
}
else {
    self.mkdirs() ;
}
return self ;

But that is just code typed into my phone whilst Sat print off the tv, and not checked in any way whatsoever ;-)

On 22 Feb 2012 21:47, "Andre Steingress" <[hidden email]> wrote:

Tim Yates wrote
>
> Would an overloaded
>
>   static File mkdirs( File self, boolean parents )
>
> Be better than introducing a new method name?
>
>

What do others think?

If parents == true we would need something like

self.exists() || self.getParentFile() == null ||
self.getParentFile().exists() || self.getParentFile().mkdirs()

else we would default to self.mkdirs().

Cheers, André

--
View this message in context: http://groovy.329449.n5.nabble.com/DGM-proposal-File-mkParentDirs-tp5506254p5506356.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

tim yates-2

'Sat print off' == 'sat in front of'

Stupid phone...

On 22 Feb 2012 22:03, "Tim Yates" <[hidden email]> wrote:
>
> But that is just code typed into my phone whilst Sat print off the tv, and not checked in any way whatsoever ;-)
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

Andre Steingress
In reply to this post by tim yates-2
Tim Yates wrote
Should be able to use:

if( parent ) {
    File p = self.getParentFile() ;
    if( p != null ) {
        p.mkdirs() ;
    }
}
else {
    self.mkdirs() ;
}
return self ;
The only issue I see with overloading java.io.File.mkdirs() are the return types. If we choose mkdirs() as the method name, than mkdirs() and mkdirs(true/false) will have different return types:

def f = new File('/some/dir/subdir/subsubdir/file.txt')
f.mkdirs() // return true/false

f.mkdirs(false) // returns f

Cheers, André



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

tim yates-2

True... I'll see if I can think of an already existing president for doing this

If not, maybe something snappier like mkpath could be used

I quite like the idea of being able to make a path and having the function return the file rather than the current boolean

Tim

On 22 Feb 2012 22:14, "Andre Steingress" <[hidden email]> wrote:

Tim Yates wrote
>
> Should be able to use:
>
> if( parent ) {
>     File p = self.getParentFile() ;
>     if( p != null ) {
>         p.mkdirs() ;
>     }
> }
> else {
>     self.mkdirs() ;
> }
> return self ;
>

The only issue I see with overloading java.io.File.mkdirs() are the return
types. If we choose mkdirs() as the method name, than mkdirs() and
mkdirs(true/false) will have different return types:

def f = new File('/some/dir/subdir/subsubdir/file.txt')
f.mkdirs() // return true/false

f.mkdirs(false) // returns f

Cheers, André





--
View this message in context: http://groovy.329449.n5.nabble.com/DGM-proposal-File-mkParentDirs-tp5506254p5506415.html
Sent from the groovy - dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: DGM proposal: File.mkParentDirs()

Andre Steingress
Tim Yates wrote
True... I'll see if I can think of an already existing president for doing
this

If not, maybe something snappier like mkpath could be used
A snappier (but slightly more geekier) name than 'mkParentDirs' would be 'mkdirsp'. On the one hand referring to File.mkdirs(), on the other to "mkdir -p".

Besides the name, would this DGM be seen as reasonable by the other Groovy devs?

Cheers, André
12
Loading...