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

org.basinmc.plunger.mapping.csv.CSVClassNameMappingParser Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
/*
 * Copyright 2018 Johannes Donath 
 * and other copyright owners as documented in the project's IP log.
 *
 * 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.basinmc.plunger.mapping.csv;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.basinmc.plunger.mapping.ClassNameMapping;

/**
 * Provides a parser for CSV backed class name mappings.
 *
 * @author Johannes Donath
 */
public final class CSVClassNameMappingParser extends AbstractCSVMappingParser {

  private final String originalNameColumn;
  private final String targetNameColumn;

  private CSVClassNameMappingParser(
      @NonNull CSVFormat format,
      @NonNull String originalNameColumn,
      @NonNull String targetNameColumn) {
    super(format);
    this.originalNameColumn = originalNameColumn;
    this.targetNameColumn = targetNameColumn;
  }

  /**
   * Creates a new empty factory for the class name parser.
   *
   * @return an empty factory.
   */
  @NonNull
  public static Builder builder() {
    return new Builder();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected ClassNameMapping doParse(@NonNull CSVParser parser) throws IOException {
    ClassNameMappingImpl mapping = new ClassNameMappingImpl();

    parser.getRecords().forEach(
        (r) -> mapping.nameMap.put(r.get(this.originalNameColumn), r.get(this.targetNameColumn)));

    return mapping;
  }

  /**
   * Provides a factory for CSV backed class name mapping parsers.
   */
  public static final class Builder extends AbstractCSVMappingParser.Builder {

    private Builder() {
    }

    /**
     * Constructs a new CSV mapping parser for class name mappings using the configuration of this
     * builder.
     *
     * @param originalNameColumn the name of the column in which the original class name is stored.
     * @param targetNameColumn the name of the column in which the target class name is stored.
     */
    @NonNull
    public CSVClassNameMappingParser build(
        @NonNull String originalNameColumn,
        @NonNull String targetNameColumn) {
      return new CSVClassNameMappingParser(this.format, originalNameColumn, targetNameColumn);
    }

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public Builder withFormat(@NonNull CSVFormat format) {
      super.withFormat(format);
      return this;
    }
  }

  /**
   * Provides a map based class name mapping which is to be filled based on a CSV file.
   */
  private static class ClassNameMappingImpl implements ClassNameMapping {

    private final Map nameMap = new HashMap<>();

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public Optional getClassName(@NonNull String original) {
      return Optional.ofNullable(this.nameMap.get(original));
    }

    /**
     * {@inheritDoc}
     */
    @NonNull
    @Override
    public ClassNameMappingImpl invert() {
      ClassNameMappingImpl mapping = new ClassNameMappingImpl();
      mapping.nameMap.putAll(
          this.nameMap.entrySet().stream()
              .collect(Collectors.toMap(Entry::getValue, Entry::getKey))
      );
      return mapping;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy