|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.java; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.Serializable; |
| 21 | +import java.io.UncheckedIOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.util.AbstractMap.SimpleImmutableEntry; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import java.util.stream.Stream; |
| 30 | + |
| 31 | +import com.diffplug.spotless.FormatterFunc; |
| 32 | +import com.diffplug.spotless.FormatterStep; |
| 33 | + |
| 34 | +public final class ImportOrderStep { |
| 35 | + private ImportOrderStep() {} |
| 36 | + |
| 37 | + public static FormatterStep createFromOrder(List<String> importOrder) { |
| 38 | + Objects.requireNonNull(importOrder); |
| 39 | + return importOrder instanceof Serializable |
| 40 | + ? createFromOrderImpl(importOrder) |
| 41 | + : createFromOrderImpl(new ArrayList<>(importOrder)); |
| 42 | + } |
| 43 | + |
| 44 | + public static FormatterStep createFromFile(File importsFile) { |
| 45 | + Objects.requireNonNull(importsFile); |
| 46 | + return createFromOrderImpl(getImportOrder(importsFile)); |
| 47 | + } |
| 48 | + |
| 49 | + private static FormatterStep createFromOrderImpl(List<String> importOrder) { |
| 50 | + return FormatterStep.createLazy("importOrder", |
| 51 | + () -> new State(importOrder), |
| 52 | + State::toFormatter); |
| 53 | + } |
| 54 | + |
| 55 | + private static List<String> getImportOrder(File importsFile) { |
| 56 | + try (Stream<String> lines = Files.lines(importsFile.toPath())) { |
| 57 | + return lines.filter(line -> !line.startsWith("#")) |
| 58 | + // parse 0=input |
| 59 | + .map(ImportOrderStep::splitIntoIndexAndName) |
| 60 | + .sorted(Map.Entry.comparingByKey()) |
| 61 | + .map(Map.Entry::getValue) |
| 62 | + .collect(Collectors.toCollection(ArrayList::new)); |
| 63 | + } catch (IOException e) { |
| 64 | + throw new UncheckedIOException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static Map.Entry<Integer, String> splitIntoIndexAndName(String line) { |
| 69 | + String[] pieces = line.split("="); |
| 70 | + Integer index = Integer.valueOf(pieces[0]); |
| 71 | + String name = pieces.length == 2 ? pieces[1] : ""; |
| 72 | + return new SimpleImmutableEntry<>(index, name); |
| 73 | + } |
| 74 | + |
| 75 | + private static final class State implements Serializable { |
| 76 | + private static final long serialVersionUID = 1L; |
| 77 | + |
| 78 | + private final List<String> importOrder; |
| 79 | + |
| 80 | + State(List<String> importOrder) { |
| 81 | + this.importOrder = importOrder; |
| 82 | + } |
| 83 | + |
| 84 | + FormatterFunc toFormatter() { |
| 85 | + return raw -> new ImportSorter(importOrder).format(raw); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments