// Copyright (C) 2009 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.google.gerrit.sshd;

import com.google.inject.Key;

import org.apache.sshd.server.Command;

import java.lang.annotation.Annotation;

/** Utilities to support {@link CommandName} construction. */
public class Commands {
  /** Magic value signaling the top level. */
  public static final String ROOT = "";

  /** Magic value signaling the top level. */
  public static final CommandName CMD_ROOT = named(ROOT);

  public static Key<Command> key(final String name) {
    return key(named(name));
  }

  public static Key<Command> key(final CommandName name) {
    return Key.get(Command.class, name);
  }

  public static Key<Command> key(final CommandName parent,
      final String name) {
    return Key.get(Command.class, named(parent, name));
  }

  public static Key<Command> key(final CommandName parent,
      final String name, final String descr) {
    return Key.get(Command.class, named(parent, name, descr));
  }

  /** Create a CommandName annotation for the supplied name. */
  public static CommandName named(final String name) {
    return new CommandName() {
      @Override
      public String value() {
        return name;
      }

      @Override
      public Class<? extends Annotation> annotationType() {
        return CommandName.class;
      }

      @Override
      public int hashCode() {
        // This is specified in java.lang.Annotation.
        return (127 * "value".hashCode()) ^ value().hashCode();
      }

      @Override
      public boolean equals(final Object obj) {
        return obj instanceof CommandName
            && value().equals(((CommandName) obj).value());
      }

      @Override
      public String toString() {
        return "@" + CommandName.class.getName() + "(value=" + value() + ")";
      }
    };
  }

  /** Create a CommandName annotation for the supplied name. */
  public static CommandName named(final CommandName parent, final String name) {
    return new NestedCommandNameImpl(parent, name);
  }

  /** Create a CommandName annotation for the supplied name and description. */
  public static CommandName named(final CommandName parent, final String name,
      final String descr) {
    return new NestedCommandNameImpl(parent, name, descr);
  }

  /** Return the name of this command, possibly including any parents. */
  public static String nameOf(final CommandName name) {
    if (name instanceof NestedCommandNameImpl) {
      return nameOf(((NestedCommandNameImpl) name).parent) + " " + name.value();
    }
    return name.value();
  }

  /** Is the second command a direct child of the first command? */
  public static boolean isChild(final CommandName parent, final CommandName name) {
    if (name instanceof NestedCommandNameImpl) {
      return parent.equals(((NestedCommandNameImpl) name).parent);
    }
    if (parent == CMD_ROOT) {
      return true;
    }
    return false;
  }

  static CommandName parentOf(CommandName name) {
    if (name instanceof NestedCommandNameImpl) {
      return ((NestedCommandNameImpl) name).parent;
    }
    return null;
  }

  static final class NestedCommandNameImpl implements CommandName {
    private final CommandName parent;
    private final String name;
    private final String descr;

    NestedCommandNameImpl(final CommandName parent, final String name) {
      this.parent = parent;
      this.name = name;
      this.descr = "";
    }

    NestedCommandNameImpl(final CommandName parent, final String name,
        final String descr) {
      this.parent = parent;
      this.name = name;
      this.descr = descr;
    }

    @Override
    public String value() {
      return name;
    }

    public String descr() {
      return descr;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
      return CommandName.class;
    }

    @Override
    public int hashCode() {
      return parent.hashCode() * 31 + value().hashCode();
    }

    @Override
    public boolean equals(final Object obj) {
      return obj instanceof NestedCommandNameImpl
          && parent.equals(((NestedCommandNameImpl) obj).parent)
          && value().equals(((NestedCommandNameImpl) obj).value());
    }

    @Override
    public String toString() {
      return "CommandName[" + nameOf(this) + "]";
    }
  }

  private Commands() {
  }
}

@@ -631,24 +631,69 @@
 rrit.sshd;%0A%0A
+import com.google.auto.value.AutoAnnotation;%0A
 import com.g
@@ -1654,528 +1654,84 @@
 /%0A  
-public static CommandName named(final String name) %7B%0A    return new CommandName() %7B%0A      @Override%0A      public String value() %7B%0A        return name;%0A      %7D%0A%0A      @Override%0A      public Class%3C? extends Annotation%3E annotationType() %7B%0A        return CommandName.class;%0A      %7D%0A%0A      @Override%0A      public int hashCode() %7B%0A        // This is specified in java.lang.Annotation.%0A        return (127 * %22value%22.hashCode()) %5E value().hashCode();%0A      %7D%0A%0A      @Override%0A      public boolean equals(final Object obj
+@AutoAnnotation%0A  public static CommandName named(final String value
 ) %7B%0A    
     
@@ -1730,255 +1730,55 @@
     
- 
-   
 return 
-obj instanceof CommandName%0A            && value().equals(((CommandName) obj).value());%0A      %7D%0A%0A      @Override%0A      public String toString() %7B%0A        return %22@%22 + CommandName.class.getName() + %22(value=%22 + value() + %22)%22;%0A      %7D%0A    %7D
+new AutoAnnotation_Commands_named(value)
 ;%0A  

Use @AutoAnnotation