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

com.intellij.util.containers.ConcurrentWeakKeyWeakValueHashMap Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition util library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2015 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.
 */

/*
 * Created by IntelliJ IDEA.
 * User: Alexey
 * Date: 18.12.2006
 * Time: 20:18:31
 */
package com.intellij.util.containers;

import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * Concurrent map with weak keys and weak values.
 * Null keys are NOT allowed
 * Null values are NOT allowed
 * @deprecated Use {@link ContainerUtil#createConcurrentWeakKeyWeakValueMap()} instead
 */
class ConcurrentWeakKeyWeakValueHashMap extends ConcurrentWeakKeySoftValueHashMap {
  ConcurrentWeakKeyWeakValueHashMap(int initialCapacity,
                                    float loadFactor,
                                    int concurrencyLevel,
                                    @NotNull final TObjectHashingStrategy hashingStrategy) {
    super(initialCapacity, loadFactor, concurrencyLevel, hashingStrategy);
  }

  private static class WeakValue extends WeakReference implements ValueReference {
    @NotNull private volatile KeyReference myKeyReference; // can't make it final because of circular dependency of KeyReference to ValueReference
    private WeakValue(@NotNull V value, @NotNull ReferenceQueue queue) {
      super(value, queue);
    }

    // When referent is collected, equality should be identity-based (for the processQueues() remove this very same SoftValue)
    // otherwise it's just canonical equals on referents for replace(K,V,V) to work
    @Override
    public final boolean equals(final Object o) {
      if (this == o) return true;
      if (o == null) return false;

      V v = get();
      Object thatV = ((ValueReference)o).get();
      return v != null && thatV != null && v.equals(thatV);
    }

    @NotNull
    @Override
    public KeyReference getKeyReference() {
      return myKeyReference;
    }
 }

  @Override
  @NotNull
  protected KeyReference createKeyReference(@NotNull K k, @NotNull final V v) {
    final ValueReference valueReference = createValueReference(v, myValueQueue);
    WeakKey keyReference = new WeakKey(k, valueReference, myHashingStrategy, myKeyQueue);
    if (valueReference instanceof WeakValue) {
      ((WeakValue)valueReference).myKeyReference = keyReference;
    }
    return keyReference;
  }

  @Override
  @NotNull
  protected ValueReference createValueReference(@NotNull V value, @NotNull ReferenceQueue queue) {
    return new WeakValue(value, queue);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy