io.github.palexdev.mfxresources.utils.EnumUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of materialfx-all Show documentation
Show all versions of materialfx-all Show documentation
Material Design/Modern components for JavaFX, now packed as a single Jar
The 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.mfxresources.utils;
import java.util.Random;
/**
* Utility class which provides convenience methods for enumerators
*/
public class EnumUtils {
public static final Random rng = new Random(System.currentTimeMillis());
private EnumUtils() {
}
/**
* Checks if the given enumerator (as a class) contains the given String,
* same as {@link Enum#valueOf(Class, String)} but case-insensitive.
*
* @param clazz the Class object of the enum class from which to return a constant
* @param name the name of the constant to return
* @return the enum constant of the specified enum class with the specified name
*/
public static > E valueOfIgnoreCase(Class clazz, String name) {
E enumeration = null;
for (E e : clazz.getEnumConstants()) {
if (e.name().equalsIgnoreCase(name)) {
enumeration = e;
}
}
if (enumeration == null) {
throw new IllegalArgumentException("No enum constant " + clazz.getCanonicalName() + "." + name);
}
return enumeration;
}
/**
* Given an enum class and an enumeration of the given enum returns the next enumeration.
*
* @param clazz the enum class
* @param val the enum constant from which get the next constant
* @param the enum type
* @return the constant next to the given val
*/
public static > E next(Class clazz, E val) {
E[] values = clazz.getEnumConstants();
return values[(val.ordinal() + 1) % values.length];
}
/**
* Given an enum class and an enumeration of the given enum returns the previous enumeration.
*
* @param clazz the enum class
* @param val the enum constant from which get the previous constant
* @param the enum type
* @return the constant before the given val
*/
public static > E previous(Class clazz, E val) {
E[] values = clazz.getEnumConstants();
return values[(val.ordinal() - 1 + values.length) % values.length];
}
/**
* Given an enumerator (as a class) retrieves a random constant
* using {@link Class#getEnumConstants()}.
*/
public static > E randomEnum(Class clazz) {
E[] vals = clazz.getEnumConstants();
return vals[rng.nextInt(vals.length)];
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy