For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2008 > a boolean method inside an inner class.....









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2008-03-06, 10:20 pm

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

2008-03-06, 10:20 pm

maya wrote:
> 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...
>


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

You could change it to lower if you wanted too.

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Roedy Green

2008-03-07, 4:37 am

On Thu, 06 Mar 2008 22:20:21 -0500, maya <maya778899@yahoo.com> wrote,
quoted or indirectly quoted someone who said :

> 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

2008-03-07, 8:22 am

Roedy Green wrote:
> 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.

--
Lew
maya

2008-03-07, 7:23 pm

Lew wrote:[color=darkred]
> Roedy Green wrote:
>
> Here's a summary of memory/speed/complexity tradeoffs among the approaches.
>
>
> 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.
>

perfect -- thanks!! :)

Ian Shef

2008-03-07, 7:23 pm

maya <maya778899@yahoo.com> wrote in news:47d0b479$1@news.x-privat.org:

> 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...
>


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






Roedy Green

2008-03-07, 7:23 pm

On Thu, 06 Mar 2008 22:20:21 -0500, maya <maya778899@yahoo.com> wrote,
quoted or indirectly quoted someone who said :

> 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
Lew

2008-03-08, 4:37 am

Ian Shef wrote:
> 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/a....html#listFiles(java.io.FileFilter)>

The pattern is quite similar.

--
Lew
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com