Author: jonl
Date: Thu Jul 22 23:22:41 2010
New Revision: 966898

URL: http://svn.apache.org/viewvc?rev=966898&view=rev
Log:
added a few convenient methods, made folder and file methods non-final to allow 
sub-classing

Modified:
    wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/File.java
    
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Folder.java
    
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/time/DurationTest.java

Modified: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/File.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/File.java?rev=966898&r1=966897&r2=966898&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/File.java 
(original)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/File.java 
Thu Jul 22 23:22:41 2010
@@ -17,10 +17,16 @@
 package org.apache.wicket.util.file;
 
 import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
 import java.net.URI;
 
 import org.apache.wicket.util.io.Streams;
@@ -57,7 +63,9 @@ public class File extends java.io.File i
         * Construct.
         * 
         * @param parent
+        *            parent
         * @param child
+        *            child
         */
        public File(final java.io.File parent, final String child)
        {
@@ -109,7 +117,16 @@ public class File extends java.io.File i
        {
                super(uri);
        }
-
+       
+       /**
+        * @param name Name of child file
+        * @return Child file object
+        */
+       public File file(final String name)
+       {
+           return new File(this, name);
+       }
+       
        /**
         * @return File extension (whatever is after the last '.' in the file 
name)
         */
@@ -130,22 +147,51 @@ public class File extends java.io.File i
        {
                return new Folder(getParent());
        }
-
+       
+       /**
+        * @return Input stream that reads this file
+        * @throws FileNotFoundException Thrown if the file cannot be found
+        */
+       public InputStream inputStream() throws FileNotFoundException
+       {
+           return new BufferedInputStream(new FileInputStream(this));
+       }
+       
        /**
         * Returns a Time object representing the most recent time this file 
was modified.
         * 
         * @return This file's lastModified() value as a Time object
         */
-       public final Time lastModifiedTime()
+       public  Time lastModifiedTime()
        {
                return Time.milliseconds(lastModified());
        }
-
+       
+       /**
+        * Creates a buffered output stream that writes to this file.
+        * If the parent folder does not yet exist, creates all necessary
+        * folders in the path.
+        * @return Output stream that writes to this file
+        * @throws FileNotFoundException Thrown if the file cannot be found
+        */
+       public OutputStream outputStream() throws FileNotFoundException
+       {
+               final Folder parent = getParentFolder();
+               if (!parent.exists()) 
+               {
+                   if (!parent.mkdirs()) 
+                   {
+                       throw new FileNotFoundException("Couldn't create path " 
+ parent);
+                   }
+               }
+           return new BufferedOutputStream(new FileOutputStream(this));
+       }
+       
        /**
         * @return String read from this file
         * @throws IOException
         */
-       public final String readString() throws IOException
+       public String readString() throws IOException
        {
                final InputStream in = new FileInputStream(this);
                try
@@ -156,9 +202,28 @@ public class File extends java.io.File i
                {
                        in.close();
                }
+       } 
+       
+       /**
+        * @return Object read from serialization file
+        * @throws IOException
+        * @throws ClassNotFoundException
+        */
+       public Object readObject() throws IOException, ClassNotFoundException
+       {
+           return new ObjectInputStream(inputStream()).readObject();
        }
 
        /**
+        * @param object Object to write to this file
+        * @throws FileNotFoundException
+        * @throws IOException
+        */
+       public void writeObject(final Object object) throws 
FileNotFoundException, IOException
+       {
+           new ObjectOutputStream(outputStream()).writeObject(object);
+       }
+       /**
         * @return True if the file was removed
         * @see java.io.File#delete()
         */
@@ -184,7 +249,17 @@ public class File extends java.io.File i
                        in.close();
                }
        }
-
+       
+       /**
+        * @return This file in double quotes (useful for passing to 
+        *         commands and tools that have issues with spaces in 
+        *         filenames)
+        */
+       public String toQuotedString()
+       {
+           return "\"" + toString() + "\"";
+       }
+       
        /**
         * Writes the given file to this one
         * 
@@ -192,7 +267,7 @@ public class File extends java.io.File i
         *            The file to copy
         * @throws IOException
         */
-       public final void write(final File file) throws IOException
+       public void write(final File file) throws IOException
        {
                final InputStream in = new BufferedInputStream(new 
FileInputStream(file));
                try
@@ -213,7 +288,7 @@ public class File extends java.io.File i
         * @return Number of bytes written
         * @throws IOException
         */
-       public final int write(final InputStream input) throws IOException
+       public int write(final InputStream input) throws IOException
        {
                return Files.writeTo(this, input);
        }
@@ -225,7 +300,7 @@ public class File extends java.io.File i
         *            The string to write
         * @throws IOException
         */
-       public final void write(final String string) throws IOException
+       public void write(final String string) throws IOException
        {
                final FileWriter out = new FileWriter(this);
                try
@@ -238,3 +313,8 @@ public class File extends java.io.File i
                }
        }
 }
+
+
+
+
+

Modified: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Folder.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Folder.java?rev=966898&r1=966897&r2=966898&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Folder.java 
(original)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Folder.java 
Thu Jul 22 23:22:41 2010
@@ -28,7 +28,7 @@ import java.util.List;
  * 
  * @author Jonathan Locke
  */
-public final class Folder extends File
+public class Folder extends File
 {
        /**
         * Filter for files
@@ -146,6 +146,15 @@ public final class Folder extends File
                        throw new IOException("Unable to create folder " + 
this);
                }
        }
+       
+       /**
+        * @param name Name of child folder
+        * @return Child file object
+        */
+       public Folder folder(final String name)
+       {
+           return new Folder(this, name);
+       }
 
        /**
         * @return Files in this folder

Modified: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java?rev=966898&r1=966897&r2=966898&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java
 (original)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java
 Thu Jul 22 23:22:41 2010
@@ -88,7 +88,7 @@ public class LongValue implements Compar
 
                return false;
        }
-
+       
        /**
         * Compares this <code>LongValue</code> with a primitive 
<code>long</code> value.
         * 
@@ -100,6 +100,19 @@ public class LongValue implements Compar
        public final boolean greaterThan(final long value)
        {
                return this.value > value;
+       }       
+       
+       /**
+        * Compares this <code>LongValue</code> with a primitive 
<code>long</code> value.
+        * 
+        * @param value
+        *            the <code>long</code> value to compare with
+        * @return <code>true</code> if this <code>LongValue</code> is greater 
than or equal to 
+        *         the given <code>long</code> value
+        */
+       public final boolean greaterThanOrEqual(final long value)
+       {
+               return this.value >= value;
        }
 
        /**
@@ -114,6 +127,19 @@ public class LongValue implements Compar
        {
                return value > that.value;
        }
+       
+       /**
+        * Compares this <code>LongValue</code> with another 
<code>LongValue</code>.
+        * 
+        * @param that
+        *            the <code>LongValue</code> to compare with
+        * @return <code>true</code> if this <code>LongValue</code> is greater 
than or equal 
+        *         to the given <code>LongValue</code>
+        */
+       public final boolean greaterThanOrEqual(final LongValue that)
+       {
+               return value >= that.value;
+       }
 
        /**
         * Returns the hash code for this <code>Object</code>.
@@ -140,6 +166,19 @@ public class LongValue implements Compar
        }
 
        /**
+        * Compares this <code>LongValue</code> with a primitive 
<code>long</code> value.
+        * 
+        * @param that
+        *            the <code>long</code> value to compare with
+        * @return <code>true</code> if this <code>LongValue</code> is less 
than or equal 
+        *         to the given <code>long</code> value
+        */
+       public final boolean lessThanOrEqual(final long that)
+       {
+               return value <= that;
+       }
+       
+       /**
         * Compares this <code>LongValue</code> with another 
<code>LongValue</code>.
         * 
         * @param that
@@ -150,6 +189,19 @@ public class LongValue implements Compar
        public final boolean lessThan(final LongValue that)
        {
                return value < that.value;
+       }       
+       
+       /**
+        * Compares this <code>LongValue</code> with another 
<code>LongValue</code>.
+        * 
+        * @param that
+        *            the <code>LongValue</code> value to compare with
+        * @return <code>true</code> if this <code>LongValue</code> is less 
than or equal 
+        *         to the given <code>LongValue</code>
+        */
+       public final boolean lessThanOrEqual(final LongValue that)
+       {
+               return value <= that.value;
        }
 
        /**

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/time/DurationTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/util/time/DurationTest.java?rev=966898&r1=966897&r2=966898&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/time/DurationTest.java 
(original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/util/time/DurationTest.java 
Thu Jul 22 23:22:41 2010
@@ -58,7 +58,11 @@ public final class DurationTest extends 
        public void testOperations()
        {
                
Assert.assertTrue(Duration.milliseconds(3001).greaterThan(Duration.seconds(3)));
+               
Assert.assertTrue(Duration.milliseconds(3001).greaterThanOrEqual(Duration.seconds(3)));
+               
Assert.assertTrue(Duration.milliseconds(3000).greaterThanOrEqual(Duration.seconds(3)));
                
Assert.assertTrue(Duration.milliseconds(2999).lessThan(Duration.seconds(3)));
+               
Assert.assertTrue(Duration.milliseconds(2999).lessThanOrEqual(Duration.seconds(3)));
+               
Assert.assertTrue(Duration.milliseconds(3000).lessThanOrEqual(Duration.seconds(3)));
                Assert.assertEquals(-1, 
Duration.milliseconds(2999).compareTo(Duration.seconds(3)));
                Assert.assertEquals(1, 
Duration.milliseconds(3001).compareTo(Duration.seconds(3)));
                Assert.assertEquals(0, 
Duration.milliseconds(3000).compareTo(Duration.seconds(3)));


Reply via email to