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

org.jetbrains.jet.util.slicedmap.SlicedMapImpl Maven / Gradle / Ivy

/*
 * Copyright 2010-2013 JetBrains s.r.o.
 *
 * 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.jetbrains.jet.util.slicedmap;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class SlicedMapImpl implements MutableSlicedMap {

    public static SlicedMapImpl create() {
        return new SlicedMapImpl(Maps., Object>newLinkedHashMap());
    }

    public static SlicedMapImpl create(Map, Object> map) {
        return new SlicedMapImpl(map);
    }

    public static SlicedMapImpl create(MapSupplier mapSupplier) {
        return new SlicedMapImpl(mapSupplier., Object>get());
    }
    
    private final Map, Object> map;
    private final Multimap, Object> collectiveSliceKeys = ArrayListMultimap.create();

    protected SlicedMapImpl(Map, Object> map) {
        this.map = map;
    }

    @Override
    public  void put(WritableSlice slice, K key, V value) {
        if (!slice.check(key, value)) {
            return;
        }

        SlicedMapKey slicedMapKey = slice.makeKey(key);
        RewritePolicy rewritePolicy = slice.getRewritePolicy();
        if (rewritePolicy.rewriteProcessingNeeded(key)) {
            if (map.containsKey(slicedMapKey)) {
                //noinspection unchecked
                if (!rewritePolicy.processRewrite(slice, key, (V) map.get(slicedMapKey), value)) {
                    return;
                }
            }
        }

        if (slice.isCollective()) {
            collectiveSliceKeys.put(slice, key);
        }

        map.put(slicedMapKey, value);
        slice.afterPut(this, key, value);
    }

    @Override
    public void clear() {
        map.clear();
    }

    @Override
    public  V get(ReadOnlySlice slice, K key) {
        SlicedMapKey slicedMapKey = slice.makeKey(key);
        //noinspection unchecked
        V value = (V) map.get(slicedMapKey);
        return slice.computeValue(this, key, value, value == null && !map.containsKey(slicedMapKey));
    }

    @Override
    @SuppressWarnings("unchecked")
    public  Collection getKeys(WritableSlice slice) {
        assert slice.isCollective() : "Keys are not collected for slice " + slice;
        return (Collection) collectiveSliceKeys.get(slice);
    }

    @Override
    public  V remove(RemovableSlice slice, K key) {
        //noinspection unchecked
        return (V) map.remove(slice.makeKey(key));
    }

    @NotNull
    @Override
    public Iterator, ?>> iterator() {
        //noinspection unchecked
        return (Iterator) map.entrySet().iterator();
    }

    @NotNull
    @Override
    public  ImmutableMap getSliceContents(@NotNull ReadOnlySlice slice) {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        for (Map.Entry, ?> entry : map.entrySet()) {
            if (entry.getKey().getSlice() == slice) {
                builder.put((K) entry.getKey().getKey(), (V) entry.getValue());
            }
        }
        return builder.build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy