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

com.hazelcast.function.FunctionsImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
 *
 * 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.hazelcast.function;

import com.hazelcast.internal.serialization.SerializableByConvention;
import com.hazelcast.security.impl.function.SecuredFunction;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serial;
import java.io.Serializable;
import java.security.Permission;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

class FunctionsImpl {
    // utility class
    private FunctionsImpl() {
    }

    @SerializableByConvention
    static class ComposedSecuredFunction
            implements SecuredFunction, Serializable {
        protected final F before;
        protected final G after;

        ComposedSecuredFunction(F before, G after) {
            this.before = Objects.requireNonNull(before, "before");
            this.after = Objects.requireNonNull(after, "after");
        }

        @Nullable
        @Override
        public List permissions() {
            List beforeP = before.permissions();
            List afterP = after.permissions();
            return sumPermissions(afterP, beforeP);
        }

        @Nullable
        private static List sumPermissions(@Nullable List afterP, @Nullable List beforeP) {
            if (afterP == null || afterP.isEmpty()) {
                return beforeP;
            } else if (beforeP == null || beforeP.isEmpty()) {
                return afterP;
            } else {
                List permissions = new ArrayList<>(afterP.size() + beforeP.size());
                permissions.addAll(beforeP);
                permissions.addAll(afterP);
                return permissions;
            }
        }
    }

    @SerializableByConvention
    static final class ComposedFunctionEx
            extends ComposedSecuredFunction, FunctionEx>
            implements FunctionEx {

        @Serial
        private static final long serialVersionUID = 1L;

        ComposedFunctionEx(@Nonnull FunctionEx before,
                                  @Nonnull FunctionEx after) {
            super(before, after);
        }

        @Override
        public R applyEx(V v) throws Exception {
            return after.applyEx(before.applyEx(v));
        }
    }

    @SerializableByConvention
    static final class ComposedBiFunctionEx
            extends ComposedSecuredFunction, FunctionEx>
            implements BiFunctionEx {

        @Serial
        private static final long serialVersionUID = 1L;

        ComposedBiFunctionEx(@Nonnull BiFunctionEx before,
                                  @Nonnull FunctionEx after) {
            super(before, after);
        }

        @Override
        public R applyEx(U t, V u) throws Exception {
            return after.applyEx(before.applyEx(t, u));
        }
    }

    @SerializableByConvention
    static final class ComposedConsumerEx
            extends ComposedSecuredFunction, ConsumerEx>
            implements ConsumerEx {

        @Serial
        private static final long serialVersionUID = 1L;

        ComposedConsumerEx(@Nonnull ConsumerEx before,
                                    @Nonnull ConsumerEx after) {
            super(before, after);
        }

        @Override
        public void acceptEx(T t) throws Exception {
            before.acceptEx(t);
            after.acceptEx(t);
        }
    }

    @SerializableByConvention
    static final class ComposedSupplierEx
            extends ComposedSecuredFunction, FunctionEx>
            implements SupplierEx {

        ComposedSupplierEx(SupplierEx before, FunctionEx after) {
            super(before, after);
        }

        @Override
        public R getEx() throws Exception {
            return after.applyEx(before.getEx());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy