1 /* 2 * Copyright (C) 2017 The Guava Authors 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 import com.google.common.collect.TreeTraverser; 18 import com.google.common.graph.Traverser; 19 import com.google.errorprone.refaster.annotation.AfterTemplate; 20 import com.google.errorprone.refaster.annotation.BeforeTemplate; 21 import com.google.errorprone.refaster.annotation.Placeholder; 22 23 /** 24 * Refaster rules to rewrite usages of {@code com.google.common.collect.TreeTraverser} in terms of 25 * {@code com.google.common.graph.Traverser}. 26 */ 27 @SuppressWarnings("DefaultPackage") 28 public class TraverserRewrite { 29 abstract class TreeTraverserPreOrder<N> { 30 @Placeholder getChildren(N node)31 abstract Iterable<N> getChildren(N node); 32 33 @BeforeTemplate before1(N root)34 Iterable<N> before1(N root) { 35 return TreeTraverser.using((N node) -> getChildren(node)).preOrderTraversal(root); 36 } 37 38 @BeforeTemplate before2(N root)39 Iterable<N> before2(N root) { 40 return new TreeTraverser<N>() { 41 @Override 42 public Iterable<N> children(N node) { 43 return getChildren(node); 44 } 45 }.preOrderTraversal(root); 46 } 47 48 @AfterTemplate after(N root)49 Iterable<N> after(N root) { 50 return Traverser.forTree((N node) -> getChildren(node)).depthFirstPreOrder(root); 51 } 52 } 53 54 abstract class TreeTraverserPostOrder<N> { 55 @Placeholder 56 abstract Iterable<N> getChildren(N node); 57 58 @BeforeTemplate 59 Iterable<N> before1(N root) { 60 return TreeTraverser.using((N node) -> getChildren(node)).postOrderTraversal(root); 61 } 62 63 @BeforeTemplate 64 Iterable<N> before2(N root) { 65 return new TreeTraverser<N>() { 66 @Override 67 public Iterable<N> children(N node) { 68 return getChildren(node); 69 } 70 }.postOrderTraversal(root); 71 } 72 73 @AfterTemplate 74 Iterable<N> after(N root) { 75 return Traverser.forTree((N node) -> getChildren(node)).depthFirstPostOrder(root); 76 } 77 } 78 79 abstract class TreeTraverserBreadthFirst<N> { 80 @Placeholder 81 abstract Iterable<N> getChildren(N node); 82 83 @BeforeTemplate 84 Iterable<N> before1(N root) { 85 return TreeTraverser.using((N node) -> getChildren(node)).breadthFirstTraversal(root); 86 } 87 88 @BeforeTemplate 89 Iterable<N> before2(N root) { 90 return new TreeTraverser<N>() { 91 @Override 92 public Iterable<N> children(N node) { 93 return getChildren(node); 94 } 95 }.breadthFirstTraversal(root); 96 } 97 98 @AfterTemplate 99 Iterable<N> after(N root) { 100 return Traverser.forTree((N node) -> getChildren(node)).breadthFirst(root); 101 } 102 } 103 } 104