I want to filter grid content according to two filtering criterias, therefore I have the following
ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
SerializablePredicate<Person> filter = new SerializablePredicate<Person>() {
#Override
public boolean test(Person Person) {
return false;
}
};
filter.and(Person -> Person.getAge() == 30);
filter.and(Person -> Person.getName().equalsIgnoreCase("bernd"));
dataProvider.setFilter(filter);
}
However, the grid does not show anything.
Your conjunction always evaluates to false because it is modeled like:
false && Person.getAge() == 30 && Person.getName().equalsIgnoreCase("bernd")
Either you're using return true in the first filter, or you ommit it and start with Person.getAge() == 30.
Second problem is that you're ignoring the results of your conjunction. In addition to this, the method and is defined in Predicate and returns a non-serializable Predicate. Solution would be to use a simple &&.
SerializablePredicate<Person> filter = p -> p.getAge() == 30 &&
p.getName().equalsIgnoreCase("bernd");
There are two different problems.
First one:
The function
default Predicate<T> and(Predicate<? super T> other)
returns a new predicate.
ListDataProvider<Person> dataProvider = (ListDataProvider<Person>)
grid.getDataProvider();
SerializablePredicate<Person> filter = new SerializablePredicate<Person>() {
#Override
public boolean test(Person Person) {
return true;
}
};
filter = filter.and(Person -> Person.getAge() == 30);
filter = filter.and(Person -> Person.getName().equalsIgnoreCase("bernd"));
dataProvider.setFilter(filter);
The other problem is, that ListDataProvider expected a SerializablePredicate.
You can extend the SerializiablePredicate
public interface ExtendedSerializablePredicate<T> extends SerializablePredicate<T> {
default ExtendedSerializablePredicate<T> and(ExtendedSerializablePredicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
}
now you can use this:
ListDataProvider<Person> dataProvider = (ListDataProvider<Person>)
grid.getDataProvider();
ExtendedSerializablePredicate<Person> filter = new ExtendedSerializablePredicate<Person>() {
#Override
public boolean test(Person Person) {
return true;
}
};
filter = filter.and(Person -> Person.getAge() == 30);
filter = filter.and(Person -> Person.getName().equalsIgnoreCase("bernd"));
dataProvider.setFilter(filter);
Related
I am creating a spring boot project and working on spring data jpa and currently I am using custom query to fetch data from db based on users selection and this is my page where user can select option based on their condition https://i.imgur.com/coO3BCJ.png
So, I googled it and found we can use specification but as I am very new to specification, so, I used specification and I want that based on users choice, it should keep adding specification, so, this is my conditional specification...
Specification<UserDetails> specification =
Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage()));
if(!customSearch.getManglik().isBlank()) {
specification.and(UserDetailsSpecification.isManglik
(customSearch.getManglik()));
}
if(!customSearch.getMaritalStatus().isBlank()) {
specification.and(UserDetailsSpecification
.hasMaritalStatus(customSearch.getMaritalStatus()));
}
if(!customSearch.getReligion().isBlank()) {
specification.and(UserDetailsSpecification.hasReligion
(customSearch.getReligion()));
}
if(!customSearch.getCaste().isBlank()) {
specification.and(UserDetailsSpecification.hasCaste
(customSearch.getCaste()));
}
if(!customSearch.getLocation().isBlank()) {
specification.and(UserDetailsSpecification.hasLocation
(customSearch.getLocation()));
}
listOfCustomSearch=userDetailsRepository
.findAll(specification, pageable);
List<UserDetails> listOfAllSearchedUsers = listOfCustomSearch.getContent();
but it is not appending the specification and just filtering the data based on only
Specification<UserDetails> specification = Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage()));
so, based on users selection, so, my final query should be something like this(If user has selected all fields):
Specification<UserDetails> specification = Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage())
.and(UserDetailsSpecification.isManglik(customSearch.getManglik()) .and(UserDetailsSpecification.hasMaritalStatus(customSearch.getMaritalStatus())) .and(UserDetailsSpecification.hasReligion(customSearch.getReligion()))
.and(UserDetailsSpecification.hasCaste(customSearch.getCaste()))
.and(UserDetailsSpecification.hasLocation(customSearch.getLocation()))))
But suppose if user has selected only let suppose 3 or 4 fields, so, my final specification should be something like below:(This specification should be completely depends upon user selection)
Specification<UserDetails> specification = Specification.where(UserDetailsSpecification
.isAgeBetween(customSearch.getFromage(), customSearch.getToage())
.and(UserDetailsSpecification.isManglik(customSearch.getManglik())
.and(UserDetailsSpecification.hasLocation(customSearch.getLocation()))))
Currently it is not appending specification based on users selection, so, please help me in adding specification based on users selections
Finally, I found the solution and we can create customized specification like this below:
public static Specification<UserDetails> getSpecs(String gender, int fromAge, int toAge, String manglikStatus, String maritalStatus, String religion, String caste, String location){
Specification<UserDetails> spec = null;
Specification<UserDetails> temp = null;
if(!gender.isBlank() && !gender.isEmpty() && gender!=null && !gender.contains("$")) {
spec = getSpecsForGenderDetails(gender);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(fromAge!=0 || toAge!=0) {
spec = isAgeBetween(fromAge, toAge);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!manglikStatus.isBlank() && !manglikStatus.isEmpty() && manglikStatus!=null && !manglikStatus.contains("$")) {
spec = isManglik(manglikStatus);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!maritalStatus.isBlank() && !maritalStatus.isEmpty() && maritalStatus!=null && !maritalStatus.contains("$")) {
spec = hasMaritalStatus(maritalStatus);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!religion.isBlank() && !religion.isEmpty() && religion!=null && !religion.contains("$")) {
spec = hasReligion(religion);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!caste.isBlank() && !caste.isEmpty() && caste!=null && !caste.equalsIgnoreCase("select") && !caste.contains("$")) {
spec = hasCaste(caste);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
if(!location.isBlank() && !location.isEmpty() && location!=null && !location.contains("$")) {
spec = hasLocation(location);
temp = spec!=null?Specification.where(spec).and(temp):temp;
}
return temp;
}
And based on that we can define our method like this:
private static Specification<UserDetails> getSpecsForGenderDetails(String gender) {
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.equal(root.get("gender"),gender);
});
}
private static Specification<UserDetails> isAgeBetween(int fromAge, int toAge){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.between(root.get("age"), fromAge, toAge);
});
}
private static Specification<UserDetails> isManglik(String manglikStatus){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("manglikStatus")),"%" +manglikStatus.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasMaritalStatus(String maritalStatus){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("maritalStatus")),"%" +maritalStatus.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasReligion(String religion){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("religion")),"%" +religion.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasCaste(String caste){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("caste")),"%" +caste.toLowerCase() +"%");
});
}
private static Specification<UserDetails> hasLocation(String presentState){
return ((root, query, criteriaBuilder) -> {
return criteriaBuilder.like(criteriaBuilder.lower(root.get("presentState")),"%" +presentState.toLowerCase() +"%");
});
}
protected static double averagePrice( List<ComputerComponent> list ) {
return list.stream()
// .filter( line-> EnumUtils.isValidEnum(ComputerComponentCategory.class, line.getCategory()) )
// .filter( line-> isInEnum( line.getCategory(), ComputerComponentCategory.class) )
// .filter( line-> inEnum(line.getCategory(),EnumUtils.getEnumMap(ComputerComponentCategory.class ).keySet() ))
.filter( line ->
line.getCategory().contains("CPU")
|| line.getCategory().contains("GPU")
|| line.getCategory().contains("Monitor")
|| line.getCategory().contains("Keyboard")
|| line.getCategory().contains("Mouse")
|| line.getCategory().contains("Storage")
|| line.getCategory().contains("Memory"))
.mapToDouble(ComputerComponent::getPrice)
.average()
.orElseThrow(NoSuchElementException:: new);
}
I have an enum as
public enum ComputerComponentCategory {
CPU("CPU"),
MONITOR("Monitor"),
KEYBOARD("Keyboard"),
MOUSE("Mouse"),
GPU("GPU"),
MEMORY("Memory"),
STORAGE("Storage"),
NULL("NOT DEFINED");
private String label;
ComputerComponentCategory(String label) {
this.label = label;
}
public String getLabel() {
return this.label;
}
public static ComputerComponentCategory getValue(String label) {
switch(label) {
case "CPU":
return CPU;
case "Monitor":
return MONITOR;
case "Keyboard":
return KEYBOARD;
case "Mouse":
return MOUSE;
case "GPU":
return GPU;
case "Memory":
return MEMORY;
case "Storage":
return STORAGE;
default:
return NULL ;
}
}
}
I pass a list of ComputerComponent class to the averagePrice() function which has two fields of price
which is of type double and
category which is of type String.
My list has 4 elements with categories as "CPU", "Mouse",
"Keyboard" and "Storage" with their respective prices as 34.0, 155.0, 23.0 and 75.0.
When I try to use inEnum(), isInEnum() or EnumUtils.isValidEnum() functions, I get the average price
as 34.0 which I think that they just return the price of the first element rather than the average.
But when I do filtering using
.filter( line ->
line.getCategory().contains("CPU")
|| line.getCategory().contains("GPU")
|| line.getCategory().contains("Monitor")
|| line.getCategory().contains("Keyboard")
|| line.getCategory().contains("Mouse")
|| line.getCategory().contains("Storage")
|| line.getCategory().contains("Memory"))
I get the correct average value of 71.75.
The implementations that I have used for isInEnum() and inEnum() functions are the following:
public static <E extends Enum<E>> boolean isInEnum(String value, Class<E> enumClass) {
for (E e : enumClass.getEnumConstants()) {
if(e.name().contains(value)) { return true; }
}
return false;
}
public static boolean inEnum ( String category, Set<String> value ) {
for(String s: value ) {
if ( category.contains(s) ) {
return true ;
}
}
return false ;
}
How can I use enums correctly with java streams to filter by valid category names and get the correct
average value of price?
What mistake I am making when using streams and its functions ?
You could simply use your ComputerCategoryValue.getValue method and check for null, given the category of line:
public class EnumTest {
#Test
public void testBothMethods() {
final ComputerComponent c1 = new ComputerComponent(ComputerComponentCategory.CPU.getLabel(), 12.21);
final ComputerComponent c2 = new ComputerComponent(ComputerComponentCategory.MEMORY.getLabel(), 23.45);
final List<ComputerComponent> list = Arrays.asList(c1, c2);
assertEquals(averagePriceWithFilter(list), averagePriceWithInEnum(list), 0.01);
}
protected static double averagePriceWithFilter(final List<ComputerComponent> list) {
return list.stream()
.filter(line -> line.getCategory().contains("CPU")
|| line.getCategory().contains("GPU")
|| line.getCategory().contains("Monitor")
|| line.getCategory().contains("Keyboard")
|| line.getCategory().contains("Mouse")
|| line.getCategory().contains("Storage")
|| line.getCategory().contains("Memory"))
.mapToDouble(ComputerComponent::getPrice)
.average()
.orElseThrow(NoSuchElementException::new);
}
protected static double averagePriceWithInEnum(final List<ComputerComponent> list) {
return list.stream()
.filter(line -> ComputerComponentCategory.getValue(line.getCategory()) != null)
.mapToDouble(ComputerComponent::getPrice)
.average()
.orElseThrow(NoSuchElementException::new);
}
}
EDIT: explaining your mistakes:
EnumUtils.getEnumMap(ComputerComponentCategory.class).keySet()) returns a map of the enum name (not its label), so that the check will only work for CPU as there name and label are the same.
Same for the other method!
You need to use getLabel() instead of name() or use equalsIgnoreCase instead of contains.
Thanks in advance. I can get required output when using var but i want to get required output by using Distinct in List<>.
InventoryDetails.cs
public class InventoryDetails
{
public int? PersonalInventoryGroupId { get; set; }
public int? PersonalInventoryBinId { get; set; }
}
InventoryController.cs
[HttpGet("GetInventory")]
public IActionResult GetInventory(int id)
{
//Below code will return distinct record
var inventory = (from i in _context.TempTbl
where i.TempId == id
select new
{
PersonalInventoryBinId = i.PersonalInventoryBinId,
PersonalInventoryGroupId = i.PersonalInventoryGroupId,
}).ToList().Distinct().ToList();
//Below code is not doing distinct
List<InventoryDetails> inventory = (from i in _context.TempTbl
where i.TempId == id
select new InventoryDetails
{
PersonalInventoryBinId = i.PersonalInventoryBinId,
PersonalInventoryGroupId = i.PersonalInventoryGroupId,
}).ToList().Distinct().ToList();
}
If i use var as return type, then i am able to get distinct records. Could some one assist it.
Please try like this it may help.
IList<InventoryDetails> inventory = _context.InventoryDetails.Where(x=>x.TempId == id).GroupBy(p => new {p.PersonalInventoryGroupId, p.PersonalInventoryBinId } )
.Select(g => g.First())
.ToList();
You need to override Equals and GetHashCode.
First, let's see the AnonymousType vs InventoryDetails
var AnonymousTypeObj1 = new { PersonalInventoryGroupId = 1, PersonalInventoryBinId = 1 };
var AnonymousTypeObj2 = new { PersonalInventoryGroupId = 1, PersonalInventoryBinId = 1 };
Console.WriteLine(AnonymousTypeObj1.Equals(AnonymousTypeObj2)); // True
var InventoryDetailsObj1 = new InventoryDetails { PersonalInventoryBinId = 1, PersonalInventoryGroupId = 1 };
var InvertoryDetailsObj2 = new InventoryDetails { PersonalInventoryBinId = 1, PersonalInventoryGroupId = 1 };
Console.WriteLine(InventoryDetailsObj1.Equals(InvertoryDetailsObj2)); // False
You can see the Equals behave differently which make Distinct behave differently. The problem is not var you mentioned in your question but AnonoymizeType
To make Distinct works as you expect, you need to override Equals and GetHashCode
public class InventoryDetails
{
public int? PersonalInventoryGroupId { get; set; }
public int? PersonalInventoryBinId { get; set; }
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj is InventoryDetails)
{
if (PersonalInventoryGroupId == (obj as InventoryDetails).PersonalInventoryGroupId
&& PersonalInventoryBinId == (obj as InventoryDetails).PersonalInventoryBinId)
return true;
}
return false;
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + PersonalInventoryBinId.GetHashCode();
hash = hash * 23 + PersonalInventoryGroupId.GetHashCode();
return hash;
}
}
Another approach would be
List<InventoryDetails> inventory = (from i in TempTbl
where i.TempId == id
select new InventoryDetails
{
PersonalInventoryBinId = i.PersonalInventoryBinId,
PersonalInventoryGroupId = i.PersonalInventoryGroupId,
}).AsQueryable().ToList().Distinct(new customComparer()).ToList();
public class customComparer:IEqualityComparer<InventoryDetails>
{
public bool Equals(InventoryDetails x, InventoryDetails y)
{
if (x.TempId == y.TempId && x.PersonalInventoryBinId == y.PersonalInventoryBinId
&& x.PersonalInventoryGroupId == y.PersonalInventoryGroupId)
{
return true;
}
return false;
}
public int GetHashCode(InventoryDetails obj)
{
return string.Concat(obj.PersonalInventoryBinId.ToString(),
obj.PersonalInventoryGroupId.ToString(),
obj.TempId.ToString()).GetHashCode();
}
}
As said in a comment by Ivan, you make your life difficult by calling ToList before Distinct. This prevents the SQL provider from incorporating the Distinct call into the generated SQL statement. But that leaves the question: what causes the difference?
The first query generates anonymous type instances. As per the C# specification, by default anonymous types (in C#) are equal when their properties and property values are equal (structural equality). Conversely, by default, reference types (like InventoryDetails) are equal when their reference (say memory address) is equal (reference equality or identity). They can be made equal by overriding their Equals and GetHashcode methods, as some people suggested to do.
But that's not necessary if you remove the first ToList():
var inventory = (from i in _context.TempTbl
where i.TempId == id
select new InventoryDetails
{
PersonalInventoryBinId = i.PersonalInventoryBinId,
PersonalInventoryGroupId = i.PersonalInventoryGroupId,
}).Distinct().ToList();
Now the whole statement until ToList() is an IQueryable that can be translated into SQL. The SQL is executed and the database returns a distinct result set of raw records from which EF materializes InventoryDetails objects. The C# runtime code was even never aware of duplicates!
How is it possible to filter Nodes in a JavaFX 2 TreeView?
I have a TextField and I want to filter all Nodes (for example node labels) based on the content of the TextField.
Thanks.
this is reusable filterable tree item class i've wrote.
the filter should be bound on predicateProperty, and you must use getSourceChildren method to manipulate tree items.
public class FilterableTreeItem<T> extends TreeItem<T> {
private final ObservableList<TreeItem<T>> sourceChildren = FXCollections.observableArrayList();
private final FilteredList<TreeItem<T>> filteredChildren = new FilteredList<>(sourceChildren);
private final ObjectProperty<Predicate<T>> predicate = new SimpleObjectProperty<>();
public FilterableTreeItem(T value) {
super(value);
filteredChildren.predicateProperty().bind(Bindings.createObjectBinding(() -> {
Predicate<TreeItem<T>> p = child -> {
if (child instanceof FilterableTreeItem) {
((FilterableTreeItem<T>) child).predicateProperty().set(predicate.get());
}
if (predicate.get() == null || !child.getChildren().isEmpty()) {
return true;
}
return predicate.get().test(child.getValue());
};
return p;
} , predicate));
filteredChildren.addListener((ListChangeListener<TreeItem<T>>) c -> {
while (c.next()) {
getChildren().removeAll(c.getRemoved());
getChildren().addAll(c.getAddedSubList());
}
});
}
public ObservableList<TreeItem<T>> getSourceChildren() {
return sourceChildren;
}
public ObjectProperty<Predicate<T>> predicateProperty() {
return predicate;
}
}
There is no special filter, provided by JFX.
So you should implement it by yourself.
The only support from JFX you have - tracking of collection of TreeItems' items. When you add or remove an item, it will be added or removed. But adding or removing from collections you implement yourself.
Operator overloading is working perfect in C# code, since I am trying in the following way.
**
public class HostCode
{
public string Code { get; set; }
public string Name { get; set; }
public static bool operator ==(HostCode hc1, HostCode hc2)
{
return hc1.Code == hc2.Code;
}
public static bool operator !=(HostCode hc1, HostCode hc2)
{
return true;
}
}**
I have a clas called HostCode and it contains 2 overloading methods (one for '==' and another for '!=')
And I created a collection of Host Codes below.
**var hostCodes = new List<HostCode>()
{
new HostCode(){ Code = "1", Name = "sreekanth" },
new HostCode(){ Code = "2", Name = "sajan" },
new HostCode(){ Code = "3", Name = "mathew" },
new HostCode(){ Code = "4", Name = "sachin" }
};**
***var hc = new HostCode() { Code = "1", Name = "sreekanth" };***
***var isEq = hostCodes[1] == hc;***
when I am trying like above, the respective operator method fired in HostCode class (in this case it is '=='). So that I can write my custom logic there.
But if Iam trying with a Linq query as below, it is not firing. But in this case also Iam comparing 2 objects having same type.
**var isEqual = from obj in hostCodes where (HostCode)obj == (HostCode)hc select obj;**
Can anyone please help me to find out a way which I can compare 2 objects by Linq queries?
You can use IEqualityComparer or override equals for this purpose.
public class HostCode
{
....
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj as HostCode == null ? false : (obj as HostCode).Code == Code;
}
}
And IEqualityComparer usage is when you have some equality on object and in some functions like Contain, ... you want use some specific equality:
public class EqualityComparer : IEqualityComparer<HostCode >
{
public bool Equals(HostCode x, HostCode y)
{
return y.ID == x.ID;
}
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}
}
Since your question is "how should I compare them for this to work" I would answer "you should overload and use .Equals() instead of =="
And what's with your overload definition of !=?