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

com.intellij.slicer.DuplicateMap Maven / Gradle / Ivy

Go to download

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

The newest version!
/*
 * Copyright 2000-2009 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 com.intellij.slicer;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.TextRange;
import com.intellij.usageView.UsageInfo;
import gnu.trove.THashMap;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

/**
* @author cdr
*/ // rehash map on each PSI modification since SmartPsiPointer's hashCode() and equals() are changed
public class DuplicateMap {
  private static final TObjectHashingStrategy USAGE_INFO_EQUALITY = new TObjectHashingStrategy() {
    @Override
    public int computeHashCode(SliceUsage object) {
      UsageInfo info = object.getUsageInfo();
      TextRange range = info.getRangeInElement();
      return range == null ? 0 : range.hashCode();
    }

    @Override
    public boolean equals(SliceUsage o1, SliceUsage o2) {
      return o1.getUsageInfo().equals(o2.getUsageInfo());
    }
  };
  private final Map myDuplicates = new THashMap(USAGE_INFO_EQUALITY);

  public SliceNode putNodeCheckDupe(@NotNull final SliceNode node) {
    return ApplicationManager.getApplication().runReadAction(new Computable() {
      @Override
      public SliceNode compute() {
        SliceUsage usage = node.getValue();
        SliceNode eq = myDuplicates.get(usage);
        if (eq == null) {
          myDuplicates.put(usage, node);
        }
        return eq;
      }
    });
  }

  public void clear() {
    myDuplicates.clear();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy