LCOV - code coverage report
Current view: top level - sshd - Commands.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 37 81.1 %
Date: 2022-11-19 15:00:39 Functions: 16 19 84.2 %

          Line data    Source code
       1             : // Copyright (C) 2009 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.sshd;
      16             : 
      17             : import com.google.auto.value.AutoAnnotation;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.inject.Key;
      20             : import java.lang.annotation.Annotation;
      21             : import org.apache.sshd.server.command.Command;
      22             : 
      23             : /** Utilities to support {@link CommandName} construction. */
      24             : public class Commands {
      25             :   /** Magic value signaling the top level. */
      26             :   public static final String ROOT = "";
      27             : 
      28             :   /** Magic value signaling the top level. */
      29          17 :   public static final CommandName CMD_ROOT = named(ROOT);
      30             : 
      31             :   public static Key<Command> key(String name) {
      32          17 :     return key(named(name));
      33             :   }
      34             : 
      35             :   public static Key<Command> key(CommandName name) {
      36          17 :     return Key.get(Command.class, name);
      37             :   }
      38             : 
      39             :   public static Key<Command> key(CommandName parent, String name) {
      40          17 :     return Key.get(Command.class, named(parent, name));
      41             :   }
      42             : 
      43             :   public static Key<Command> key(CommandName parent, String name, String descr) {
      44          17 :     return Key.get(Command.class, named(parent, name, descr));
      45             :   }
      46             : 
      47             :   /** Create a CommandName annotation for the supplied name. */
      48             :   @AutoAnnotation
      49             :   public static CommandName named(String value) {
      50          17 :     return new AutoAnnotation_Commands_named(value);
      51             :   }
      52             : 
      53             :   /** Create a CommandName annotation for the supplied name. */
      54             :   public static CommandName named(CommandName parent, String name) {
      55          17 :     return new NestedCommandNameImpl(parent, name);
      56             :   }
      57             : 
      58             :   /** Create a CommandName annotation for the supplied name and description. */
      59             :   public static CommandName named(CommandName parent, String name, String descr) {
      60          17 :     return new NestedCommandNameImpl(parent, name, descr);
      61             :   }
      62             : 
      63             :   /** Return the name of this command, possibly including any parents. */
      64             :   public static String nameOf(CommandName name) {
      65           0 :     if (name instanceof NestedCommandNameImpl) {
      66           0 :       return nameOf(((NestedCommandNameImpl) name).parent) + " " + name.value();
      67             :     }
      68           0 :     return name.value();
      69             :   }
      70             : 
      71             :   /** Is the second command a direct child of the first command? */
      72             :   public static boolean isChild(CommandName parent, CommandName name) {
      73           9 :     if (name instanceof NestedCommandNameImpl) {
      74           9 :       return parent.equals(((NestedCommandNameImpl) name).parent);
      75             :     }
      76           9 :     if (parent == CMD_ROOT) {
      77           9 :       return true;
      78             :     }
      79           5 :     return false;
      80             :   }
      81             : 
      82             :   @Nullable
      83             :   static CommandName parentOf(CommandName name) {
      84           0 :     if (name instanceof NestedCommandNameImpl) {
      85           0 :       return ((NestedCommandNameImpl) name).parent;
      86             :     }
      87           0 :     return null;
      88             :   }
      89             : 
      90             :   static final class NestedCommandNameImpl implements CommandName {
      91             :     private final CommandName parent;
      92             :     private final String name;
      93             :     private final String descr;
      94             : 
      95          17 :     NestedCommandNameImpl(CommandName parent, String name) {
      96          17 :       this.parent = parent;
      97          17 :       this.name = name;
      98          17 :       this.descr = "";
      99          17 :     }
     100             : 
     101          17 :     NestedCommandNameImpl(CommandName parent, String name, String descr) {
     102          17 :       this.parent = parent;
     103          17 :       this.name = name;
     104          17 :       this.descr = descr;
     105          17 :     }
     106             : 
     107             :     @Override
     108             :     public String value() {
     109          17 :       return name;
     110             :     }
     111             : 
     112             :     public String descr() {
     113           5 :       return descr;
     114             :     }
     115             : 
     116             :     @Override
     117             :     public Class<? extends Annotation> annotationType() {
     118          17 :       return CommandName.class;
     119             :     }
     120             : 
     121             :     @Override
     122             :     public int hashCode() {
     123          17 :       return parent.hashCode() * 31 + value().hashCode();
     124             :     }
     125             : 
     126             :     @Override
     127             :     public boolean equals(Object obj) {
     128          17 :       return obj instanceof NestedCommandNameImpl
     129          17 :           && parent.equals(((NestedCommandNameImpl) obj).parent)
     130          17 :           && value().equals(((NestedCommandNameImpl) obj).value());
     131             :     }
     132             : 
     133             :     @Override
     134             :     public String toString() {
     135           0 :       return "CommandName[" + nameOf(this) + "]";
     136             :     }
     137             :   }
     138             : 
     139             :   private Commands() {}
     140             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750