1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation.
  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. package org.apache.commons.io.filefilter;
  17. import java.io.FileFilter;
  18. import java.io.FilenameFilter;
  19. /**
  20. * Useful utilities for working with file filters. It provides access to all
  21. * file filter implementations in this package so you don't have to import
  22. * every class you use.
  23. *
  24. * @since Commons IO 1.0
  25. * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
  26. *
  27. * @author Henri Yandell
  28. * @author Stephen Colebourne
  29. * @author Jeremias Maerki
  30. */
  31. public class FileFilterUtils {
  32. /**
  33. * FileFilterUtils is not normally instantiated.
  34. */
  35. public FileFilterUtils() {
  36. }
  37. //-----------------------------------------------------------------------
  38. /**
  39. * Returns a filter that returns true if the filename starts with the specified text.
  40. *
  41. * @param prefix the filename prefix
  42. * @return a prefix checking filter
  43. */
  44. public static IOFileFilter prefixFileFilter(String prefix) {
  45. return new PrefixFileFilter(prefix);
  46. }
  47. /**
  48. * Returns a filter that returns true if the filename ends with the specified text.
  49. *
  50. * @param suffix the filename suffix
  51. * @return a suffix checking filter
  52. */
  53. public static IOFileFilter suffixFileFilter(String suffix) {
  54. return new SuffixFileFilter(suffix);
  55. }
  56. /**
  57. * Returns a filter that returns true if the filename matches the specified text.
  58. *
  59. * @param name the filename
  60. * @return a name checking filter
  61. */
  62. public static IOFileFilter nameFileFilter(String name) {
  63. return new NameFileFilter(name);
  64. }
  65. /**
  66. * Returns a filter that checks if the file is a directory.
  67. *
  68. * @return directory file filter
  69. */
  70. public static IOFileFilter directoryFileFilter() {
  71. return DirectoryFileFilter.INSTANCE;
  72. }
  73. //-----------------------------------------------------------------------
  74. /**
  75. * Returns a filter that ANDs the two specified filters.
  76. *
  77. * @param filter1 the first filter
  78. * @param filter2 the second filter
  79. * @return a filter that ANDs the two specified filters
  80. */
  81. public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
  82. return new AndFileFilter(filter1, filter2);
  83. }
  84. /**
  85. * Returns a filter that ORs the two specified filters.
  86. *
  87. * @param filter1 the first filter
  88. * @param filter2 the second filter
  89. * @return a filter that ORs the two specified filters
  90. */
  91. public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
  92. return new OrFileFilter(filter1, filter2);
  93. }
  94. /**
  95. * Returns a filter that NOTs the specified filter.
  96. *
  97. * @param filter the filter to invert
  98. * @return a filter that NOTs the specified filter
  99. */
  100. public static IOFileFilter notFileFilter(IOFileFilter filter) {
  101. return new NotFileFilter(filter);
  102. }
  103. //-----------------------------------------------------------------------
  104. /**
  105. * Returns a filter that always returns true.
  106. *
  107. * @return a true filter
  108. */
  109. public static IOFileFilter trueFileFilter() {
  110. return TrueFileFilter.INSTANCE;
  111. }
  112. /**
  113. * Returns a filter that always returns false.
  114. *
  115. * @return a false filter
  116. */
  117. public static IOFileFilter falseFileFilter() {
  118. return FalseFileFilter.INSTANCE;
  119. }
  120. //-----------------------------------------------------------------------
  121. /**
  122. * Returns an <code>IOFileFilter</code> that wraps the
  123. * <code>FileFilter</code> instance.
  124. *
  125. * @param filter the filter to be wrapped
  126. * @return a new filter that implements IOFileFilter
  127. */
  128. public static IOFileFilter asFileFilter(FileFilter filter) {
  129. return new DelegateFileFilter(filter);
  130. }
  131. /**
  132. * Returns an <code>IOFileFilter</code> that wraps the
  133. * <code>FilenameFilter</code> instance.
  134. *
  135. * @param filter the filter to be wrapped
  136. * @return a new filter that implements IOFileFilter
  137. */
  138. public static IOFileFilter asFileFilter(FilenameFilter filter) {
  139. return new DelegateFileFilter(filter);
  140. }
  141. //-----------------------------------------------------------------------
  142. /* Constructed on demand and then cached */
  143. private static IOFileFilter cvsFilter = null;
  144. /**
  145. * Resturns an IOFileFilter that ignores CVS directories. You may optionally
  146. * pass in an existing IOFileFilter in which case it is extended to exclude
  147. * CVS directories.
  148. * @param filter IOFileFilter to modify, null if a new IOFileFilter
  149. * should be created
  150. * @return the requested (combined) filter
  151. */
  152. public static IOFileFilter makeCVSAware(IOFileFilter filter) {
  153. if (cvsFilter == null) {
  154. cvsFilter = andFileFilter(directoryFileFilter(),
  155. notFileFilter(nameFileFilter("CVS")));
  156. }
  157. if (filter == null) {
  158. return cvsFilter;
  159. } else {
  160. return andFileFilter(filter, cvsFilter);
  161. }
  162. }
  163. }