beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bchamb...@apache.org
Subject [1/4] beam git commit: Move name utilities from StringUtils to NameUtils
Date Tue, 03 Jan 2017 21:27:59 GMT
Repository: beam
Updated Branches:
  refs/heads/master 0616245e6 -> 6c1e46976


Move name utilities from StringUtils to NameUtils

Add more exhaustive tests for NameUtils


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/38d902be
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/38d902be
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/38d902be

Branch: refs/heads/master
Commit: 38d902be61f2dd05ebb2391c86629d29ed5596f8
Parents: 0616245
Author: bchambers <bchambers@google.com>
Authored: Thu Dec 29 13:01:24 2016 -0800
Committer: bchambers <bchambers@google.com>
Committed: Tue Jan 3 13:15:26 2017 -0800

----------------------------------------------------------------------
 .../core/UnboundedReadFromBoundedSource.java    |   4 +-
 .../beam/runners/dataflow/DataflowRunner.java   |   9 +-
 .../DataflowUnboundedReadFromBoundedSource.java |   6 +-
 .../sdk/io/BoundedReadFromUnboundedSource.java  |   5 +-
 .../main/java/org/apache/beam/sdk/io/Read.java  |   7 +-
 .../apache/beam/sdk/transforms/PTransform.java  |   4 +-
 .../org/apache/beam/sdk/transforms/ParDo.java   |   6 +-
 .../org/apache/beam/sdk/util/NameUtils.java     | 134 +++++++++++++++
 .../org/apache/beam/sdk/util/StringUtils.java   | 100 -----------
 .../org/apache/beam/sdk/values/PValueBase.java  |   4 +-
 .../org/apache/beam/sdk/util/NameUtilsTest.java | 168 +++++++++++++++++++
 .../apache/beam/sdk/util/StringUtilsTest.java   | 100 -----------
 12 files changed, 323 insertions(+), 224 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/runners/core-java/src/main/java/org/apache/beam/runners/core/UnboundedReadFromBoundedSource.java
----------------------------------------------------------------------
diff --git a/runners/core-java/src/main/java/org/apache/beam/runners/core/UnboundedReadFromBoundedSource.java
b/runners/core-java/src/main/java/org/apache/beam/runners/core/UnboundedReadFromBoundedSource.java
index be1793c..645a411 100644
--- a/runners/core-java/src/main/java/org/apache/beam/runners/core/UnboundedReadFromBoundedSource.java
+++ b/runners/core-java/src/main/java/org/apache/beam/runners/core/UnboundedReadFromBoundedSource.java
@@ -19,7 +19,6 @@ package org.apache.beam.runners.core;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.apache.beam.sdk.util.StringUtils.approximateSimpleName;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -50,6 +49,7 @@ import org.apache.beam.sdk.options.PipelineOptions;
 import org.apache.beam.sdk.transforms.PTransform;
 import org.apache.beam.sdk.transforms.display.DisplayData;
 import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.util.PropertyNames;
 import org.apache.beam.sdk.values.PBegin;
 import org.apache.beam.sdk.values.PCollection;
@@ -100,7 +100,7 @@ public class UnboundedReadFromBoundedSource<T> extends PTransform<PBegin,
PColle
 
   @Override
   public String getKindString() {
-    return "Read(" + approximateSimpleName(source.getClass()) + ")";
+    return String.format("Read(%s)", NameUtils.approximateSimpleName(source.getClass()));
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
----------------------------------------------------------------------
diff --git a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
index 29c0058..69c9c18 100644
--- a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
+++ b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/DataflowRunner.java
@@ -21,8 +21,6 @@ import static com.google.common.base.MoreObjects.firstNonNull;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.base.Strings.isNullOrEmpty;
-import static org.apache.beam.sdk.util.StringUtils.approximatePTransformName;
-import static org.apache.beam.sdk.util.StringUtils.approximateSimpleName;
 import static org.apache.beam.sdk.util.WindowedValue.valueInEmptyWindows;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
@@ -143,6 +141,7 @@ import org.apache.beam.sdk.util.CoderUtils;
 import org.apache.beam.sdk.util.IOChannelUtils;
 import org.apache.beam.sdk.util.InstanceBuilder;
 import org.apache.beam.sdk.util.MimeTypes;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.util.PCollectionViews;
 import org.apache.beam.sdk.util.PathValidator;
 import org.apache.beam.sdk.util.PropertyNames;
@@ -2309,7 +2308,7 @@ public class DataflowRunner extends PipelineRunner<DataflowPipelineJob>
{
 
     @Override
     public String getKindString() {
-      return "Read(" + approximateSimpleName(source.getClass()) + ")";
+      return String.format("Read(%s)", NameUtils.approximateSimpleName(source.getClass()));
     }
 
     static {
@@ -2785,8 +2784,8 @@ public class DataflowRunner extends PipelineRunner<DataflowPipelineJob>
{
           ? "streaming" : "batch";
       String name =
           transform == null
-              ? approximateSimpleName(doFn.getClass())
-              : approximatePTransformName(transform.getClass());
+              ? NameUtils.approximateSimpleName(doFn.getClass())
+              : NameUtils.approximatePTransformName(transform.getClass());
       throw new UnsupportedOperationException(
           String.format("The DataflowRunner in %s mode does not support %s.", mode, name));
     }

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/internal/DataflowUnboundedReadFromBoundedSource.java
----------------------------------------------------------------------
diff --git a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/internal/DataflowUnboundedReadFromBoundedSource.java
b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/internal/DataflowUnboundedReadFromBoundedSource.java
index 65db817..db87e21 100644
--- a/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/internal/DataflowUnboundedReadFromBoundedSource.java
+++ b/runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/internal/DataflowUnboundedReadFromBoundedSource.java
@@ -19,7 +19,6 @@ package org.apache.beam.runners.dataflow.internal;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.apache.beam.sdk.util.StringUtils.approximateSimpleName;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
@@ -50,6 +49,7 @@ import org.apache.beam.sdk.options.PipelineOptions;
 import org.apache.beam.sdk.transforms.PTransform;
 import org.apache.beam.sdk.transforms.display.DisplayData;
 import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.util.PropertyNames;
 import org.apache.beam.sdk.values.PBegin;
 import org.apache.beam.sdk.values.PCollection;
@@ -109,10 +109,10 @@ public class DataflowUnboundedReadFromBoundedSource<T> extends
PTransform<PBegin
     if (source.getClass().isAnonymousClass()) {
       sourceName = "AnonymousSource";
     } else {
-      sourceName = approximateSimpleName(source.getClass());
+      sourceName = NameUtils.approximateSimpleName(source.getClass());
     }
 
-    return "Read(" + sourceName + ")";
+    return String.format("Read(%s)", sourceName);
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/io/BoundedReadFromUnboundedSource.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/io/BoundedReadFromUnboundedSource.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/io/BoundedReadFromUnboundedSource.java
index f568d86..ac84c5e 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/io/BoundedReadFromUnboundedSource.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/io/BoundedReadFromUnboundedSource.java
@@ -17,8 +17,6 @@
  */
 package org.apache.beam.sdk.io;
 
-import static org.apache.beam.sdk.util.StringUtils.approximateSimpleName;
-
 import com.google.api.client.util.BackOff;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.io.IOException;
@@ -36,6 +34,7 @@ import org.apache.beam.sdk.transforms.ParDo;
 import org.apache.beam.sdk.transforms.SerializableFunction;
 import org.apache.beam.sdk.transforms.display.DisplayData;
 import org.apache.beam.sdk.util.FluentBackoff;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.util.ValueWithRecordId;
 import org.apache.beam.sdk.values.PBegin;
 import org.apache.beam.sdk.values.PCollection;
@@ -118,7 +117,7 @@ public class BoundedReadFromUnboundedSource<T> extends PTransform<PBegin,
PColle
 
   @Override
   public String getKindString() {
-    return "Read(" + approximateSimpleName(source.getClass()) + ")";
+    return "Read(" + NameUtils.approximateSimpleName(source.getClass()) + ")";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/io/Read.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/io/Read.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/io/Read.java
index 7ec3b0e..7404cba 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/io/Read.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/io/Read.java
@@ -17,12 +17,11 @@
  */
 package org.apache.beam.sdk.io;
 
-import static org.apache.beam.sdk.util.StringUtils.approximateSimpleName;
-
 import javax.annotation.Nullable;
 import org.apache.beam.sdk.coders.Coder;
 import org.apache.beam.sdk.transforms.PTransform;
 import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.util.SerializableUtils;
 import org.apache.beam.sdk.util.WindowingStrategy;
 import org.apache.beam.sdk.values.PBegin;
@@ -118,7 +117,7 @@ public class Read {
 
     @Override
     public String getKindString() {
-      return "Read(" + approximateSimpleName(source.getClass()) + ")";
+      return "Read(" + NameUtils.approximateSimpleName(source.getClass()) + ")";
     }
 
     @Override
@@ -185,7 +184,7 @@ public class Read {
 
     @Override
     public String getKindString() {
-      return "Read(" + approximateSimpleName(source.getClass()) + ")";
+      return String.format("Read(%s)", NameUtils.approximateSimpleName(source.getClass()));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java
index ce4891d..efe4339 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/PTransform.java
@@ -25,7 +25,7 @@ import org.apache.beam.sdk.coders.CannotProvideCoderException;
 import org.apache.beam.sdk.coders.Coder;
 import org.apache.beam.sdk.transforms.display.DisplayData.Builder;
 import org.apache.beam.sdk.transforms.display.HasDisplayData;
-import org.apache.beam.sdk.util.StringUtils;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.values.PInput;
 import org.apache.beam.sdk.values.POutput;
 import org.apache.beam.sdk.values.TypedPValue;
@@ -243,7 +243,7 @@ public abstract class PTransform<InputT extends PInput, OutputT extends
POutput>
     if (getClass().isAnonymousClass()) {
       return "AnonymousTransform";
     } else {
-      return StringUtils.approximatePTransformName(getClass());
+      return NameUtils.approximatePTransformName(getClass());
     }
   }
 

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ParDo.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ParDo.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ParDo.java
index f897f82..059effd 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ParDo.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/ParDo.java
@@ -36,8 +36,8 @@ import org.apache.beam.sdk.transforms.reflect.DoFnSignature.OnTimerMethod;
 import org.apache.beam.sdk.transforms.reflect.DoFnSignatures;
 import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
 import org.apache.beam.sdk.transforms.windowing.WindowFn;
+import org.apache.beam.sdk.util.NameUtils;
 import org.apache.beam.sdk.util.SerializableUtils;
-import org.apache.beam.sdk.util.StringUtils;
 import org.apache.beam.sdk.values.PCollection;
 import org.apache.beam.sdk.values.PCollectionTuple;
 import org.apache.beam.sdk.values.PCollectionView;
@@ -772,7 +772,7 @@ public class ParDo {
       if (clazz.isAnonymousClass()) {
         return "AnonymousParDo";
       } else {
-        return String.format("ParDo(%s)", StringUtils.approximateSimpleName(clazz));
+        return String.format("ParDo(%s)", NameUtils.approximateSimpleName(clazz));
       }
     }
 
@@ -994,7 +994,7 @@ public class ParDo {
       if (clazz.isAnonymousClass()) {
         return "AnonymousParMultiDo";
       } else {
-        return String.format("ParMultiDo(%s)", StringUtils.approximateSimpleName(clazz));
+        return String.format("ParMultiDo(%s)", NameUtils.approximateSimpleName(clazz));
       }
     }
 

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/util/NameUtils.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/NameUtils.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/NameUtils.java
new file mode 100644
index 0000000..60a0e41
--- /dev/null
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/NameUtils.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.beam.sdk.util;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.beam.sdk.transforms.Combine.CombineFn;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+
+/**
+ * Helpers for extracting the name of objects (most commonly {@link DoFn} and {@link CombineFn}).
+ */
+public class NameUtils {
+
+  private static final String[] STANDARD_NAME_SUFFIXES =
+      new String[]{"OldDoFn", "DoFn", "Fn"};
+
+  /**
+   * Pattern to match a non-anonymous inner class.
+   * Eg, matches "Foo$Bar", or even "Foo$1$Bar", but not "Foo$1" or "Foo$1$2".
+   */
+  private static final Pattern NAMED_INNER_CLASS =
+      Pattern.compile(".+\\$(?<INNER>[^0-9].*)");
+
+  private static final String ANONYMOUS_CLASS_REGEX = "\\$[0-9]+\\$";
+
+  private static String approximateSimpleName(Class<?> clazz, boolean dropOuterClassNames)
{
+    checkArgument(!clazz.isAnonymousClass(),
+        "Attempted to get simple name of anonymous class");
+    return approximateSimpleName(clazz.getName(), dropOuterClassNames);
+  }
+
+  @VisibleForTesting
+  static String approximateSimpleName(String fullName, boolean dropOuterClassNames) {
+    String shortName = fullName.substring(fullName.lastIndexOf('.') + 1);
+
+    // Drop common suffixes for each named component.
+    String[] names = shortName.split("\\$");
+    for (int i = 0; i < names.length; i++) {
+      names[i] = simplifyNameComponent(names[i]);
+    }
+    shortName = Joiner.on('$').join(names);
+
+    if (dropOuterClassNames) {
+      // Simplify inner class name by dropping outer class prefixes.
+      Matcher m = NAMED_INNER_CLASS.matcher(shortName);
+      if (m.matches()) {
+        shortName = m.group("INNER");
+      }
+    } else {
+      // Dropping anonymous outer classes
+      shortName = shortName.replaceAll(ANONYMOUS_CLASS_REGEX, ".");
+      shortName = shortName.replaceAll("\\$", ".");
+    }
+    return shortName;
+  }
+
+
+  private static String simplifyNameComponent(String name) {
+    for (String suffix : STANDARD_NAME_SUFFIXES) {
+      if (name.endsWith(suffix) && name.length() > suffix.length()) {
+        return name.substring(0, name.length() - suffix.length());
+      }
+    }
+    return name;
+  }
+
+  /**
+   * Returns a simple name for a class.
+   *
+   * <p>Note: this is non-invertible - the name may be simplified to an
+   * extent that it cannot be mapped back to the original class.
+   *
+   * <p>This can be used to generate human-readable names. It
+   * removes the package and outer classes from the name,
+   * and removes common suffixes.
+   *
+   * <p>Examples:
+   * <ul>
+   *   <li>{@code some.package.Word.SummaryDoFn} becomes "Summary"
+   *   <li>{@code another.package.PairingFn} becomes "Pairing"
+   * </ul>
+   *
+   * @throws IllegalArgumentException if the class is anonymous
+   */
+  public static String approximateSimpleName(Class<?> clazz) {
+    return approximateSimpleName(clazz, /* dropOuterClassNames */ true);
+  }
+
+  /**
+   * Returns a name for a PTransform class.
+   *
+   * <p>This can be used to generate human-readable transform names. It
+   * removes the package from the name, and removes common suffixes.
+   *
+   * <p>It is different than approximateSimpleName:
+   * <ul>
+   *   <li>1. It keeps the outer classes names.
+   *   <li>2. It removes the common transform inner class: "Bound".
+   * </ul>
+   *
+   * <p>Examples:
+   * <ul>
+   *   <li>{@code some.package.Word.Summary} becomes "Word.Summary"
+   *   <li>{@code another.package.Pairing.Bound} becomes "Pairing"
+   * </ul>
+   */
+  public static String approximatePTransformName(Class<?> clazz) {
+    checkArgument(PTransform.class.isAssignableFrom(clazz));
+    return approximateSimpleName(clazz, /* dropOuterClassNames */ false)
+        .replaceFirst("\\.Bound$", "");
+  }
+}

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/util/StringUtils.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/StringUtils.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/StringUtils.java
index 3ff8448..993353d 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/StringUtils.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/StringUtils.java
@@ -17,15 +17,10 @@
  */
 package org.apache.beam.sdk.util;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
-import com.google.common.base.Joiner;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import org.apache.beam.sdk.transforms.PTransform;
 
 /**
  * Utilities for working with JSON and other human-readable string formats.
@@ -95,64 +90,6 @@ public class StringUtils {
     return byteArray;
   }
 
-  private static final String[] STANDARD_NAME_SUFFIXES =
-      new String[]{"OldDoFn", "DoFn", "Fn"};
-
-  /**
-   * Pattern to match a non-anonymous inner class.
-   * Eg, matches "Foo$Bar", or even "Foo$1$Bar", but not "Foo$1" or "Foo$1$2".
-   */
-  private static final Pattern NAMED_INNER_CLASS =
-      Pattern.compile(".+\\$(?<INNER>[^0-9].*)");
-
-  private static final String ANONYMOUS_CLASS_REGEX = "\\$[0-9]+\\$";
-
-  /**
-   * Returns a simple name for a class.
-   *
-   * <p>Note: this is non-invertible - the name may be simplified to an
-   * extent that it cannot be mapped back to the original class.
-   *
-   * <p>This can be used to generate human-readable names. It
-   * removes the package and outer classes from the name,
-   * and removes common suffixes.
-   *
-   * <p>Examples:
-   * <ul>
-   *   <li>{@code some.package.Word.SummaryDoFn} becomes "Summary"
-   *   <li>{@code another.package.PairingFn} becomes "Pairing"
-   * </ul>
-   *
-   * @throws IllegalArgumentException if the class is anonymous
-   */
-  public static String approximateSimpleName(Class<?> clazz) {
-    return approximateSimpleName(clazz, /* dropOuterClassNames */ true);
-  }
-
-  /**
-   * Returns a name for a PTransform class.
-   *
-   * <p>This can be used to generate human-readable transform names. It
-   * removes the package from the name, and removes common suffixes.
-   *
-   * <p>It is different than approximateSimpleName:
-   * <ul>
-   *   <li>1. It keeps the outer classes names.
-   *   <li>2. It removes the common transform inner class: "Bound".
-   * </ul>
-   *
-   * <p>Examples:
-   * <ul>
-   *   <li>{@code some.package.Word.Summary} becomes "Word.Summary"
-   *   <li>{@code another.package.Pairing.Bound} becomes "Pairing"
-   * </ul>
-   */
-  public static String approximatePTransformName(Class<?> clazz) {
-    checkArgument(PTransform.class.isAssignableFrom(clazz));
-    return approximateSimpleName(clazz, /* dropOuterClassNames */ false)
-        .replaceFirst("\\.Bound$", "");
-  }
-
   /**
    * Calculate the Levenshtein distance between two strings.
    *
@@ -204,41 +141,4 @@ public class StringUtils {
 
     return v1[t.length()];
   }
-
-  private static String approximateSimpleName(Class<?> clazz, boolean dropOuterClassNames)
{
-    checkArgument(!clazz.isAnonymousClass(),
-        "Attempted to get simple name of anonymous class");
-
-    String fullName = clazz.getName();
-    String shortName = fullName.substring(fullName.lastIndexOf('.') + 1);
-
-    // Drop common suffixes for each named component.
-    String[] names = shortName.split("\\$");
-    for (int i = 0; i < names.length; i++) {
-      names[i] = simplifyNameComponent(names[i]);
-    }
-    shortName = Joiner.on('$').join(names);
-
-    if (dropOuterClassNames) {
-      // Simplify inner class name by dropping outer class prefixes.
-      Matcher m = NAMED_INNER_CLASS.matcher(shortName);
-      if (m.matches()) {
-        shortName = m.group("INNER");
-      }
-    } else {
-      // Dropping anonymous outer classes
-      shortName = shortName.replaceAll(ANONYMOUS_CLASS_REGEX, ".");
-      shortName = shortName.replaceAll("\\$", ".");
-    }
-    return shortName;
-  }
-
-  private static String simplifyNameComponent(String name) {
-    for (String suffix : STANDARD_NAME_SUFFIXES) {
-      if (name.endsWith(suffix) && name.length() > suffix.length()) {
-        return name.substring(0, name.length() - suffix.length());
-      }
-    }
-    return name;
-  }
 }

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PValueBase.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PValueBase.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PValueBase.java
index 7b44737..8778597 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PValueBase.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PValueBase.java
@@ -22,7 +22,7 @@ import java.util.List;
 import org.apache.beam.sdk.Pipeline;
 import org.apache.beam.sdk.transforms.AppliedPTransform;
 import org.apache.beam.sdk.transforms.PTransform;
-import org.apache.beam.sdk.util.StringUtils;
+import org.apache.beam.sdk.util.NameUtils;
 
 /**
  * A {@link PValueBase} is an abstract base class that provides
@@ -155,6 +155,6 @@ public abstract class PValueBase extends POutputValueBase implements PValue
{
    * <p>By default, uses the base name of the current class as its kind string.
    */
   protected String getKindString() {
-    return StringUtils.approximateSimpleName(getClass());
+    return NameUtils.approximateSimpleName(getClass());
   }
 }

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/test/java/org/apache/beam/sdk/util/NameUtilsTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/NameUtilsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/NameUtilsTest.java
new file mode 100644
index 0000000..b35e942
--- /dev/null
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/NameUtilsTest.java
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.beam.sdk.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.beam.sdk.io.TextIO;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.values.PBegin;
+import org.apache.beam.sdk.values.PDone;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Tests for {@link NameUtils}.
+ */
+@RunWith(JUnit4.class)
+public class NameUtilsTest {
+
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
+  @Test
+  public void testDropsStandardSuffixes() {
+    assertEquals("Embedded", NameUtils.approximateSimpleName("EmbeddedOldDoFn", true));
+    assertEquals("Embedded", NameUtils.approximateSimpleName("EmbeddedDoFn", true));
+    assertEquals("Embedded", NameUtils.approximateSimpleName("EmbeddedFn", true));
+
+    assertEquals("Embedded", NameUtils.approximateSimpleName("EmbeddedOldDoFn", false));
+    assertEquals("Embedded", NameUtils.approximateSimpleName("EmbeddedDoFn", false));
+    assertEquals("Embedded", NameUtils.approximateSimpleName("EmbeddedFn", false));
+  }
+
+  @Test
+  public void testDropsStandardSuffixesInAllComponents() {
+    assertEquals("Embedded", NameUtils.approximateSimpleName("SomeOldDoFn$EmbeddedFn", true));
+    assertEquals("Embedded", NameUtils.approximateSimpleName("SomeDoFn$EmbeddedDoFn", true));
+    assertEquals("Embedded", NameUtils.approximateSimpleName("SomeFn$EmbeddedFn", true));
+
+    assertEquals("Some.Embedded", NameUtils.approximateSimpleName("SomeOldDoFn$EmbeddedFn",
false));
+    assertEquals("Some.Embedded", NameUtils.approximateSimpleName("SomeDoFn$EmbeddedDoFn",
false));
+    assertEquals("Some.Embedded", NameUtils.approximateSimpleName("SomeFn$EmbeddedFn", false));
+  }
+
+  @Test
+  public void testDropsOuterClassNamesTrue() {
+    assertEquals("Bar", NameUtils.approximateSimpleName("Foo$1$Bar", true));
+    assertEquals("Foo$1", NameUtils.approximateSimpleName("Foo$1", true));
+    assertEquals("Foo$1$2", NameUtils.approximateSimpleName("Foo$1$2", true));
+  }
+
+  @Test
+  public void testDropsOuterClassNamesFalse() {
+    assertEquals("Foo.Bar", NameUtils.approximateSimpleName("Foo$1$Bar", false));
+    assertEquals("Foo.1", NameUtils.approximateSimpleName("Foo$1", false));
+    assertEquals("Foo.2", NameUtils.approximateSimpleName("Foo$1$2", false));
+  }
+
+  /**
+   * Inner class for simple name test.
+   */
+  private class EmbeddedOldDoFn {
+
+    private class DeeperEmbeddedOldDoFn extends EmbeddedOldDoFn {}
+
+    private EmbeddedOldDoFn getEmbedded() {
+      return new DeeperEmbeddedOldDoFn();
+    }
+  }
+
+  private class EmbeddedPTransform extends PTransform<PBegin, PDone> {
+    @Override
+    public PDone expand(PBegin begin) {
+      throw new IllegalArgumentException("Should never be applied");
+    }
+
+    private class Bound extends PTransform<PBegin, PDone> {
+      @Override
+      public PDone expand(PBegin begin) {
+        throw new IllegalArgumentException("Should never be applied");
+      }
+    }
+
+    private Bound getBound() {
+      return new Bound();
+    }
+  }
+
+  private interface AnonymousClass {
+    Object getInnerClassInstance();
+  }
+
+  @Test
+  public void testSimpleName() {
+    assertEquals("Embedded", NameUtils.approximateSimpleName(EmbeddedOldDoFn.class));
+  }
+
+  @Test
+  public void testAnonSimpleName() throws Exception {
+    thrown.expect(IllegalArgumentException.class);
+
+    EmbeddedOldDoFn anon = new EmbeddedOldDoFn(){};
+
+    NameUtils.approximateSimpleName(anon.getClass());
+  }
+
+  @Test
+  public void testNestedSimpleName() {
+    EmbeddedOldDoFn fn = new EmbeddedOldDoFn();
+    EmbeddedOldDoFn inner = fn.getEmbedded();
+
+    assertEquals("DeeperEmbedded", NameUtils.approximateSimpleName(inner.getClass()));
+  }
+
+  @Test
+  public void testPTransformName() {
+    EmbeddedPTransform transform = new EmbeddedPTransform();
+    assertEquals(
+        "NameUtilsTest.EmbeddedPTransform",
+        NameUtils.approximatePTransformName(transform.getClass()));
+    assertEquals(
+        "NameUtilsTest.EmbeddedPTransform",
+        NameUtils.approximatePTransformName(transform.getBound().getClass()));
+    assertEquals("TextIO.Write", NameUtils.approximatePTransformName(TextIO.Write.Bound.class));
+  }
+
+  @Test
+  public void testPTransformNameWithAnonOuterClass() throws Exception {
+    AnonymousClass anonymousClassObj = new AnonymousClass() {
+      class NamedInnerClass extends PTransform<PBegin, PDone> {
+        @Override
+        public PDone expand(PBegin begin) {
+          throw new IllegalArgumentException("Should never be applied");
+        }
+      }
+
+      @Override
+      public Object getInnerClassInstance() {
+        return new NamedInnerClass();
+      }
+    };
+
+    assertEquals("NamedInnerClass",
+        NameUtils.approximateSimpleName(anonymousClassObj.getInnerClassInstance().getClass()));
+    assertEquals("NameUtilsTest.NamedInnerClass",
+        NameUtils.approximatePTransformName(
+            anonymousClassObj.getInnerClassInstance().getClass()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/beam/blob/38d902be/sdks/java/core/src/test/java/org/apache/beam/sdk/util/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/StringUtilsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/StringUtilsTest.java
index 1ac176b..df6a300 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/StringUtilsTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/StringUtilsTest.java
@@ -20,13 +20,7 @@ package org.apache.beam.sdk.util;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
-import org.apache.beam.sdk.io.TextIO;
-import org.apache.beam.sdk.transforms.PTransform;
-import org.apache.beam.sdk.values.PBegin;
-import org.apache.beam.sdk.values.PDone;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
@@ -35,8 +29,6 @@ import org.junit.runners.JUnit4;
  */
 @RunWith(JUnit4.class)
 public class StringUtilsTest {
-  @Rule
-  public ExpectedException thrown = ExpectedException.none();
 
   @Test
   public void testTranscodeEmptyByteArray() {
@@ -55,98 +47,6 @@ public class StringUtilsTest {
     assertArrayEquals(bytes, StringUtils.jsonStringToByteArray(string));
   }
 
-  /**
-   * Inner class for simple name test.
-   */
-  private class EmbeddedOldDoFn {
-
-    private class DeeperEmbeddedOldDoFn extends EmbeddedOldDoFn {}
-
-    private EmbeddedOldDoFn getEmbedded() {
-      return new DeeperEmbeddedOldDoFn();
-    }
-  }
-
-  private class EmbeddedPTransform extends PTransform<PBegin, PDone> {
-    @Override
-    public PDone expand(PBegin begin) {
-      throw new IllegalArgumentException("Should never be applied");
-    }
-
-    private class Bound extends PTransform<PBegin, PDone> {
-      @Override
-      public PDone expand(PBegin begin) {
-        throw new IllegalArgumentException("Should never be applied");
-      }
-    }
-
-    private Bound getBound() {
-      return new Bound();
-    }
-  }
-
-  private interface AnonymousClass {
-    Object getInnerClassInstance();
-  }
-
-  @Test
-  public void testSimpleName() {
-    assertEquals("Embedded",
-        StringUtils.approximateSimpleName(EmbeddedOldDoFn.class));
-  }
-
-  @Test
-  public void testAnonSimpleName() throws Exception {
-    thrown.expect(IllegalArgumentException.class);
-
-    EmbeddedOldDoFn anon = new EmbeddedOldDoFn(){};
-
-    StringUtils.approximateSimpleName(anon.getClass());
-  }
-
-  @Test
-  public void testNestedSimpleName() {
-    EmbeddedOldDoFn fn = new EmbeddedOldDoFn();
-    EmbeddedOldDoFn inner = fn.getEmbedded();
-
-    assertEquals("DeeperEmbedded", StringUtils.approximateSimpleName(inner.getClass()));
-  }
-
-  @Test
-  public void testPTransformName() {
-    EmbeddedPTransform transform = new EmbeddedPTransform();
-    assertEquals(
-        "StringUtilsTest.EmbeddedPTransform",
-        StringUtils.approximatePTransformName(transform.getClass()));
-    assertEquals(
-        "StringUtilsTest.EmbeddedPTransform",
-        StringUtils.approximatePTransformName(transform.getBound().getClass()));
-    assertEquals("TextIO.Write", StringUtils.approximatePTransformName(TextIO.Write.Bound.class));
-  }
-
-  @Test
-  public void testPTransformNameWithAnonOuterClass() throws Exception {
-    AnonymousClass anonymousClassObj = new AnonymousClass() {
-      class NamedInnerClass extends PTransform<PBegin, PDone> {
-        @Override
-        public PDone expand(PBegin begin) {
-          throw new IllegalArgumentException("Should never be applied");
-        }
-      }
-
-      @Override
-      public Object getInnerClassInstance() {
-        return new NamedInnerClass();
-      }
-    };
-
-    assertEquals("NamedInnerClass",
-        StringUtils.approximateSimpleName(anonymousClassObj.getInnerClassInstance().getClass()));
-    assertEquals("StringUtilsTest.NamedInnerClass",
-        StringUtils.approximatePTransformName(
-            anonymousClassObj.getInnerClassInstance().getClass()));
-  }
-
   @Test
   public void testLevenshteinDistance() {
     assertEquals(0, StringUtils.getLevenshteinDistance("", "")); // equal


Mime
View raw message