Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014, The OpenNMS Group
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"); you may
   * not use this file except in compliance with the License. You may obtain
   * a copy of the License at
   * 
   *     http://www.apache.org/licenses/LICENSE-2.0
   *     
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.opennms.newts.gsod;
 
 import java.io.File;
 import java.io.Reader;
 
 
 public class FileIterable {
 
     public static FluentIterable<PathfileTreeWalker(final Path root) {
         return FluentIterable.from(Iterables.concat(groupFilesByDir(root)));
     }
     
     public static class KeyedIterable<K, T> extends FluentIterable<T> {
         
         private K m_key;
         private Iterable<T> m_items;
         
 
         public KeyedIterable(K keyIterable<T> items) {
              = key;
              = items;
         }
 
         @Override
         public Iterator<T> iterator() {
             return .iterator();
         }
         
         public K getKey() {
             return ;
         }
 
     }
     
     private static KeyedIterable<PathPathchildren(File dirFileFilter filter) {
         return new KeyedIterable<PathPath>(dir.toPath(), toPaths(dir.listFiles(filter)));
     }
 
     private static KeyedIterable<PathPathfiles(File dir) {
         return children(dirfileMatcher());
     }
     
     private static KeyedIterable<PathPathsubdirs(File dir) {
         return children(dirdirectoryMatcher());
     }
 
 
     private static FileFilter fileMatcher() {
         return new FileFilter() {
             
             @Override
             public boolean accept(File f) {
                 return f.isFile();
             }
         };
     }
     
     private static FileFilter directoryMatcher() {
         return new FileFilter() {
             
             @Override
             public boolean accept(File f) {
                 return f.isDirectory();
             }
        };
    }
    public static Iterable<PathtoPaths(File[] files) {
        return files == null ? Collections.<Path>emptyList() : toPaths(Arrays.asList(files));
    }
    
    public static Iterable<PathtoPaths(Iterable<Filefiles) {
        return Iterables.transform(filesnew Function<FilePath>(){
            @Override
            public Path apply(File input) {
                return input.toPath();
            }
            
        });
    }
    
    public static Iterable<FiletoFiles(Iterable<Pathfiles) {
        return Iterables.transform(filesnew Function<PathFile>(){
            @Override
            public File apply(Path input) {
                return input.toFile();
            }
            
        });
    }
    
    public static class GroupedPathIterator extends AbstractIterator<KeyedIterable<PathPath>> {
        
        Stack<Iterator<Path>> m_dirStack;
        File m_root;
        
        GroupedPathIterator(File root) {
             = root;
        }
        @Override
        protected KeyedIterable<PathPathcomputeNext() {
            if ( == null) {
                 = new Stack<Iterator<Path>>();
                .push(subdirs().iterator());
                return files();
            } 
            
            while(!.isEmpty()) {
                Iterator<Pathsubdirs = .peek();
                if (!subdirs.hasNext()) {
                    .pop();
                } else {
                    File dir = subdirs.next().toFile();
                    .push(subdirs(dir).iterator());
                    return files(dir);
                }
            }
            
            endOfData();
            return null;
        }
        
    }
    
    public static FluentIterable<KeyedIterable<PathPath>> groupFilesByDir(final Path root) {
        return new FluentIterable<KeyedIterable<PathPath>>() {
            @Override
            public Iterator<KeyedIterable<PathPath>> iterator() {
                return new GroupedPathIterator(root.toFile());
            }
            
        };
    }
    
    private static class LineIterator implements Iterator<String> {
        
        private BufferedReader m_in;
        private String m_nextLine;
        
        public LineIterator(Reader r) {
             = new BufferedReader(r);
            fetchLine();
        }
        
        private void fetchLine() {
            if ( == nullreturn;
            try {
                 = .readLine();
            } catch (IOException e) {
                throw Throwables.propagate(e);
            } finally {
                if ( == null) {
                    close();
                }
            }
        }
        
        private void close() {
            try {
                if ( != null) {
                    .close();
                }
            } catch(IOException e) {
                throw Throwables.propagate(e);
            } finally {
                 = null;
            }
        }
        @Override
        public boolean hasNext() {
            return  != null;
        }
        @Override
        public String next() {
            String line = ;
            fetchLine();
            return line;
        }
        @Override
        public void remove() {
            throw new UnsupportedOperationException("Iterator<String>.remove is not yet implemented.");
        }
        
    }
    
    public static FluentIterable<Stringlines(final Path path) {
        return new FluentIterable<String>() {
            @Override
            public Iterator<Stringiterator() {
                try {
                    return new LineIterator(new FileReader(path.toFile()));
                } catch (FileNotFoundException e) {
                    throw Throwables.propagate(e);
                }
            }
        };
        
    }
    
    public static FluentIterable<StringunzipLines(final Path pathfinal Charset cs) {
        return new FluentIterable<String>() {
            @Override
            public Iterator<Stringiterator() {
                try {
                    return new LineIterator(zippedFileReader(pathcs));
                } catch (IOException e) {
                    throw Throwables.propagate(e);
                }
            }
        };
        
    }
    
    private static Reader zippedFileReader(final Path pathfinal Charset cs)  throws IOException {
        InputStream gzipStream = new GZIPInputStream(new FileInputStream(path.toFile()));
        return new InputStreamReader(gzipStreamcs);
    }
    
    public static <F, T> Function<? super Iterable<F>, Iterable<T>> bind(final Function<? super F, Iterable<T>> f) {
        return new Function<Iterable<F>, Iterable<T>>() {
            @Override
            public Iterable<T> apply(Iterable<F> input) {
                return Iterables.concat(Iterables.transform(inputf));
            }
        };
    }
    
    public static <F, T> Function<? super Iterable<F>, Iterable<T>> lift(final Function<F, T> f) {
        return new Function<Iterable<F>, Iterable<T>>() {
            @Override
            public Iterable<T> apply(Iterable<F> input) {
                return Iterables.transform(inputf);
            }
            
        };
    }
    
New to GrepCode? Check out our FAQ X