Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Keyboard utilities and Predicates

 
Bartender
Posts: 7077
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a  Keyboard Utility similar to Campbell's. In addition to his methods I have  one that will take a predicate so you can implement any arbitrary rule.
REPLACE WITH:
 
Marshal
Posts: 68917
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice
 
Sheriff
Posts: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:REPLACE WITH:


Nice but I think something like this would be nicer:

You can provide static factory methods that return predicates with closures on the parameters you provide them.

I kind of like Kotlin's semantics for "in 0..N" which is a range from 0-N, inclusive [0..N] and "in 0 until N" which is the range [0..N), exclusive of N. With those semantics, you might have

Apache Commons Lang and Google Guava libraries have nice implementation of Range, with lots of combinations for specifying open- and closed-ended ranges but that would mean pulling in more dependencies.
 
Junilu Lacar
Sheriff
Posts: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementations:

You can static import these so the client code isn't so verbose.
 
Carey Brown
Bartender
Posts: 7077
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, I had to add a type in jdk 14 to get it to compile. Without it it says it can't compare an Object to an int.
Even so, in my app I get a conversion suppression warning on line 1 here.

 
Saloon Keeper
Posts: 3900
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could make some Predicates generic, for instance:

with use, say,
 
Junilu Lacar
Sheriff
Posts: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles fine for me, JDK 14. IntelliJ IDEA doesn't give me any kind of warnings.
<br /> Usage: <br />
We've digressed a bit but I think OP has left the thread at this point. I kind of like your idea of putting the Predicates in a separate class though.
 
Junilu Lacar
Sheriff
Posts: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there's a need to use a static block to initialize the Scanner instance on lines 9-11. That was just a remnant of the example I took that showed lazy initialization with a System.out.println(..) statement thrown into the static block.
 
Carey Brown
Bartender
Posts: 7077
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 3 & 6 don't complain but line 9 complains. 'and' belongs to the raw type Predicate and that what is needed is Predicate<T>. I'm not sure how I'd do that.

A minor side annoyance is I can't do a static import of the class Predicates which is in the same package as my app.

 
Junilu Lacar
Sheriff
Posts: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, I pulled the predicates into a separate class/file and I still didn't get any warnings/errors like that in IntelliJ IDEA, JDK 14. Not sure what's going on at your end but here's the entire program I have if that helps:
 
Carey Brown
Bartender
Posts: 7077
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, FOUND THE PROBLEM !!! It appears to be a bug with Eclipse and jdk 14. I have jdk 14 configured as the default and that is what was giving me the warnings. BUT if I manually configure it -- using the exact same settings -- for this specific project, then I don't get the warnings. I'm using Eclipse 2020-03 and jdk 14.0.1 under Windows 10.
 
Sheriff
Posts: 7056
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this thread is now about Scanner wrappers, I'll offer mine.  I'm embarrassed that it is still not very DRY, but one thing it does that I haven't seen the others do is provide a default value that can be taken just by pressing <enter>.  Because of this, all of the Scanner calls are nextLine().

It allows the user to send it a predicate to validate the input data and it has some built-in lambdas, but I like Junilu's suggestion and maybe I'll try to implement it.
 
No holds barred. And no bars holed. Except this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic