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

com.caoccao.javet.interfaces.IEnumBitset Maven / Gradle / Ivy

/*
 * Copyright (c) 2021-2024. caoccao.com Sam Cao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.caoccao.javet.interfaces;

import java.util.EnumSet;
import java.util.Objects;

/**
 * The interface Enum bitset.
 *
 * @since 1.0.3
 */
public interface IEnumBitset {
    /**
     * Gets enum set.
     *
     * @param          the type parameter
     * @param bitsetValue the bitset value
     * @param enumClass   the enum class
     * @return the enum set
     * @since 1.0.3
     */
    static  & IEnumBitset> EnumSet getEnumSet(int bitsetValue, Class enumClass) {
        return getEnumSet(bitsetValue, enumClass, null);
    }

    /**
     * Gets enum set.
     *
     * @param          the type parameter
     * @param bitsetValue the bitset value
     * @param enumClass   the enum class
     * @param defaultEnum the default enum
     * @return the enum set
     * @since 1.0.3
     */
    static  & IEnumBitset> EnumSet getEnumSet(int bitsetValue, Class enumClass, E defaultEnum) {
        EnumSet enumSet = EnumSet.noneOf(enumClass);
        for (E e : EnumSet.allOf(enumClass)) {
            if ((bitsetValue & e.getValue()) > 0) {
                enumSet.add(e);
            }
        }
        if (enumSet.isEmpty() && defaultEnum != null) {
            enumSet.add(defaultEnum);
        }
        return enumSet;
    }

    /**
     * Gets value.
     *
     * @param      the type parameter
     * @param enumSet the enum set
     * @return the value
     * @since 1.0.3
     */
    static  & IEnumBitset> int getValue(EnumSet enumSet) {
        int value = 0;
        for (E e : Objects.requireNonNull(enumSet)) {
            value |= e.getValue();
        }
        return value;
    }

    /**
     * Gets value.
     *
     * @return the value
     * @since 1.0.3
     */
    int getValue();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy