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

com.google.cloud.tools.opensource.classpath.SymbolReferenceMaps Maven / Gradle / Ivy

There is a newer version: 1.5.13
Show newest version
/*
 * Copyright 2019 Google LLC.
 *
 * 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.google.cloud.tools.opensource.classpath;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSetMultimap;
import java.util.Objects;

/**
 * A set of symbol references. Symbol references are stored as maps from {@link ClassFile} to {@link
 * ClassSymbol}s, {@link MethodSymbol}s, and {@link FieldSymbol}s.
 */
class SymbolReferenceMaps {
  private final ImmutableSetMultimap classToClassSymbols;
  private final ImmutableSetMultimap classToMethodSymbols;
  private final ImmutableSetMultimap classToFieldSymbols;

  ImmutableSetMultimap getClassToClassSymbols() {
    return classToClassSymbols;
  }

  ImmutableSetMultimap getClassToMethodSymbols() {
    return classToMethodSymbols;
  }

  ImmutableSetMultimap getClassToFieldSymbols() {
    return classToFieldSymbols;
  }

  @VisibleForTesting
  SymbolReferenceMaps(
      ImmutableSetMultimap classToClassSymbols,
      ImmutableSetMultimap classToMethodSymbols,
      ImmutableSetMultimap classToFieldSymbols) {
    this.classToClassSymbols = checkNotNull(classToClassSymbols);
    this.classToMethodSymbols = checkNotNull(classToMethodSymbols);
    this.classToFieldSymbols = checkNotNull(classToFieldSymbols);
  }

  static class Builder {
    private final ImmutableSetMultimap.Builder classToClassSymbols;
    private final ImmutableSetMultimap.Builder classToMethodSymbols;
    private final ImmutableSetMultimap.Builder classToFieldSymbols;

    Builder() {
      classToClassSymbols = ImmutableSetMultimap.builder();
      classToMethodSymbols = ImmutableSetMultimap.builder();
      classToFieldSymbols = ImmutableSetMultimap.builder();
    }

    Builder addClassReference(ClassFile source, ClassSymbol symbol) {
      classToClassSymbols.put(source, symbol);
      return this;
    }

    Builder addMethodReference(ClassFile source, MethodSymbol symbol) {
      classToMethodSymbols.put(source, symbol);
      return this;
    }

    Builder addFieldReference(ClassFile source, FieldSymbol symbol) {
      classToFieldSymbols.put(source, symbol);
      return this;
    }

    SymbolReferenceMaps build() {
      return new SymbolReferenceMaps(
          classToClassSymbols.build(), classToMethodSymbols.build(), classToFieldSymbols.build());
    }

    Builder addAll(Builder other) {
      classToClassSymbols.putAll(other.classToClassSymbols.build());
      classToMethodSymbols.putAll(other.classToMethodSymbols.build());
      classToFieldSymbols.putAll(other.classToFieldSymbols.build());
      return this;
    }
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (other == null || getClass() != other.getClass()) {
      return false;
    }
    SymbolReferenceMaps that = (SymbolReferenceMaps) other;
    return classToClassSymbols.equals(that.classToClassSymbols)
        && classToMethodSymbols.equals(that.classToMethodSymbols)
        && classToFieldSymbols.equals(that.classToFieldSymbols);
  }

  @Override
  public int hashCode() {
    return Objects.hash(classToClassSymbols, classToMethodSymbols, classToFieldSymbols);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy