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
17package com.android.io;
18
19
20import java.io.File;
21import java.net.URI;
22import java.util.ArrayList;
23
24/**
25 * An implementation of {@link IAbstractFolder} extending {@link File}.
26 */
27public 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    @Override
85    public IAbstractResource[] listMembers() {
86        File[] files = listFiles();
87        final int count = files == null ? 0 : files.length;
88        IAbstractResource[] afiles = new IAbstractResource[count];
89
90        if (files != null) {
91            for (int i = 0 ; i < count ; i++) {
92                File f = files[i];
93                if (f.isFile()) {
94                    afiles[i] = new FileWrapper(f);
95                } else if (f.isDirectory()) {
96                    afiles[i] = new FolderWrapper(f);
97                }
98            }
99        }
100
101        return afiles;
102    }
103
104    @Override
105    public boolean hasFile(final String name) {
106        String[] match = list(new FilenameFilter() {
107            @Override
108            public boolean accept(IAbstractFolder dir, String filename) {
109                return name.equals(filename);
110            }
111        });
112
113        return match.length > 0;
114    }
115
116    @Override
117    public IAbstractFile getFile(String name) {
118        return new FileWrapper(this, name);
119    }
120
121    @Override
122    public IAbstractFolder getFolder(String name) {
123        return new FolderWrapper(this, name);
124    }
125
126    @Override
127    public IAbstractFolder getParentFolder() {
128        String p = this.getParent();
129        if (p == null) {
130            return null;
131        }
132        return new FolderWrapper(p);
133    }
134
135    @Override
136    public String getOsLocation() {
137        return getAbsolutePath();
138    }
139
140    @Override
141    public boolean exists() {
142        return isDirectory();
143    }
144
145    @Override
146    public String[] list(FilenameFilter filter) {
147        File[] files = listFiles();
148        if (files != null && files.length > 0) {
149            ArrayList<String> list = new ArrayList<String>();
150
151            for (File file : files) {
152                if (filter.accept(this, file.getName())) {
153                    list.add(file.getName());
154                }
155            }
156
157            return list.toArray(new String[list.size()]);
158        }
159
160        return new String[0];
161    }
162}
163