| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (

This example Scala source code file ( is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

aggregatecompleter, arraylist, collection, completer, completion, linkedlist, list, override, string

The Scala example source code

 * 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
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

 * Completer which contains multiple completers and aggregates them together.
 * @author <a href="">Jason Dillon</a>
 * @since 2.3
public class AggregateCompleter
    implements Completer
    private final List<Completer> completers = new ArrayList<Completer>();

    public AggregateCompleter() {
        // empty

    public AggregateCompleter(final Collection<Completer> completers) {
        assert completers != null;

    public AggregateCompleter(final Completer... completers) {

    public Collection<Completer> getCompleters() {
        return completers;

    public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
        // buffer could be null
        assert candidates != null;

        List<Completion> completions = new ArrayList<Completion>(completers.size());

        // Run each completer, saving its completion results
        int max = -1;
        for (Completer completer : completers) {
            Completion completion = new Completion(candidates);
            completion.complete(completer, buffer, cursor);

            // Compute the max cursor position
            max = Math.max(max, completion.cursor);


        // Append candidates from completions which have the same cursor position as max
        for (Completion completion : completions) {
            if (completion.cursor == max) {

        return max;

    public String toString() {
        return getClass().getSimpleName() + "{" +
            "completers=" + completers +

    private class Completion
        public final List<CharSequence> candidates;

        public int cursor;

        public Completion(final List<CharSequence> candidates) {
            assert candidates != null;
            this.candidates = new LinkedList<CharSequence>(candidates);

        public void complete(final Completer completer, final String buffer, final int cursor) {
            assert completer != null;

            this.cursor = completer.complete(buffer, cursor, candidates);

Other Scala source code examples

Here is a short list of links related to this Scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.