summaryrefslogtreecommitdiffstats
path: root/gerrit-sshd/src/main/java/com/google/gerrit/sshd/Commands.java
blob: 43d2c5033a23645a9bfa035ee103b4016702a748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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.auto.value.AutoAnnotation;
import com.google.inject.Key;
import java.lang.annotation.Annotation;
import org.apache.sshd.server.Command;

/** 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(String name) {
    return key(named(name));
  }

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

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

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

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

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

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

  /** Return the name of this command, possibly including any parents. */
  public static String nameOf(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(CommandName parent, 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(CommandName parent, String name) {
      this.parent = parent;
      this.name = name;
      this.descr = "";
    }

    NestedCommandNameImpl(CommandName parent, String name, 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(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() {}
}