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

android.arch.core.internal.FastSafeIterableMap Maven / Gradle / Ivy

There is a newer version: 8.2.0-rc.4
Show newest version
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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 android.arch.core.internal;

import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;

import java.util.HashMap;
import java.util.Map;

// TODO: Auto-generated Javadoc
/**
 * Poor's man LinkedHashMap, which supports modifications during iterations.
 * Takes more memory that SafeIterableMap
 * It is NOT thread safe.
 *
 * @param  Key type
 * @param  Value type
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class FastSafeIterableMap extends SafeIterableMap {

    /** The m hash map. */
    private HashMap> mHashMap = new HashMap<>();

    /* (non-Javadoc)
     * @see android.arch.core.internal.SafeIterableMap#get(java.lang.Object)
     */
    @Override
    protected Entry get(K k) {
        return mHashMap.get(k);
    }

    /* (non-Javadoc)
     * @see android.arch.core.internal.SafeIterableMap#putIfAbsent(java.lang.Object, java.lang.Object)
     */
    @Override
    public V putIfAbsent(@NonNull K key, @NonNull V v) {
        Entry current = get(key);
        if (current != null) {
            return current.mValue;
        }
        mHashMap.put(key, put(key, v));
        return null;
    }

    /* (non-Javadoc)
     * @see android.arch.core.internal.SafeIterableMap#remove(java.lang.Object)
     */
    @Override
    public V remove(@NonNull K key) {
        V removed = super.remove(key);
        mHashMap.remove(key);
        return removed;
    }

    /**
     * Returns {@code true} if this map contains a mapping for the specified
     * key.
     *
     * @param key the key
     * @return true, if successful
     */
    public boolean contains(K key) {
        return mHashMap.containsKey(key);
    }

    /**
     * Return an entry added to prior to an entry associated with the given key.
     *
     * @param k the key
     * @return the map. entry
     */
    public Map.Entry ceil(K k) {
        if (contains(k)) {
            return mHashMap.get(k).mPrevious;
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy