1 /* 2 * Copyright (C) 2015 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 package com.google.currysrc.api.match; 17 18 import com.google.currysrc.api.process.ast.BodyDeclarationLocator; 19 20 import org.eclipse.jdt.core.dom.CompilationUnit; 21 22 /** 23 * Useful factory methods for {@link SourceMatcher} instances. 24 */ 25 public final class SourceMatchers { 26 27 private static final SourceMatcher ALL_MATCHER = new SourceMatcher() { 28 @Override 29 public boolean matches(CompilationUnit cu) { 30 return true; 31 } 32 33 @Override public String toString() { 34 return "{match all}"; 35 } 36 }; 37 38 private SourceMatchers() { 39 } 40 41 /** Returns a {@link SourceMatcher} that matches any {@link CompilationUnit}. */ 42 public static SourceMatcher all() { 43 return ALL_MATCHER; 44 } 45 46 /** 47 * Returns a {@link SourceMatcher} that matches a {@link CompilationUnit} if it contains the 48 * body declaration located by {@code locator}. 49 */ 50 public static SourceMatcher contains(final BodyDeclarationLocator locator) { 51 return new SourceMatcher() { 52 @Override public boolean matches(CompilationUnit cu) { 53 return locator.find(cu) != null; 54 } 55 }; 56 } 57 58 } 59