OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / sdkmanager / libs / sdklib / src / com / android / sdklib / io / FolderWrapper.java
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.sdklib.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.length;
87         IAbstractResource[] afiles = new IAbstractResource[count];
88
89         for (int i = 0 ; i < count ; i++) {
90             File f = files[i];
91             if (f.isFile()) {
92                 afiles[i] = new FileWrapper(f);
93             } else {
94                 afiles[i] = new FolderWrapper(f);
95             }
96         }
97
98         return afiles;
99     }
100
101     public boolean hasFile(final String name) {
102         String[] match = list(new FilenameFilter() {
103             public boolean accept(IAbstractFolder dir, String filename) {
104                 return name.equals(filename);
105             }
106         });
107
108         return match.length > 0;
109     }
110
111     public IAbstractFile getFile(String name) {
112         return new FileWrapper(this, name);
113     }
114
115     public IAbstractFolder getFolder(String name) {
116         return new FolderWrapper(this, name);
117     }
118
119     public IAbstractFolder getParentFolder() {
120         String p = this.getParent();
121         if (p == null) {
122             return null;
123         }
124         return new FolderWrapper(p);
125     }
126
127     public String getOsLocation() {
128         return getAbsolutePath();
129     }
130
131     @Override
132     public boolean exists() {
133         return isDirectory();
134     }
135
136     public String[] list(FilenameFilter filter) {
137         File[] files = listFiles();
138         if (files.length > 0) {
139             ArrayList<String> list = new ArrayList<String>();
140
141             for (File file : files) {
142                 if (filter.accept(this, file.getName())) {
143                     list.add(file.getName());
144                 }
145             }
146
147             return list.toArray(new String[list.size()]);
148         }
149
150         return new String[0];
151     }
152 }