View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.filters.pattern;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.mojo.jaxb2.shared.Validate;
23  import org.codehaus.mojo.jaxb2.shared.filters.AbstractFilter;
24  import org.codehaus.mojo.jaxb2.shared.filters.Filter;
25  
26  import java.io.File;
27  import java.io.FileFilter;
28  
29  /**
30   * Filter implementation adapting a FileFilter instance to the Filter interface.
31   * Delegates the {@link #onCandidate(File)} call to the supplied {@link FileFilter} delegate.
32   *
33   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
34   * @since 2.3
35   */
36  public class FileFilterAdapter extends AbstractFilter<File> implements Filter<File>, FileFilter {
37  
38      // Internal state
39      private FileFilter delegate;
40  
41      /**
42       * Compound constructor, creating a FileFilterAdapter using the supplied {@link FileFilter} to determine if
43       * candidate Files should be accepted.
44       *
45       * @param delegate The delegate FileFilter.
46       */
47      public FileFilterAdapter(final FileFilter delegate) {
48          setDelegate(delegate);
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public boolean isInitialized() {
56          return super.isInitialized() && delegate != null;
57      }
58  
59      /**
60       * Assigns the supplied FileFilter delegate.
61       *
62       * @param delegate A non-null FileFilter instance.
63       */
64      public void setDelegate(final FileFilter delegate) {
65  
66          // Check sanity
67          Validate.notNull(delegate, "delegate");
68  
69          // Assign internal state
70          this.delegate = delegate;
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected boolean onCandidate(final File nonNullCandidate) {
78          return delegate.accept(nonNullCandidate);
79      }
80  }