Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / InputIterator.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  *
21  */
22
23 package org.onap.aaf.auth.batch.helpers;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.util.Iterator;
29 import java.util.NoSuchElementException;
30 import org.onap.aaf.auth.env.AuthzTrans;
31
32
33 public class InputIterator implements Iterable<String> {
34     private BufferedReader in;
35     private final PrintStream out;
36     private final String prompt;
37     private final String instructions;
38     private static AuthzTrans trans;
39     
40     public InputIterator(BufferedReader in, PrintStream out, String prompt, String instructions) {
41         this.in = in;
42         this.out = out;
43         this.prompt = prompt;
44         this.instructions = instructions;
45     }
46     
47     @Override
48     public Iterator<String> iterator() {
49         out.println(instructions);
50         return new Iterator<String>() {
51             String input;
52             @Override
53             public boolean hasNext() {
54                 out.append(prompt);
55                 try {
56                     input = in.readLine();
57                 } catch (IOException e) {
58                     trans.error().log("IO Exception",e.getMessage());
59                     input = null;
60                     return false;
61                 }
62                 return input.length()>0;
63             }
64
65             @Override
66             public String next() {
67                 if (!hasNext()) {
68                     throw new NoSuchElementException();
69                 }
70                 return input;
71             }
72
73             @Override
74             public void remove() {
75                 // To Do
76             }
77         };
78     }
79 }
80