zoukankan      html  css  js  c++  java
  • elfinder中通过DirectoryStream.Filter实现筛选隐藏目录(二)

    今天还是没事看了看elfinder源码,发现之前说的两个版本实现都是基于不同的jdkelfinder源码浏览-Volume文件系统操作类(1)

    带前端页面的是基于1.6中File实现,另一个是基于1.7中的Path实现,

    今天看了一个非常有意思的匿名类应用,先看基础java中一个引人深思的匿名内部类

    其中引起我注意的是一个文件搜索的实现,

    项目代码并没有引起我的问题,但深思的是基于1.8源码的实现使用了一个

    @@@@@DirectoryStream.Filter<Path>接口,此接口是一个包装接口,使用了java1.8中的接口函数式方法@FunctionalInterface@@@@@@@@@@@@@@@@

    请先参考java1.7中新增的NIOJAVA NIO:Path ,File

    首先先看一看此方法应用,此方法获取给定的工作空间中非隐藏的文件列表,

    包装Volume类
      @Override
        public Target[] listChildren(Target target) throws IOException {
            //其中这里使用了一个NioHelper工具类封装了之前定义的Path接口,来获取非隐藏文件
            
            List<Path> childrenResultList = NioHelper.listChildrenNotHidden(fromTarget(target));
            List<Target> targets = new ArrayList<>(childrenResultList.size());
            for (Path path : childrenResultList) {
                targets.add(fromPath(path));
            }
            return targets.toArray(new Target[targets.size()]);
        }
    
    

    令人深思的是下面我们它的实现

    NIOHelper类

    此类是我今天看到的一个匿名接口使用

      /**
         * Gets children paths from the give directory (excluding hidden files).
         *  带有文件过滤的
         * @param dir path to the directory.
         * @return the children paths from the give directory.
         * @throws IOException if something goes wrong.
         */
        public static List<Path> listChildrenNotHidden(Path dir) throws IOException {
            // not hidden file filter
    // DirectoryStream.Filter<Path> notHiddenFilter = new DirectoryStream.Filter<Path>() { public boolean accept(Path path) throws IOException { return !Files.isHidden(path); } };
        //调用了同级类中的方法listChildren
    return listChildren(dir, notHiddenFilter); }
      /**
         * Gets children paths from the give directory appling the given filter.
         * 依据NIO中Files类中newDirectoryStream方法传入一个
        DirectoryStream.Filter接口来判断是否是隐藏目录,如果是就舍弃,不是就加入一个list中,
        然后使用Collrctions中的unmodifiableList设置此list不可更改,
        通过判断传入的dir来让Collrctions中emptyList方法设置返回的list是否是一个空的
    * @param dir path to the directory. * @param filter the filter to be applied * @return the children paths from the give directory. * @throws IOException if something goes wrong. */ 
    public static List<Path> listChildren(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
        if (isFolder(dir)) { List<Path> list = new ArrayList<>();
    try (DirectoryStream<Path> directoryStream = (filter != null ? Files.newDirectoryStream(dir, filter) : Files.newDirectoryStream(dir))) { for (Path p : directoryStream) { list.add(p); } } return Collections.unmodifiableList(list); } return Collections.emptyList(); }
    
    

     至此我们已经看完了这个实现的具体应用,发现原来匿名类是如此的强大,完全少写很多之前写代码中的逻辑判断

  • 相关阅读:
    人机猜拳
    M1分数分配
    postmortem report of period M1
    视频文档分享网站场景测试说明书
    功能规格说明书
    11.9Daily Scrum
    11.6Daily Scrum
    需求博客
    11.5Daily Scrum
    11.3Daily Scrum
  • 原文地址:https://www.cnblogs.com/dmeck/p/8631670.html
Copyright © 2011-2022 走看看