2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.netconf.cli.reader.impl;
10 import static org.opendaylight.controller.netconf.cli.io.IOUtil.SKIP;
12 import com.google.common.base.Optional;
13 import java.io.IOException;
14 import jline.console.completer.AggregateCompleter;
15 import jline.console.completer.Completer;
16 import jline.console.completer.StringsCompleter;
17 import jline.internal.Log;
18 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
19 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
20 import org.opendaylight.controller.netconf.cli.io.IOUtil;
21 import org.opendaylight.controller.netconf.cli.reader.ReadingException;
23 public class DecisionReader {
25 private static final String YES = "Y";
26 private static final String NO = "N";
27 public static final Completer YES_NO_COMPLETER = new StringsCompleter(YES, NO);
29 public Optional<Boolean> read(final ConsoleIO console, final String questionMessageBlueprint,
30 final Object... questionMessageArgs) throws IOException, ReadingException {
31 final ConsoleContext ctx = getContext();
32 console.enterContext(ctx);
34 console.formatLn(questionMessageBlueprint, questionMessageArgs);
35 final String rawValue = console.read();
36 if (YES.equals(rawValue.toUpperCase())) {
37 return Optional.of(Boolean.TRUE);
38 } else if (NO.equals(rawValue.toUpperCase())) {
39 return Optional.of(Boolean.FALSE);
40 } else if (SKIP.equals(rawValue)) {
41 return Optional.absent();
43 final String message = String.format("Incorrect possibility (%s) was selected", rawValue);
45 throw new ReadingException(message);
48 console.leaveContext();
52 private static ConsoleContext getContext() {
53 return new ConsoleContext() {
56 public Optional<String> getPrompt() {
57 return Optional.absent();
61 public Completer getCompleter() {
62 return new AggregateCompleter(YES_NO_COMPLETER, new StringsCompleter(IOUtil.SKIP));