Skip to content
Permalink
Browse files

Open source refactorings directory. This is knowingly very simple, wi…

…thout yet documentation or many refactorings; it's a prototype first.

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190505527
  • Loading branch information
lowasser authored and ronshapiro committed Mar 26, 2018
1 parent 38d2f32 commit ddaf710bedddf89bdd85c4f3cc5c767a03fc5e5e
Showing with 283 additions and 0 deletions.
  1. +181 −0 refactorings/CharMatcherRewrite.java
  2. +102 −0 refactorings/TraverserRewrite.java
@@ -0,0 +1,181 @@
/*
* Copyright (C) 2017 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.google.common.base.CharMatcher;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;

/**
* Refaster refactorings to rewrite uses of CharMatcher static constants to the static factory
* methods.
*/
public class CharMatcherRewrite {
class Whitespace {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.WHITESPACE;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.whitespace();
}
}

class BreakingWhitespace {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.BREAKING_WHITESPACE;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.breakingWhitespace();
}
}

class Ascii {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.ASCII;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.ascii();
}
}

class Digit {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.DIGIT;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.digit();
}
}

class JavaDigit {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.JAVA_DIGIT;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.javaDigit();
}
}

class JavaLetterOrDigit {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.JAVA_LETTER_OR_DIGIT;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.javaLetterOrDigit();
}
}

class JavaUpperCase {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.JAVA_UPPER_CASE;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.javaUpperCase();
}
}

class JavaLowerCase {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.JAVA_LOWER_CASE;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.javaLowerCase();
}
}

class JavaIsoControl {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.JAVA_ISO_CONTROL;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.javaIsoControl();
}
}

class Invisible {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.INVISIBLE;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.invisible();
}
}

class SingleWidth {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.SINGLE_WIDTH;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.singleWidth();
}
}

class Any {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.ANY;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.any();
}
}

class None {
@BeforeTemplate
CharMatcher before() {
return CharMatcher.NONE;
}

@AfterTemplate
CharMatcher after() {
return CharMatcher.none();
}
}
}
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2017 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.google.common.collect.TreeTraverser;
import com.google.common.graph.Traverser;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Placeholder;

/**
* Refaster rules to rewrite usages of {@code com.google.common.collect.TreeTraverser} in terms of
* {@code com.google.common.graph.Traverser}.
*/
public class TraverserRewrite {
abstract class TreeTraverserPreOrder<N> {
@Placeholder
abstract Iterable<N> getChildren(N node);

@BeforeTemplate
Iterable<N> before1(N root) {
return TreeTraverser.using((N node) -> getChildren(node)).preOrderTraversal(root);
}

@BeforeTemplate
Iterable<N> before2(N root) {
return new TreeTraverser<N>() {
@Override
public Iterable<N> children(N node) {
return getChildren(node);
}
}.preOrderTraversal(root);
}

@AfterTemplate
Iterable<N> after(N root) {
return Traverser.forTree((N node) -> getChildren(node)).depthFirstPreOrder(root);
}
}

abstract class TreeTraverserPostOrder<N> {
@Placeholder
abstract Iterable<N> getChildren(N node);

@BeforeTemplate
Iterable<N> before1(N root) {
return TreeTraverser.using((N node) -> getChildren(node)).postOrderTraversal(root);
}

@BeforeTemplate
Iterable<N> before2(N root) {
return new TreeTraverser<N>() {
@Override
public Iterable<N> children(N node) {
return getChildren(node);
}
}.postOrderTraversal(root);
}

@AfterTemplate
Iterable<N> after(N root) {
return Traverser.forTree((N node) -> getChildren(node)).depthFirstPostOrder(root);
}
}

abstract class TreeTraverserBreadthFirst<N> {
@Placeholder
abstract Iterable<N> getChildren(N node);

@BeforeTemplate
Iterable<N> before1(N root) {
return TreeTraverser.using((N node) -> getChildren(node)).breadthFirstTraversal(root);
}

@BeforeTemplate
Iterable<N> before2(N root) {
return new TreeTraverser<N>() {
@Override
public Iterable<N> children(N node) {
return getChildren(node);
}
}.breadthFirstTraversal(root);
}

@AfterTemplate
Iterable<N> after(N root) {
return Traverser.forTree((N node) -> getChildren(node)).breadthFirst(root);
}
}
}

0 comments on commit ddaf710

Please sign in to comment.
You can’t perform that action at this time.