io.github.palexdev.mfxcore.filter.EnumFilter Maven / Gradle / Ivy
/*
* Copyright (C) 2022 Parisi Alessandro - [email protected]
* This file is part of MaterialFX (https://github.com/palexdev/MaterialFX)
*
* MaterialFX is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* MaterialFX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MaterialFX. If not, see .
*/
package io.github.palexdev.mfxcore.filter;
import io.github.palexdev.mfxcore.base.beans.BiPredicateBean;
import io.github.palexdev.mfxcore.filter.base.AbstractFilter;
import io.github.palexdev.mfxcore.utils.converters.EnumStringConverter;
import io.github.palexdev.mfxcore.utils.fx.FXCollectors;
import io.github.palexdev.mfxlocalization.I18N;
import javafx.collections.ObservableList;
import javafx.util.StringConverter;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Extension of {@link AbstractFilter} for {@link Enum} fields.
*
* Offers the following default {@link BiPredicateBean}s:
* - "is": checks for enums equality
*
- "is not": checks for enums inequality
*
* This filter is special because to extract the enumerations of a given E enum, it's
* needed to also pass the type to the constructor. This is necessary for the {@link EnumStringConverter}.
*/
public class EnumFilter> extends AbstractFilter {
//================================================================================
// Properties
//================================================================================
private final Class enumType;
//================================================================================
// Constructors
//================================================================================
public EnumFilter(String name, Function extractor, Class enumType) {
this(name, extractor, enumType, new EnumStringConverter<>(enumType));
}
public EnumFilter(String name, Function extractor, Class enumType, StringConverter converter) {
super(name, extractor, converter);
this.enumType = enumType;
}
//================================================================================
// Getters
//================================================================================
public Class getEnumType() {
return enumType;
}
//================================================================================
// Overridden Methods
//================================================================================
@Override
protected ObservableList> defaultPredicates() {
return Stream.>of(
new BiPredicateBean<>(I18N.getOrDefault("filter.is"), Enum::equals),
new BiPredicateBean<>(I18N.getOrDefault("filter.isNot"), (anEnum, anEnum2) -> !anEnum.equals(anEnum2))
).collect(FXCollectors.toList());
}
@SafeVarargs
@Override
protected final EnumFilter extend(BiPredicateBean... predicateBeans) {
Collections.addAll(super.predicates, predicateBeans);
return this;
}
}