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

io.fluxcapacitor.javaclient.modeling.SideEffectFreeEntity Maven / Gradle / Ivy

There is a newer version: 0.1072.0
Show newest version
/*
 * Copyright (c) Flux Capacitor IP B.V. or its affiliates. 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 io.fluxcapacitor.javaclient.modeling;

import io.fluxcapacitor.javaclient.common.Message;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import static java.util.stream.Collectors.toList;

@AllArgsConstructor
@Accessors(fluent = true)
public class SideEffectFreeEntity implements Entity {

    private final Supplier> loader;

    @Getter(lazy = true, value = AccessLevel.PRIVATE)
    private final Entity delegate = loader.get();

    @Getter(lazy = true)
    private final List> entities
            = delegate().entities().stream().map(e -> new SideEffectFreeEntity<>((Entity) e)).collect(toList());

    public SideEffectFreeEntity(Entity delegate) {
        this.loader = () -> delegate;
    }

    @Override
    public SideEffectFreeEntity apply(Message eventMessage) {
        var wasLoading = Entity.isLoading();
        try {
            Entity.loading.set(true);
            return new SideEffectFreeEntity<>(delegate().apply(eventMessage));
        } finally {
            Entity.loading.set(wasLoading);
        }
    }

    @Override
    public SideEffectFreeEntity update(UnaryOperator function) {
        var wasLoading = Entity.isLoading();
        try {
            Entity.loading.set(true);
            return new SideEffectFreeEntity<>(delegate().update(function));
        } finally {
            Entity.loading.set(wasLoading);
        }
    }

    @Override
    public Entity commit() {
        return this;
    }

    @Override
    public  Entity assertLegal(Object command) throws E {
        delegate().assertLegal(command);
        return this;
    }

    @Override
    public Entity parent() {
        return Optional.ofNullable(delegate().parent()).map(SideEffectFreeEntity::new).orElse(null);
    }

    @Override
    public Collection aliases() {
        return delegate().aliases();
    }

    @Override
    public SideEffectFreeEntity previous() {
        Entity previous = delegate().previous();
        return previous == null ? null : new SideEffectFreeEntity<>(previous);
    }

    @Override
    public Object id() {
        return delegate().id();
    }

    @Override
    public Class type() {
        return delegate().type();
    }

    @Override
    public Entity withType(Class type) {
        return delegate().withType(type);
    }

    @Override
    public T get() {
        return delegate().get();
    }

    @Override
    public String idProperty() {
        return delegate().idProperty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy