org.apache.accumulo.shell.Token Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.accumulo.shell;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* A token is a word in a command in the shell. The tree that this builds is used for tab-completion
* of tables, users, commands and certain other parts of the shell that can be realistically and
* quickly gathered. Tokens can have multiple commands grouped together and many possible
* subcommands, although they are stored in a set so duplicates aren't allowed.
*/
public class Token {
private Set command = new HashSet<>();
private Set subcommands = new HashSet<>();
private boolean caseSensitive = false;
public Token() {}
public Token(String commandName) {
this();
command.add(commandName);
}
public Token(Collection commandNames) {
this();
command.addAll(commandNames);
}
public void setCaseSensitive(boolean cs) {
caseSensitive = cs;
}
public boolean getCaseSensitive() {
return caseSensitive;
}
public Set getCommandNames() {
return command;
}
public Set getSubcommandList() {
return subcommands;
}
public Token getSubcommand(String name) {
for (Token t : subcommands) {
if (t.containsCommand(name))
return t;
}
return null;
}
public Set getSubcommandNames() {
HashSet set = new HashSet<>();
for (Token t : subcommands)
set.addAll(t.getCommandNames());
return set;
}
public Set getSubcommandNames(String startsWith) {
Iterator iter = subcommands.iterator();
HashSet set = new HashSet<>();
while (iter.hasNext()) {
Token t = iter.next();
Set subset = t.getCommandNames();
for (String s : subset) {
if (t.getCaseSensitive()) {
if (s.startsWith(startsWith)) {
set.add(s);
}
} else {
if (s.toLowerCase().startsWith(startsWith.toLowerCase())) {
set.add(s);
}
}
}
}
return set;
}
public boolean containsCommand(String match) {
for (String t : command) {
if (caseSensitive) {
if (t.equals(match))
return true;
} else {
if (t.equalsIgnoreCase(match))
return true;
}
}
return false;
}
public void addSubcommand(Token t) {
subcommands.add(t);
}
public void addSubcommand(Collection t) {
for (String a : t) {
addSubcommand(new Token(a));
}
}
@Override
public String toString() {
return this.command.toString();
}
}