All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.palexdev.mfxcore.utils.fx.PropUtils Maven / Gradle / Ivy

There is a newer version: 11.26.0
Show newest version
/*
 * 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.utils.fx;

import io.github.palexdev.mfxcore.base.TriFunction;
import io.github.palexdev.mfxcore.base.properties.functional.*;
import io.github.palexdev.mfxcore.utils.NumberUtils;
import javafx.beans.property.*;

import java.util.Comparator;
import java.util.function.*;

/**
 * Convenience methods related to properties.
 */
public class PropUtils {

	private PropUtils() {
	}

	public static DoubleProperty mappedDoubleProperty(Function mapper) {
		return new SimpleDoubleProperty() {
			@Override
			public void set(double newValue) {
				super.set(mapper.apply(newValue));
			}
		};
	}

	public static FloatProperty mappedFloatProperty(Function valMapper) {
		return new SimpleFloatProperty() {
			@Override
			public void set(float newValue) {
				super.set(valMapper.apply(newValue));
			}
		};
	}

	public static IntegerProperty mappedIntProperty(Function valMapper) {
		return new SimpleIntegerProperty() {
			@Override
			public void set(int newValue) {
				super.set(valMapper.apply(newValue));
			}
		};
	}

	public static LongProperty mappedLongProperty(Function valMapper) {
		return new SimpleLongProperty() {
			@Override
			public void set(long newValue) {
				super.set(valMapper.apply(newValue));
			}
		};
	}

	public static StringProperty mappedStringProperty(Function valMapper) {
		return new SimpleStringProperty() {
			@Override
			public void set(String newValue) {
				super.set(valMapper.apply(newValue));
			}
		};
	}

	public static  ObjectProperty mappedObjectProperty(Function valMapper) {
		return new SimpleObjectProperty<>() {
			@Override
			public void set(T newValue) {
				super.set(valMapper.apply(newValue));
			}
		};
	}

	public static DoubleProperty clampedDoubleProperty(Supplier min, Supplier max) {
		return mappedDoubleProperty(val -> NumberUtils.clamp(val, min.get(), max.get()));
	}

	public static FloatProperty clampedFloatProperty(Supplier min, Supplier max) {
		return mappedFloatProperty(val -> NumberUtils.clamp(val, min.get(), max.get()));
	}

	public static IntegerProperty clampedIntProperty(Supplier min, Supplier max) {
		return mappedIntProperty(val -> NumberUtils.clamp(val, min.get(), max.get()));
	}

	public static LongProperty clampedLongProperty(Supplier min, Supplier max) {
		return mappedLongProperty(val -> NumberUtils.clamp(val, min.get(), max.get()));
	}

	public static  ComparatorProperty compare(Comparator comparator) {
		return new ComparatorProperty<>(comparator);
	}

	public static  ConsumerProperty consume(Consumer consumer) {
		return new ConsumerProperty<>(consumer);
	}

	public static  FunctionProperty function(Function function) {
		return new FunctionProperty<>(function);
	}

	public static  PredicateProperty predicate(Predicate predicate) {
		return new PredicateProperty<>(predicate);
	}

	public static  SupplierProperty supply(Supplier supplier) {
		return new SupplierProperty<>(supplier);
	}

	public static  BiConsumerProperty biConsume(BiConsumer biConsumer) {
		return new BiConsumerProperty<>(biConsumer);
	}

	public static  BiFunctionProperty biFunction(BiFunction biFunction) {
		return new BiFunctionProperty<>(biFunction);
	}

	public static  BiPredicateProperty biPredicate(BiPredicate biPredicate) {
		return new BiPredicateProperty<>(biPredicate);
	}

	public static  TriFunctionProperty triFunction(TriFunction function) {
		return new TriFunctionProperty<>(function);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy