View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.filter;
20  
21  import org.eclipse.aether.RepositorySystemSession;
22  import org.eclipse.aether.artifact.Artifact;
23  import org.eclipse.aether.metadata.Metadata;
24  import org.eclipse.aether.repository.RemoteRepository;
25  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
26  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
27  
28  /**
29   * Some filters used in UTs.
30   */
31  public final class Filters {
32      /**
33       * Returns a filter that always accepts.
34       */
35      public static RemoteRepositoryFilterSource alwaysAccept() {
36          return new RemoteRepositoryFilterSource() {
37              public String getName() {
38                  return "always-accept";
39              }
40  
41              private final RemoteRepositoryFilter.Result RESULT =
42                      new RemoteRepositoryFilterSourceSupport.SimpleResult(true, getName());
43  
44              @Override
45              public RemoteRepositoryFilter getRemoteRepositoryFilter(RepositorySystemSession session) {
46                  return new RemoteRepositoryFilter() {
47                      @Override
48                      public Result acceptArtifact(RemoteRepository remoteRepository, Artifact artifact) {
49                          return RESULT;
50                      }
51  
52                      @Override
53                      public Result acceptMetadata(RemoteRepository remoteRepository, Metadata metadata) {
54                          return RESULT;
55                      }
56                  };
57              }
58          };
59      }
60  
61      /**
62       * Returns a filter that always accepts from given repo.
63       */
64      public static RemoteRepositoryFilterSource alwaysAcceptFrom(String repoId) {
65          return new RemoteRepositoryFilterSource() {
66              public String getName() {
67                  return "always-accept-" + repoId;
68              }
69  
70              private final RemoteRepositoryFilter.Result MATCHED =
71                      new RemoteRepositoryFilterSourceSupport.SimpleResult(true, getName());
72  
73              private final RemoteRepositoryFilter.Result UNMATCHED =
74                      new RemoteRepositoryFilterSourceSupport.SimpleResult(false, getName());
75  
76              @Override
77              public RemoteRepositoryFilter getRemoteRepositoryFilter(RepositorySystemSession session) {
78                  return new RemoteRepositoryFilter() {
79                      @Override
80                      public Result acceptArtifact(RemoteRepository remoteRepository, Artifact artifact) {
81                          return repoId.equals(remoteRepository.getId()) ? MATCHED : UNMATCHED;
82                      }
83  
84                      @Override
85                      public Result acceptMetadata(RemoteRepository remoteRepository, Metadata metadata) {
86                          return repoId.equals(remoteRepository.getId()) ? MATCHED : UNMATCHED;
87                      }
88                  };
89              }
90          };
91      }
92  
93      /**
94       * Returns a filter that never accepts.
95       */
96      public static RemoteRepositoryFilterSource neverAccept() {
97          return new RemoteRepositoryFilterSource() {
98              public String getName() {
99                  return "never-accept";
100             }
101 
102             private final RemoteRepositoryFilter.Result RESULT =
103                     new RemoteRepositoryFilterSourceSupport.SimpleResult(false, getName());
104 
105             @Override
106             public RemoteRepositoryFilter getRemoteRepositoryFilter(RepositorySystemSession session) {
107                 return new RemoteRepositoryFilter() {
108                     @Override
109                     public Result acceptArtifact(RemoteRepository remoteRepository, Artifact artifact) {
110                         return RESULT;
111                     }
112 
113                     @Override
114                     public Result acceptMetadata(RemoteRepository remoteRepository, Metadata metadata) {
115                         return RESULT;
116                     }
117                 };
118             }
119         };
120     }
121 
122     /**
123      * Returns a filter that never accepts from given repo.
124      */
125     public static RemoteRepositoryFilterSource neverAcceptFrom(String repoId) {
126         return new RemoteRepositoryFilterSource() {
127             public String getName() {
128                 return "never-accept-" + repoId;
129             }
130 
131             private final RemoteRepositoryFilter.Result MATCHED =
132                     new RemoteRepositoryFilterSourceSupport.SimpleResult(false, getName());
133 
134             private final RemoteRepositoryFilter.Result UNMATCHED =
135                     new RemoteRepositoryFilterSourceSupport.SimpleResult(true, getName());
136 
137             @Override
138             public RemoteRepositoryFilter getRemoteRepositoryFilter(RepositorySystemSession session) {
139                 return new RemoteRepositoryFilter() {
140                     @Override
141                     public Result acceptArtifact(RemoteRepository remoteRepository, Artifact artifact) {
142                         return repoId.equals(remoteRepository.getId()) ? MATCHED : UNMATCHED;
143                     }
144 
145                     @Override
146                     public Result acceptMetadata(RemoteRepository remoteRepository, Metadata metadata) {
147                         return repoId.equals(remoteRepository.getId()) ? MATCHED : UNMATCHED;
148                     }
149                 };
150             }
151         };
152     }
153 }