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

net.revelc.code.impsort.Result Maven / Gradle / Ivy

Go to download

Sort your imps! A Maven plugin to sort the import statements in your Java source files.

The newest version!
/*
 * 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
 *
 *     https://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 net.revelc.code.impsort;

import com.github.javaparser.Position;
import com.github.javaparser.Problem;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Result {

  private Boolean isSorted;

  private final Path path;
  private final Charset sourceEncoding;
  private final String originalSection;
  private final String newSection;
  private final Collection allImports;
  private final List fileLines;
  private final int start;
  private final int stop;
  private final LineEnding lineEnding;
  private List problems;
  private List reportableProblems;

  public static final Result EMPTY_FILE =
      new Result(null, null, null, 0, 0, "", "", Collections.emptyList(), null, null, null);

  Result(Path path, Charset sourceEncoding, List fileLines, int start, int stop,
      String originalSection, String newSection, Collection allImports,
      LineEnding lineEnding, List problems, List reportableProblems) {
    this.path = path;
    this.sourceEncoding = sourceEncoding;
    this.originalSection = originalSection;
    this.newSection = newSection;
    this.allImports = allImports;
    this.fileLines = fileLines;
    this.start = start;
    this.stop = stop;
    this.lineEnding = lineEnding;
    this.problems = problems;
    this.reportableProblems = reportableProblems;
  }

  public boolean isSorted() {
    if (isSorted == null) {
      isSorted = originalSection.contentEquals(newSection);
    }
    return isSorted;
  }

  public Collection getImports() {
    return Collections.unmodifiableCollection(allImports);
  }

  public void saveBackup(Path destination) throws IOException {
    Files.copy(path, destination, StandardCopyOption.REPLACE_EXISTING);
  }

  public byte[] saveSorted(Path destination) throws IOException {
    if (isSorted()) {
      if (!Files.isSameFile(path, destination)) {
        saveBackup(destination);
      }
      return null;
    }
    List beforeImports = fileLines.subList(0, start);
    List importLines = Arrays.asList(newSection.split(lineEnding.getChars()));
    List afterImports = fileLines.subList(stop, fileLines.size());
    List allLines =
        new ArrayList<>(beforeImports.size() + importLines.size() + afterImports.size() + 1);
    allLines.addAll(beforeImports);
    allLines.addAll(importLines);
    if (afterImports.size() > 0) {
      allLines.add(""); // restore blank line lost by split
    }
    allLines.addAll(afterImports);
    return writeLines(destination, allLines, sourceEncoding);
  }

  private byte[] writeLines(Path destination, List lines, Charset sourceEncoding)
      throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (Writer writer = new OutputStreamWriter(baos, sourceEncoding.newEncoder())) {
      for (String line : lines) {
        writer.write(line);
        writer.write(lineEnding.getChars());
      }
    }
    byte[] buf = baos.toByteArray();
    Files.write(destination, buf);
    return buf;
  }

  public Position getFirstLineContaining(String string) {
    for (int i = 0; i < fileLines.size(); i++) {
      if (fileLines.get(i).contains(string)) {
        return new Position(i, 0);
      }
    }

    throw new IllegalStateException("Can't find '" + string + "'!");
  }

  public List getProblems() {
    return Collections.unmodifiableList(problems);
  }

  public List getReportableProblems() {
    return Collections.unmodifiableList(reportableProblems);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy