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

org.jhotdraw8.draw.key.BoundingBoxMapAccessor Maven / Gradle / Ivy

The newest version!
/*
 * @(#)BoundingBoxMapAccessor.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */
package org.jhotdraw8.draw.key;

import javafx.geometry.BoundingBox;
import org.jhotdraw8.fxcollection.typesafekey.Key;
import org.jhotdraw8.fxcollection.typesafekey.MapAccessor;
import org.jhotdraw8.icollection.immutable.ImmutableMap;
import org.jspecify.annotations.Nullable;

import java.util.Map;

/**
 * BoundingBoxMapAccessor.
 *
 * @author Werner Randelshofer
 */
public class BoundingBoxMapAccessor extends AbstractMapAccessor {


    private final MapAccessor xKey;
    private final MapAccessor yKey;
    private final MapAccessor widthKey;
    private final MapAccessor heightKey;

    /**
     * Creates a new instance with the specified name.
     *
     * @param name      the name of the accessor
     * @param xKey      the key for the x coordinate of the rectangle
     * @param yKey      the key for the y coordinate of the rectangle
     * @param widthKey  the key for the width of the rectangle
     * @param heightKey the key for the height of the rectangle
     */
    public BoundingBoxMapAccessor(String name, MapAccessor xKey, MapAccessor yKey, MapAccessor widthKey, MapAccessor heightKey) {
        super(name, BoundingBox.class, new MapAccessor[]{xKey, yKey, widthKey, heightKey}, new BoundingBox(xKey.getDefaultValue(), yKey.getDefaultValue(), widthKey.getDefaultValue(), heightKey.getDefaultValue()));

        this.xKey = xKey;
        this.yKey = yKey;
        this.widthKey = widthKey;
        this.heightKey = heightKey;
    }

    @Override
    public BoundingBox get(Map, Object> a) {
        return new BoundingBox(xKey.get(a), yKey.get(a), widthKey.get(a), heightKey.get(a));
    }


    @Override
    public BoundingBox put(Map, Object> a, @Nullable BoundingBox value) {
        BoundingBox oldValue = get(a);
        if (value == null) {
            remove(a);
        } else {
            xKey.put(a, value.getMinX());
            yKey.put(a, value.getMinY());
            widthKey.put(a, value.getWidth());
            heightKey.put(a, value.getHeight());
        }
        return oldValue;
    }

    @Override
    public BoundingBox remove(Map, Object> a) {
        BoundingBox oldValue = get(a);
        xKey.remove(a);
        yKey.remove(a);
        widthKey.remove(a);
        heightKey.remove(a);
        return oldValue;
    }

    @Override
    public ImmutableMap, Object> put(ImmutableMap, Object> a, @Nullable BoundingBox value) {
        if (value == null) {
            return remove(a);
        } else {
            a = xKey.put(a, value.getMinX());
            a = yKey.put(a, value.getMinY());
            a = widthKey.put(a, value.getWidth());
            return heightKey.put(a, value.getHeight());
        }
    }

    @Override
    public ImmutableMap, Object> remove(ImmutableMap, Object> a) {
        a = xKey.remove(a);
        a = yKey.remove(a);
        a = widthKey.remove(a);
        return heightKey.remove(a);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy