org.apache.commons.io.comparator.NameFileComparator Java Examples

The following examples show how to use org.apache.commons.io.comparator.NameFileComparator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileNodeUtil.java    From youran with Apache License 2.0 6 votes vote down vote up
/**
 * 递归获取某个目录下的文件节点树
 *
 * @param dirFile
 * @param basePath
 * @return
 */
public static List<FileNodeVO> recurFileNodeTree(File dirFile, File basePath) {
    File[] files = dirFile.listFiles(getFileFilter());
    if (ArrayUtils.isEmpty(files)) {
        return Collections.emptyList();
    }
    return Arrays.stream(files)
        .sorted(new CompositeFileComparator(DirectoryFileComparator.DIRECTORY_COMPARATOR, NameFileComparator.NAME_COMPARATOR))
        .map(file -> {
            FileNodeVO nodeVO = fileToNodeVO(file, basePath);
            if (file.isDirectory()) {
                List<FileNodeVO> children = recurFileNodeTree(file, basePath);
                nodeVO.setChildren(children);
            }
            return nodeVO;
        })
        .collect(Collectors.toList());
}
 
Example #2
Source File: FileAlterationObserver.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Construct an observer for the specified directory, file filter and
 * file comparator.
 *
 * @param rootEntry the root directory to observe
 * @param fileFilter The file filter or null if none
 * @param caseSensitivity  what case sensitivity to use comparing file names, null means system sensitive
 */
protected FileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, IOCase caseSensitivity) {
    if (rootEntry == null) {
        throw new IllegalArgumentException("Root entry is missing");
    }
    if (rootEntry.getFile() == null) {
        throw new IllegalArgumentException("Root directory is missing");
    }
    this.rootEntry = rootEntry;
    this.fileFilter = fileFilter;
    if (caseSensitivity == null || caseSensitivity.equals(IOCase.SYSTEM)) {
        this.comparator = NameFileComparator.NAME_SYSTEM_COMPARATOR;
    } else if (caseSensitivity.equals(IOCase.INSENSITIVE)) {
        this.comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
    } else {
        this.comparator = NameFileComparator.NAME_COMPARATOR;
    }
}
 
Example #3
Source File: FileAlterationObserver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct an observer for the specified directory, file filter and
 * file comparator.
 *
 * @param rootEntry the root directory to observe
 * @param fileFilter The file filter or null if none
 * @param caseSensitivity  what case sensitivity to use comparing file names, null means system sensitive
 */
protected FileAlterationObserver(final FileEntry rootEntry, final FileFilter fileFilter,
                                 final IOCase caseSensitivity) {
    if (rootEntry == null) {
        throw new IllegalArgumentException("Root entry is missing");
    }
    if (rootEntry.getFile() == null) {
        throw new IllegalArgumentException("Root directory is missing");
    }
    this.rootEntry = rootEntry;
    this.fileFilter = fileFilter;
    if (caseSensitivity == null || caseSensitivity.equals(IOCase.SYSTEM)) {
        this.comparator = NameFileComparator.NAME_SYSTEM_COMPARATOR;
    } else if (caseSensitivity.equals(IOCase.INSENSITIVE)) {
        this.comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
    } else {
        this.comparator = NameFileComparator.NAME_COMPARATOR;
    }
}
 
Example #4
Source File: AbstractIteratorMojo.java    From iterator-maven-plugin with Apache License 2.0 6 votes vote down vote up
protected Comparator<File> convertSortOrder()
{
    Comparator<File> result = NameFileComparator.NAME_COMPARATOR;
    if ( getSortOrder().equalsIgnoreCase( "NAME_INSENSITIVE_COMPARATOR" ) )
    {
        result = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
    }
    else if ( getSortOrder().equalsIgnoreCase( "NAME_INSENSITIVE_REVERSE" ) )
    {
        result = NameFileComparator.NAME_INSENSITIVE_REVERSE;
    }
    else if ( getSortOrder().equalsIgnoreCase( "NAME_REVERSE" ) )
    {
        result = NameFileComparator.NAME_REVERSE;
    }
    else if ( getSortOrder().equalsIgnoreCase( "NAME_SYSTEM_COMPARATOR" ) )
    {
        result = NameFileComparator.NAME_SYSTEM_COMPARATOR;
    }
    else if ( getSortOrder().equalsIgnoreCase( "NAME_SYSTEM_REVERSE" ) )
    {
        result = NameFileComparator.NAME_SYSTEM_REVERSE;
    }
    return result;
}
 
Example #5
Source File: SingerUtils.java    From singer with Apache License 2.0 5 votes vote down vote up
public int compare(File file1, File file2) {
  int lastModifiedTimeComparison = LastModifiedFileComparator.LASTMODIFIED_COMPARATOR.compare(file1, file2);
  if (lastModifiedTimeComparison != 0) {
    return lastModifiedTimeComparison;
  }

  int fileNameLengthComparsion = file2.getName().length() - file1.getName().length();
  if (fileNameLengthComparsion != 0) {
    return fileNameLengthComparsion;
  }

  return NameFileComparator.NAME_REVERSE.compare(file1, file2);
}
 
Example #6
Source File: LogStream.java    From singer with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public void initialize() throws IOException {
  SingerLogConfig singerLogConfig = singerLog.getSingerLogConfig();
  String regexStr = fileNamePrefix;
  File logDir = new File(singerLogConfig.getLogDir());

  if (singerLogConfig.getFilenameMatchMode() == FileNameMatchMode.PREFIX) {
    regexStr += ".*";
  }

  LOG.info("Matching files under {} with filter {}", logDir, regexStr);
  FileFilter fileFilter = new RegexFileFilter(regexStr);
  File[] files = logDir.listFiles(fileFilter);

  // Sort the file first by last_modified timestamp and then by name in case two files have
  // the same mtime due to precision (mtime is up to seconds).
  Ordering ordering = Ordering.from(
      new CompositeFileComparator(
          LastModifiedFileComparator.LASTMODIFIED_COMPARATOR, NameFileComparator.NAME_REVERSE));
  List<File> logFiles = ordering.sortedCopy(Arrays.asList(files));

  LOG.info(files.length + " files matches the regex '{}'", regexStr);
  synchronized (logFilesInfoLock) {
    logFilePaths.clear();
    logFilePathsIndex.clear();
    for (File entry : logFiles) {
      long inode = SingerUtils.getFileInode(entry.toPath());
      append(new LogFile(inode), entry.toPath().toString());
    }
  }
  OpenTsdbMetricConverter.incr(SingerMetrics.LOGSTREAM_INITIALIZE, 1,
      "log=" + logStreamName, "host=" + SingerUtils.getHostname());
}
 
Example #7
Source File: NavigationHelper.java    From Android-FileBrowser-FilePicker with MIT License 5 votes vote down vote up
public ArrayList<FileItem> getFilesItemsInCurrentDirectory() {
    Operations op = Operations.getInstance(mContext);
    Constants.SORT_OPTIONS option = op.getmCurrentSortOption();
    Constants.FILTER_OPTIONS filterOption = op.getmCurrentFilterOption();
    if (mFileNavigator.getmCurrentNode() == null) mFileNavigator.setmCurrentNode(mFileNavigator.getmRootNode());
    File[] files = mFileNavigator.getFilesInCurrentDirectory();
    if (files != null) {
        mFiles.clear();
        Comparator<File> comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
        switch(option) {
            case SIZE:
                comparator = SizeFileComparator.SIZE_COMPARATOR;
                break;
            case LAST_MODIFIED:
                comparator = LastModifiedFileComparator.LASTMODIFIED_COMPARATOR;
                break;
        }
        Arrays.sort(files,comparator);
        for (int i = 0; i < files.length; i++) {
            boolean addToFilter = true;
            switch(filterOption) {
                case FILES:
                    addToFilter = !files[i].isDirectory();
                    break;
                case FOLDER:
                    addToFilter = files[i].isDirectory();
                    break;
            }
            if (addToFilter)
                mFiles.add(new FileItem(files[i]));
        }
    }
    return mFiles;
}
 
Example #8
Source File: SQSActivityAction.java    From aws-codecommit-trigger-plugin with Apache License 2.0 5 votes vote down vote up
public List<String> getLogNames() {
    List<String> names = new ArrayList<>();
    File[] files = this.activityDir.listFiles();
    if (files != null) {
        Arrays.sort(files, NameFileComparator.NAME_REVERSE);
        for (File file : files) {
            names.add(file.getName());
        }
    }
    return names;
}
 
Example #9
Source File: TemplateService.java    From java-platform with Apache License 2.0 5 votes vote down vote up
private TemplateFile[] toTemplates(File dir, String parentPath) {
	List<TemplateFile> list = new ArrayList<>();
	File[] files = dir.listFiles();

	Arrays.sort(files, NameFileComparator.NAME_COMPARATOR);
	Arrays.sort(files, DirectoryFileComparator.DIRECTORY_COMPARATOR);

	for (File file : files) {
		TemplateFile templateFile = new TemplateFile();
		templateFile.setText(file.getName());
		String path = parentPath + "/" + file.getName();
		templateFile.setPath(path);
		if (file.isDirectory()) {
			templateFile.setChildren(toTemplates(file, path));
			templateFile.setIconCls("Folder");
		} else {
			String extension = FilenameUtils.getExtension(file.getName());
			if ("html".equals(extension)) {
				templateFile.setIconCls("Html");
			} else if ("js".equals(extension)) {
				templateFile.setIconCls("Script");
			} else if ("css".equals(extension)) {
				templateFile.setIconCls("Css");
			} else {
				templateFile.setIconCls("Page");
			}
		}
		list.add(templateFile);
	}

	return Iterables.toArray(list, TemplateFile.class);
}
 
Example #10
Source File: SystemService.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public void cleanDumpDatabase(int keepno)
{
   File[]dumps = new File(
         cfgManager.getDatabaseConfiguration ().getDumpPath ())
         .listFiles(new FilenameFilter()
   {
      @Override
      public boolean accept(File path, String name)
      {
         if (name.startsWith("dump-"))
            return true;
         return false;
      }
   });
   if ((dumps!=null) && (dumps.length > keepno))
   {
      Arrays.sort(dumps, NameFileComparator.NAME_COMPARATOR);
      int last = dumps.length - keepno;
      for (int index=0; index<last; index++)
      {
         File dir = dumps[index];
         try
         {
            Date date = new Date (Long.parseLong (dir.getName ()
                  .replaceAll ("dump-(.*)", "$1")));
            LOGGER.info("Cleaned dump of " + date);
            FileUtils.deleteDirectory(dir);
         }
         catch (IOException e)
         {
            LOGGER.warn("Cannot delete directory " + dir.getPath() + " (" +
               e.getMessage() + ")");
         }
      }
   }
}
 
Example #11
Source File: GenerateRunnersMojo.java    From cucumber-jvm-parallel-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Called by Maven to run this mojo after parameters have been injected.
 */
public void execute() throws MojoExecutionException {

    if (!featuresDirectory.exists()) {
        throw new MojoExecutionException("Features directory does not exist");
    }

    final Collection<File> featureFiles = listFiles(featuresDirectory, new String[] {"feature"}, true);
    final List<File> sortedFeatureFiles = new NameFileComparator().sort(new ArrayList<File>(featureFiles));

    createOutputDirIfRequired();

    File packageDirectory = packageName == null
            ? outputDirectory
            : new File(outputDirectory, packageName.replace('.','/'));

    if (!packageDirectory.exists()) {
        packageDirectory.mkdirs();
    }

    final CucumberITGenerator fileGenerator = createFileGenerator();
    fileGenerator.generateCucumberITFiles(packageDirectory, sortedFeatureFiles);

    getLog().info("Adding " + outputDirectory.getAbsolutePath()
                    + " to test-compile source root");

    project.addTestCompileSourceRoot(outputDirectory.getAbsolutePath());
}