// Copyright (C) 2014 The Android Open Source Project
//
// 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.googlesource.gerrit.plugins.xdocs;

import com.google.common.base.Objects;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.reviewdb.client.Project;

import org.eclipse.jgit.lib.ObjectId;

public class XDocResourceKey {
  private final String formatter;
  private final Project.NameKey project;
  private final String resource;
  private final ObjectId revId;
  private final ObjectId metaConfigRevId;

  XDocResourceKey(String formatter, Project.NameKey project, String r,
      ObjectId revId, ObjectId metaConfigRevId) {
    this.formatter = formatter;
    this.project = project;
    this.resource = r;
    this.revId = revId;
    this.metaConfigRevId = metaConfigRevId;
  }

  public String getFormatter() {
    return formatter;
  }

  public Project.NameKey getProject() {
    return project;
  }

  public String getResource() {
    return resource;
  }

  public ObjectId getRevId() {
    return revId;
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(formatter, project, resource, revId);
  }

  @Override
  public boolean equals(Object other) {
    if (other instanceof XDocResourceKey) {
      XDocResourceKey rk = (XDocResourceKey) other;
      return formatter.equals(rk.formatter) && project.equals(rk.project)
          && resource.equals(rk.resource) && revId.equals(rk.revId);
    }
    return false;
  }

  public String asString() {
    StringBuilder b = new StringBuilder();
    b.append(IdString.fromDecoded(formatter).encoded());
    b.append("/");
    b.append(IdString.fromDecoded(project.get()).encoded());
    b.append("/");
    b.append(resource != null ? IdString.fromDecoded(resource).encoded() : "");
    b.append("/");
    b.append(revId != null ? revId.name() : "");
    b.append("/");
    b.append(metaConfigRevId != null ? metaConfigRevId.name() : "");
    return b.toString();
  }

  public static XDocResourceKey fromString(String str) {
    String[] s = str.split("/");
    String formatter = null;
    String project = null;
    String file = null;
    String revision = null;
    String metaConfigRevision = null;
    if (s.length > 0) {
      formatter = IdString.fromUrl(s[0]).get();
    }
    if (s.length > 1) {
      project = IdString.fromUrl(s[1]).get();
    }
    if (s.length > 2) {
      file = IdString.fromUrl(s[2]).get();
    }
    if (s.length > 3) {
      revision = s[3];
    }
    if (s.length > 4) {
      metaConfigRevision = s[4];
    }
    return new XDocResourceKey(formatter, new Project.NameKey(project), file,
        toObjectId(revision), toObjectId(metaConfigRevision));
  }

  private static ObjectId toObjectId(String id) {
    return id != null ? ObjectId.fromString(id) : null;
  }
}

@@ -654,47 +654,8 @@
 s;%0A%0A
-import com.google.common.base.Objects;%0A
 impo
@@ -794,16 +794,43 @@
 ectId;%0A%0A
+import java.util.Objects;%0A%0A
 public c
@@ -1610,20 +1610,16 @@
 cts.hash
-Code
 (formatt
@@ -1642,24 +1642,41 @@
 ource, revId
+, metaConfigRevId
 );%0A  %7D%0A%0A  @O
@@ -1836,25 +1836,34 @@
 urn 
-formatter.equals(
+Objects.equals(formatter, 
 rk.f
@@ -1875,27 +1875,37 @@
 ter)
+%0A         
  && 
-pro
+Ob
 ject
+s
 .equals(
 rk.p
@@ -1900,16 +1900,25 @@
 .equals(
+project, 
 rk.proje
@@ -1938,24 +1938,23 @@
  && 
-resource
+Objects
 .equals(
 rk.r
@@ -1945,24 +1945,34 @@
 ects.equals(
+resource, 
 rk.resource)
@@ -1975,29 +1975,113 @@
 rce)
- && revId.equals(rk.r
+%0A          && Objects.equals(revId, rk.revId)%0A          && Objects.equals(metaConfigRevId, rk.metaConfigR
 evId

Fix implementation of equals(...) and hashCode() in XDocResourceKey