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

io.deephaven.engine.table.impl.BaseGridAttributes Maven / Gradle / Ivy

There is a newer version: 0.36.1
Show newest version
package io.deephaven.engine.table.impl;

import io.deephaven.api.util.ConcurrentMethod;
import io.deephaven.engine.table.GridAttributes;
import io.deephaven.engine.table.Table;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Default implementation for {@link GridAttributes}.
 */
public abstract class BaseGridAttributes, IMPL_TYPE extends BaseGridAttributes>
        extends LiveAttributeMap
        implements GridAttributes {

    /**
     * @param initialAttributes The attributes map to use until mutated, or else {@code null} to allocate a new one
     */
    protected BaseGridAttributes(@Nullable final Map initialAttributes) {
        super(initialAttributes);
    }

    @Override
    @ConcurrentMethod
    public final IFACE_TYPE restrictSortTo(@NotNull final String... allowedSortingColumns) {
        checkAvailableColumns(Arrays.asList(allowedSortingColumns));
        return withAttributes(Map.of(
                GridAttributes.SORTABLE_COLUMNS_ATTRIBUTE, String.join(",", allowedSortingColumns)));
    }

    public Set getSortableColumns() {
        final String sortingRestrictions = (String) getAttribute(SORTABLE_COLUMNS_ATTRIBUTE);
        if (sortingRestrictions == null) {
            return null;
        }
        return Stream.of(sortingRestrictions.split(",")).collect(Collectors.toCollection(LinkedHashSet::new));
    }

    protected void setSortableColumns(@NotNull final Collection allowedSortingColumns) {
        checkAvailableColumns(allowedSortingColumns);
        setAttribute(SORTABLE_COLUMNS_ATTRIBUTE, String.join(",", allowedSortingColumns));
    }

    @Override
    @ConcurrentMethod
    public final IFACE_TYPE clearSortingRestrictions() {
        return withoutAttributes(List.of(SORTABLE_COLUMNS_ATTRIBUTE));
    }

    @Override
    @ConcurrentMethod
    public final IFACE_TYPE withDescription(@NotNull final String description) {
        return withAttributes(Map.of(DESCRIPTION_ATTRIBUTE, description));
    }

    @Override
    @ConcurrentMethod
    public final IFACE_TYPE withColumnDescription(@NotNull final String column, @NotNull final String description) {
        return withColumnDescriptions(Collections.singletonMap(column, description));
    }

    @Override
    @ConcurrentMethod
    public final IFACE_TYPE withColumnDescriptions(@NotNull final Map descriptions) {
        checkAvailableColumns(descriptions.keySet());
        // noinspection unchecked
        final Map existingDescriptions =
                (Map) getAttribute(Table.COLUMN_DESCRIPTIONS_ATTRIBUTE);
        if (existingDescriptions != null && existingDescriptions.entrySet().containsAll(descriptions.entrySet())) {
            return prepareReturnThis();
        }
        final Map resultDescriptions =
                existingDescriptions == null ? new HashMap<>(descriptions.size()) : new HashMap<>(existingDescriptions);
        resultDescriptions.putAll(descriptions);
        return withAttributes(Map.of(Table.COLUMN_DESCRIPTIONS_ATTRIBUTE, resultDescriptions));
    }

    protected void setColumnDescriptions(@NotNull final Map descriptions) {
        checkAvailableColumns(descriptions.keySet());
        setAttribute(Table.COLUMN_DESCRIPTIONS_ATTRIBUTE, descriptions);
    }

    @Override
    @ConcurrentMethod
    public final IFACE_TYPE setLayoutHints(@NotNull final String hints) {
        return withAttributes(Map.of(Table.LAYOUT_HINTS_ATTRIBUTE, hints));
    }

    /**
     * Check this grid to ensure that all {@code columns} are present.
     *
     * @param columns The column names to check
     * @throws NoSuchColumnException If any columns were missing
     */
    protected abstract void checkAvailableColumns(@NotNull Collection columns);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy