Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving data with streams from DB (problem)  RSS feed

 
Boris Petrov
Greenhorn
Posts: 21
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to everyone. I am very new to Java 8 features like streams, filters and stuff and the tell the truth, I haven't been writing in Java for more than a year.
Here is my problem if someone could help me.


The main idea is that I want return all enabled agents. gerConfigState() returns an enum (__ConfigState). not sure If am doing this correctly.
 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't declare Streams. Use them as part of a method call.
That predicate in line 4 looks very peculiar:-
a.equals(a.getConfigState().Enabled)
That will only return true if you have some sort of recursive definition so an enum element contains itself as a field of a field. If that is an enum element, why isn't it written ENABLED? If it an enum element, you can use the == operator instead of equals(). If that predicate is incorrect, it will always return false and you will return an empty List.
Don't declare Lists by their implementation.You are obviously aware of the stream() method of the Collection interface. That returns a Stream. Use its filter() method which can use that λ to create a Predicate object. Collect the Stream into a List with the collect() method. This takes a Collector as its argument and the easiest way to get a Collector is with one of the methods of the Collectors class. This method returns a Collector which creates a List.
Since the List returned is a different object from the original List, there is no need for any defensive copies or use of this sort of method.

You may be able to reduce that λ to a method reference:-
….filter(a::isEnabled)…
 
Boris Petrov
Greenhorn
Posts: 21
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the final version I came up:


I cant do e.getConfigState().Enable because this means that I am accessing the value staticly.
I cant use == operator beacuse my e.getConfigState is not boolean; (ConfigState is enuim)
 
Campbell Ritchie
Sheriff
Posts: 53749
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boris Petrov wrote:. . . I cant use == operator beacuse my e.getConfigState is not boolean; (ConfigState is enuim)
Yes, you can; look in the Java® Language Specification (same link as earlier). Try this:-The return type should be List, not ArrayList. I have got rid of the cast, which is not type‑safe because there is no guarantee that collect() will return any particular implementation. If you are declaring variables as type ArrayList elsewhere, you should change them to List, too. The superclass version of that method should also declare List as its return type. Why is that method overriding anything anyway? Do you really need an overridden version?
The documentation of the toList method (see my earlier post) suggests an alternative method you can use for more control over the exact type of List returned. But I think maybe you ought not to do that.
Why are you declaring throws Exception? That is probably wrong; I cannot see anything which would cause an Exception. If any exceptions occur, they might be SQLExceptions, so that makes me think that method is trying to do too much. If repository.all() simply returns a reference to your List, then why isn't that List a field? Then you can have a method which reads from the database and populates the List; that will risk the SQLExceptions. If you are doing it in this method, then this method is doing too much.
I think you need a List<Agent> field (declare it as List), you need a method to populate that List from the database, and you need to ensure the List is populated before you do anything else. If you call populateList(...) in the constructor, that method should be marked private or final (but not both).

[Edit]Maybe the Agent object should have an isEnabled() method:-
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!