|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.io.file; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.FileVisitResult; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.attribute.BasicFileAttributes; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Comparator; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | + |
| 30 | +import org.apache.commons.io.file.Counters.PathCounters; |
| 31 | + |
| 32 | +/** |
| 33 | + * Accumulates normalized paths during visitation. |
| 34 | + * <p> |
| 35 | + * Use with care on large file trees as each visited Path element is remembered. |
| 36 | + * </p> |
| 37 | + * |
| 38 | + * @since 2.7 |
| 39 | + */ |
| 40 | +public class AccumulatorPathVisitor extends CountingPathVisitor { |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new instance configured with a BigInteger {@link PathCounters}. |
| 44 | + * |
| 45 | + * @return a new instance configured with a BigInteger {@link PathCounters}. |
| 46 | + */ |
| 47 | + public static AccumulatorPathVisitor withBigIntegerCounters() { |
| 48 | + return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new instance configured with a long {@link PathCounters}. |
| 53 | + * |
| 54 | + * @return a new instance configured with a long {@link PathCounters}. |
| 55 | + */ |
| 56 | + public static AccumulatorPathVisitor withLongCounters() { |
| 57 | + return new AccumulatorPathVisitor(Counters.longPathCounters()); |
| 58 | + } |
| 59 | + |
| 60 | + private final List<Path> dirList = new ArrayList<>(); |
| 61 | + |
| 62 | + private final List<Path> fileList = new ArrayList<>(); |
| 63 | + |
| 64 | + /** |
| 65 | + * Constructs a new instance. |
| 66 | + * |
| 67 | + * @param pathCounter How to count path visits. |
| 68 | + */ |
| 69 | + public AccumulatorPathVisitor(PathCounters pathCounter) { |
| 70 | + super(pathCounter); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean equals(Object obj) { |
| 75 | + if (this == obj) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + if (!super.equals(obj)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (!(obj instanceof AccumulatorPathVisitor)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + AccumulatorPathVisitor other = (AccumulatorPathVisitor) obj; |
| 85 | + return Objects.equals(dirList, other.dirList) && Objects.equals(fileList, other.fileList); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Gets the list of visited directories. |
| 90 | + * |
| 91 | + * @return the list of visited directories. |
| 92 | + */ |
| 93 | + public List<Path> getDirList() { |
| 94 | + return dirList; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets the list of visited files. |
| 99 | + * |
| 100 | + * @return the list of visited files. |
| 101 | + */ |
| 102 | + public List<Path> getFileList() { |
| 103 | + return fileList; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public int hashCode() { |
| 108 | + final int prime = 31; |
| 109 | + int result = super.hashCode(); |
| 110 | + result = prime * result + Objects.hash(dirList, fileList); |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Relativizes each directory path with {@link Path#relativize(Path)} against the given {@code parent}, optionally |
| 116 | + * sorting the result. |
| 117 | + * |
| 118 | + * @param parent A parent path |
| 119 | + * @param sort Whether to sort |
| 120 | + * @param comparator How to sort, null uses default sorting. |
| 121 | + * @return A new list |
| 122 | + */ |
| 123 | + public List<Path> relativizeDirectories(final Path parent, boolean sort, Comparator<? super Path> comparator) { |
| 124 | + return PathUtils.relativize(getDirList(), parent, sort, comparator); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Relativizes each file path with {@link Path#relativize(Path)} against the given {@code parent}, optionally |
| 129 | + * sorting the result. |
| 130 | + * |
| 131 | + * @param parent A parent path |
| 132 | + * @param sort Whether to sort |
| 133 | + * @param comparator How to sort, null uses default sorting. |
| 134 | + * @return A new list |
| 135 | + */ |
| 136 | + public List<Path> relativizeFiles(final Path parent, boolean sort, Comparator<? super Path> comparator) { |
| 137 | + return PathUtils.relativize(getFileList(), parent, sort, comparator); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { |
| 142 | + ((Files.isDirectory(file)) ? dirList : fileList).add(file.normalize()); |
| 143 | + return super.visitFile(file, attributes); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments