All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jlibs.wadl.cli.completors.Buffer Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/**
 * Copyright 2015 Santhosh Kumar Tekuri
 *
 * The JLibs authors license 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:
 *
 *   http://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 jlibs.wadl.cli.completors;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Santhosh Kumar T
 */
public class Buffer{
    private String str;
    private int fromPrev = 0;
    private int from = 0;
    private List candidates;

    public Buffer(String str, int cursor, List candidates){
        this.str = str.substring(0, cursor);
        this.candidates = candidates;
    }

    private int separatorIndex;
    private String arg;
    private List args = new ArrayList();
    
    public String next(){
        fromPrev = from;
        separatorIndex = str.indexOf(' ', from);
        if(separatorIndex==-1)
            arg = str.substring(from);
        else{
            arg = str.substring(from, separatorIndex);
            from = separatorIndex;
            while(str.charAt(from)==' '){
                from++;
                if(from==str.length())
                    break;
            }
        }
        args.add(arg);
        return arg;
    }

    public String arg(int i){
        return args.get(i);
    }
    
    public int getFrom(){
        return candidates.isEmpty() ? -1 : from;
    }

    public void eat(int count){
        fromPrev += count;
        from = fromPrev;
        arg = arg.substring(count);
    }

    public boolean hasNext(){
        return separatorIndex!=-1;
    }
    
    public void addCandidate(String candidate){
        addCandidate(candidate, ' ');
    }

    public void addCandidate(String candidate, char separator){
        if(candidate.startsWith(arg)){
            if(separator==0)
                candidates.add(candidate);
            else
                candidates.add(candidate+separator);
        }
    }

    public void addCandidateIgnoreCase(String candidate){
        if(candidate.toLowerCase().startsWith(arg.toLowerCase()))
            candidates.add(candidate+' ');
    }
    
    public boolean hasCandidates(){
        return !candidates.isEmpty();
    }

    public List candidates(){
        return candidates;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy