Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

a boolean method inside an inner class.....

Thread view: 
maya - 07 Mar 2008 03:20 GMT
I have a problem with this code:

        String[] textFileNames = directory.list(new FilenameFilter() {
             public boolean accept(File dir, String name) {
                   return name.endsWith(".JPG");
                    }
                });

this is from a class I adapted from this example:

   http://www.zanthan.com/itymbi/archives/000805.html

I need to test for whether the file name ends with ".jpg" or ".JPG"..
(found out the hard way that this method is case-sensitive....;)

I don't know how to declare this method so it can accept ".JPG" or
".jpg"...  (also this method is called nowhere in this Rename class.. so
what's the purpose of it...)

thank you...
Knute Johnson - 07 Mar 2008 03:51 GMT
> I have a problem with this code:
>
[quoted text clipped - 16 lines]
>
> thank you...

name.toUpperCase().endsWith(".JPG")

You could change it to lower if you wanted too.

Signature

Knute Johnson
email s/nospam/linux/

Roedy Green - 07 Mar 2008 07:53 GMT
>     return name.endsWith(".JPG");

there are several ways to deal with the case problem:

(1)
return name.toLowerCase().endsWith(".jpg")
is the easiest to code. It also handles *.Jpg.

(2)
return name.endsWith(".jpg") || name.endsWith( ".JPG");
won't catch *.Jpg but this code should be faster.

(3)
if ( name.length() < ".jpg".length() ) return false;
final String ext = name.substring( name.length() - ".jpg".length());
return  ext.equalsIgnoreCase( ".jpg" );

a bit verbose, but would be quick if encapsulated in a method.
Masochists can covert it to a one-line expression.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 07 Mar 2008 12:25 GMT
> there are several ways to deal with the case problem:

Here's a summary of memory/speed/complexity tradeoffs among the approaches.

> (1)
> return name.toLowerCase().endsWith(".jpg")
> is the easiest to code. It also handles *.Jpg.

Creates a temporary String, makes /n/ + 1 passes where /n/ is the number of
different file suffixes to check, ignoring case.  Only the "toLowerCase()"
pass encompasses the entire String, the rest just examine enough of the ending
to determine a match.

> (2)
> return name.endsWith(".jpg") || name.endsWith( ".JPG");
> won't catch *.Jpg but this code should be faster.

Creates no additional Strings.  Makes /n/ passes where /n/ is the number of
endings for which you're searching.  Those are "from-the-end, only to match"
passes, not the entire length of the String.

This might be faster than the first choice for only one suffix, but if you are
checking multiple suffixes the first approach gets complete case independence
with half as many comparisons as the second, amortizing the cost of the
toLowerCase() call.

> (3)
> if ( name.length() < ".jpg".length() ) return false;
> final String ext = name.substring( name.length() - ".jpg".length());
> return  ext.equalsIgnoreCase( ".jpg" );

In most JVMs will not create a new temporary character sequence, just a new
reference to an offset in the original String's character sequence.  Makes /n/
passes through just the ending part of the String, with /k/ calculations of
ending String (where /k/ is the number of different ending lengths).

> a bit verbose, but would be quick if encapsulated in a method.

with some additional risk of bugs due to the more complex code.

> Masochists can covert it to a one-line expression.

If there's only one ending to check.  If you generalize to multiple file
suffixes it probably can't be a one-liner.

Without profiling one cannot tell which of these three would be the fastest.
It depends on how many different file suffixes are of interest, whether mixed
case is an issue, how different JVMs optimize things and other variables.  The
speed difference among the three is likely to be not significant.  I
personally would use the first one, the toLowerCase() variant, unless I had
proof that it was a bottleneck.

Signature

Lew

maya - 07 Mar 2008 14:47 GMT
>> there are several ways to deal with the case problem:
>
[quoted text clipped - 10 lines]
>> (2)
>> return name.endsWith(".jpg") || name.endsWith( ".JPG");

perfect -- thanks!!  :)
Ian Shef - 07 Mar 2008 19:23 GMT
> I have a problem with this code:
>
[quoted text clipped - 16 lines]
>
> thank you...

Others have already responded to your first question but (based on the
responses that I have seen) have ignored your second question.  I will
attempt to answer it.

Method list of class File takes one parameter, a FilenameFilter.
FilenameFilter is an interface with a single method: accept.

The list method of File will use (by calling its accept method) the
FilenameFilter that you supply to determine what names to return to you.

The code shown defines an anonymous inner class and supplies it as the
parameter to the list method.

An anonymous inner class is a convenience for defining a (usually small)
class on the spot.  For more information on this topic, see a book or
http://mindprod.com/jgloss/anonymousclasses.html
and
http://mindprod.com/jgloss/innerclasses.html
Lew - 08 Mar 2008 04:48 GMT
> Method list of class File takes one parameter, a FilenameFilter.
> FilenameFilter is an interface with a single method: accept.
>
> The list method of File will use (by calling its accept method) the
> FilenameFilter that you supply to determine what names to return to you.

As an alternative,
<http://java.sun.com/javase/6/docs/api/java/io/File.html#listFiles(java.io.FileFilter)>

The pattern is quite similar.

Signature

Lew

Roedy Green - 07 Mar 2008 23:45 GMT
>         String[] textFileNames = directory.list(new FilenameFilter() {
>              public boolean accept(File dir, String name) {
>                    return name.endsWith(".JPG");
>                     }
>                 });

I have written a whole set of filters. One of them take a list of file
extensions.  See http://mindprod.com/products1.html#FILTER
Includes source code for you to use or copy.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.