Merge "Fixed sonar issue in InputIterator.java"
[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, instructions;
37     private static AuthzTrans trans;
38     
39     public InputIterator(BufferedReader in, PrintStream out, String prompt, String instructions) {
40         this.in = in;
41         this.out = out;
42         this.prompt = prompt;
43         this.instructions = instructions;
44     }
45     
46     @Override
47     public Iterator<String> iterator() {
48         out.println(instructions);
49         return new Iterator<String>() {
50             String input;
51             @Override
52             public boolean hasNext() {
53                 out.append(prompt);
54                 try {
55                     input = in.readLine();
56                 } catch (IOException e) {
57                     trans.error().log("IO Exception",e.getMessage());
58                     input = null;
59                     return false;
60                 }
61                 return input.length()>0;
62             }
63
64             @Override
65             public String next() {
66                 if (!hasNext()) {
67                     throw new NoSuchElementException();
68                 }
69                 return input;
70             }
71
72             @Override
73             public void remove() {
74             }
75         };
76     }
77 }
78