Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.io;
     18 
     19 
     20 import java.io.File;
     21 import java.net.URI;
     22 import java.util.ArrayList;
     23 
     24 /**
     25  * An implementation of {@link IAbstractFolder} extending {@link File}.
     26  */
     27 public class FolderWrapper extends File implements IAbstractFolder {
     28 
     29     private static final long serialVersionUID = 1L;
     30 
     31     /**
     32      * Creates a new File instance from a parent abstract pathname and a child pathname string.
     33      * @param parent the parent pathname
     34      * @param child the child name
     35      *
     36      * @see File#File(File, String)
     37      */
     38     public FolderWrapper(File parent, String child) {
     39         super(parent, child);
     40     }
     41 
     42     /**
     43      * Creates a new File instance by converting the given pathname string into an abstract
     44      * pathname.
     45      * @param pathname the pathname
     46      *
     47      * @see File#File(String)
     48      */
     49     public FolderWrapper(String pathname) {
     50         super(pathname);
     51     }
     52 
     53     /**
     54      * Creates a new File instance from a parent abstract pathname and a child pathname string.
     55      * @param parent the parent pathname
     56      * @param child the child name
     57      *
     58      * @see File#File(String, String)
     59      */
     60     public FolderWrapper(String parent, String child) {
     61         super(parent, child);
     62     }
     63 
     64     /**
     65      * Creates a new File instance by converting the given <code>file:</code> URI into an
     66      * abstract pathname.
     67      * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path
     68      * component, and undefined authority, query, and fragment components
     69      *
     70      * @see File#File(URI)
     71      */
     72     public FolderWrapper(URI uri) {
     73         super(uri);
     74     }
     75 
     76     /**
     77      * Creates a new File instance matching a give {@link File} object.
     78      * @param file the file to match
     79      */
     80     public FolderWrapper(File file) {
     81         super(file.getAbsolutePath());
     82     }
     83 
     84     public IAbstractResource[] listMembers() {
     85         File[] files = listFiles();
     86         final int count = files == null ? 0 : files.length;
     87         IAbstractResource[] afiles = new IAbstractResource[count];
     88 
     89         if (files != null) {
     90             for (int i = 0 ; i < count ; i++) {
     91                 File f = files[i];
     92                 if (f.isFile()) {
     93                     afiles[i] = new FileWrapper(f);
     94                 } else if (f.isDirectory()) {
     95                     afiles[i] = new FolderWrapper(f);
     96                 }
     97             }
     98         }
     99 
    100         return afiles;
    101     }
    102 
    103     public boolean hasFile(final String name) {
    104         String[] match = list(new FilenameFilter() {
    105             public boolean accept(IAbstractFolder dir, String filename) {
    106                 return name.equals(filename);
    107             }
    108         });
    109 
    110         return match.length > 0;
    111     }
    112 
    113     public IAbstractFile getFile(String name) {
    114         return new FileWrapper(this, name);
    115     }
    116 
    117     public IAbstractFolder getFolder(String name) {
    118         return new FolderWrapper(this, name);
    119     }
    120 
    121     public IAbstractFolder getParentFolder() {
    122         String p = this.getParent();
    123         if (p == null) {
    124             return null;
    125         }
    126         return new FolderWrapper(p);
    127     }
    128 
    129     public String getOsLocation() {
    130         return getAbsolutePath();
    131     }
    132 
    133     @Override
    134     public boolean exists() {
    135         return isDirectory();
    136     }
    137 
    138     public String[] list(FilenameFilter filter) {
    139         File[] files = listFiles();
    140         if (files != null && files.length > 0) {
    141             ArrayList<String> list = new ArrayList<String>();
    142 
    143             for (File file : files) {
    144                 if (filter.accept(this, file.getName())) {
    145                     list.add(file.getName());
    146                 }
    147             }
    148 
    149             return list.toArray(new String[list.size()]);
    150         }
    151 
    152         return new String[0];
    153     }
    154 }
    155