/*
 * Copyright 2016, Google Inc.
 * 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.
 */

package com.google.instrumentation.stats;

/**
 * Tag keys.
 *
 * <p>TagKey's are {@link String}s with enforced restrictions.
 */
public final class TagKey {
  public static final int MAX_LENGTH = StringUtil.MAX_LENGTH;

  /**
   * Constructs a new {@link TagKey} from the given string. The string will be sanitize such that:
   * <ol>
   * <li>length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated.
   * <li>characters are restricted to printable ascii characters, non-printable characters will be
   * replaced by an underscore '_'.
   * </ol>
   */
  public static TagKey create(String key) {
    return new TagKey(key);
  }

  @Override
  public boolean equals(Object obj) {
    return (obj instanceof TagKey) && key.equals(((TagKey) obj).key);
  }

  @Override
  public int hashCode() {
    return key.hashCode();
  }

  @Override
  public String toString() {
    return key;
  }

  private final String key;

  private TagKey(String key) {
    this.key = StringUtil.sanitize(key);
  }
}

@@ -624,16 +624,57 @@
 stats;%0A%0A
+import com.google.auto.value.AutoValue;%0A%0A
 /**%0A * T
@@ -756,20 +756,34 @@
  */%0A
-public final
+@AutoValue%0Apublic abstract
  cla
@@ -857,16 +857,31 @@
 ENGTH;%0A%0A
+  TagKey() %7B%7D%0A%0A
   /**%0A  
@@ -1299,19 +1299,50 @@
 new 
-TagKey
+AutoValue_TagKey(StringUtil.sanitize
 (key)
+)
 ;%0A  
@@ -1350,200 +1350,137 @@
 %0A%0A  
-@Override%0A  public boolean equals(Object obj) %7B%0A    return (obj instanceof TagKey) && key.equals(((TagKey) obj).key
+public abstract String asString(
 );%0A
+%0A
   
-%7D%0A%0A  @Override%0A  public int hashCode() %7B%0A    return key.hashCode();%0A  %7D%0A
+// TODO(sebright) Use the default AutoValue toString(), which is more useful for debugging.
 %0A  @
@@ -1497,16 +1497,22 @@
  public 
+final 
 String t
@@ -1538,116 +1538,17 @@
 urn 
-key;%0A  %7D%0A%0A  private final String key;%0A%0A  private TagKey(String key) %7B%0A    this.key = StringUtil.sanitize(key
+asString(
 );%0A 

Apply AutoValue to TagKey.