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

org.pkl.thirdparty.paguro.collections.BaseMap Maven / Gradle / Ivy

Go to download

Shaded fat Jar for pkl-config-java, a Java config library based on the Pkl config language.

There is a newer version: 0.27.1
Show newest version
// Copyright 2017 PlanBase Inc. & Glen Peterson
//
// 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 org.pkl.thirdparty.paguro.collections;

import java.util.Map;

import org.pkl.thirdparty.jetbrains.annotations.NotNull;
import org.pkl.thirdparty.jetbrains.annotations.Nullable;
import org.pkl.thirdparty.paguro.oneOf.Option;

/**
 Adds copy-on-write, "fluent interface" methods to {@link UnmodMap}.
 Lowest common ancestor of {@link BaseUnsortedMap}, and {@link ImSortedMap}.
 */
public interface BaseMap extends UnmodMap {
    /** Returns an option of the key/value pair associated with this key */
    @NotNull Option> entry(K key);

    /** Returns a new map with the given key/value added */
    @NotNull BaseMap assoc(K key, V val);

    /** Returns a new map with an immutable copy of the given entry added */
    default @NotNull BaseMap assoc(@NotNull Map.Entry entry) {
        return assoc(entry.getKey(), entry.getValue());
    }

    /** Returns a new map with the given key/value removed */
    @NotNull BaseMap without(K key);

    /**
     Returns a view of the mappings contained in this map.  The set should actually contain
     UnmodMap.Entry items, but that return signature is illegal in Java, so you'll just have to
     remember.
     */
    @Override @NotNull BaseSet> entrySet();
//        return map(e -> (Map.Entry) e)
//                .toImSet();
//    }

    /** Returns a view of the keys contained in this map. */
    @NotNull
    @Override BaseSet keySet();

    @SuppressWarnings("unchecked")
    @Override default boolean containsKey(Object key) { return entry((K) key).isSome(); }

    @SuppressWarnings("unchecked")
    @Override
    default @Nullable V get(Object key) {
        Option> entry = entry((K) key);
        return entry.isSome() ? entry.get().getValue() : null;
    }

    default V getOrElse(K key, V notFound) {
        Option> entry = entry(key);
        return entry.isSome() ? entry.get().getValue() : notFound;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy