/*
 * 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;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

/**
 * Native Implementation of {@link StatsContext}.
 */
final class StatsContextImpl extends StatsContext {
  final HashMap<String, String> tags;

  StatsContextImpl(HashMap<String, String> tags) {
    this.tags = tags;
  }

  @Override
  public Builder builder() {
    return new Builder(tags);
  }

  @Override
  public StatsContextImpl record(MeasurementMap stats) {
    return this;
  }

  /**
   * Serializes a {@link StatsContextImpl} into {@code CensusContextProto} serialized format.
   *
   * <p>The encoded tags are of the form: {@code <version_id><encoded_tags>}
   */
  @Override
  public void serialize(OutputStream output) throws IOException {
    StatsSerializer.serialize(this, output);
  }

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

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

  @Override
  public String toString() {
    return tags.toString();
  }

  private static final class Builder extends StatsContext.Builder {
    private final HashMap<String, String> tags;

    private Builder(HashMap<String, String> tags) {
      this.tags = new HashMap<String, String>(tags);
    }

    @Override
    public Builder set(TagKey key, TagValue value) {
      tags.put(key.toString(), value.toString());
      return this;
    }

    @Override
    public StatsContext build() {
      return new StatsContextImpl(new HashMap<String, String>(tags));
    }
  }
}

@@ -698,20 +698,72 @@
 va.util.
-Hash
+Collections;%0Aimport java.util.HashMap;%0Aimport java.util.
 Map;%0A%0A/*
@@ -874,28 +874,24 @@
 t %7B%0A  final 
-Hash
 Map%3CString, 
@@ -924,20 +924,16 @@
 extImpl(
-Hash
 Map%3CStri
@@ -1891,20 +1891,16 @@
 Builder(
-Hash
 Map%3CStri
@@ -2204,16 +2204,44 @@
 extImpl(
+Collections.unmodifiableMap(
 new Hash
@@ -2266,16 +2266,17 @@
 %3E(tags))
+)
 ;%0A    %7D%0A

Store StatsContextImpl's tags in an unmodifiable map.