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.logging.log4j.core.appender.rolling.action;
19
20 import java.io.IOException;
21 import java.nio.file.FileVisitResult;
22 import java.nio.file.Path;
23 import java.nio.file.SimpleFileVisitor;
24 import java.nio.file.attribute.BasicFileAttributes;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Objects;
29
30 /**
31 * FileVisitor that sorts files.
32 */
33 public class SortingVisitor extends SimpleFileVisitor<Path> {
34
35 private final PathSorter sorter;
36 private final List<PathWithAttributes> collected = new ArrayList<>();
37
38 /**
39 * Constructs a new DeletingVisitor.
40 *
41 * @param basePath used to relativize paths
42 * @param pathFilters objects that need to confirm whether a file can be deleted
43 */
44 public SortingVisitor(final PathSorter sorter) {
45 this.sorter = Objects.requireNonNull(sorter, "sorter");
46 }
47
48 @Override
49 public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException {
50 collected.add(new PathWithAttributes(path, attrs));
51 return FileVisitResult.CONTINUE;
52 }
53
54 public List<PathWithAttributes> getSortedPaths() {
55 Collections.sort(collected, sorter);
56 return collected;
57 }
58 }