KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > MultiCompletor


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002-2006, Marc Prud'hommeaux <mwp1@cornell.edu>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or
7  * without modification, are permitted provided that the following
8  * conditions are met:
9  *
10  * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer
15  * in the documentation and/or other materials provided with
16  * the distribution.
17  *
18  * Neither the name of JLine nor the names of its contributors
19  * may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */

36 package jline;
37
38 import java.util.*;
39
40 /**
41  * <p>
42  * A completor that contains multiple embedded completors. This differs
43  * from the {@link ArgumentCompletor}, in that the nested completors
44  * are dispatched individually, rather than delimited by arguments.
45  * </p>
46  *
47  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
48  */

49 public class MultiCompletor
50     implements Completor
51 {
52     Completor [] completors = new Completor [0];
53
54
55     /**
56      * Construct a MultiCompletor with no embedded completors.
57      */

58     public MultiCompletor ()
59     {
60         this (new Completor [0]);
61     }
62
63
64     /**
65      * Construct a MultiCompletor with the specified list of
66      * {@link Completor} instances.
67      */

68     public MultiCompletor (final List completors)
69     {
70         this ((Completor [])completors.toArray (
71             new Completor [completors.size ()]));
72     }
73
74
75     /**
76      * Construct a MultiCompletor with the specified
77      * {@link Completor} instances.
78      */

79     public MultiCompletor (final Completor [] completors)
80     {
81         this.completors = completors;
82     }
83
84
85     public int complete (final String JavaDoc buffer, final int pos, final List cand)
86     {
87         int [] positions = new int [completors.length];
88         List [] copies = new List [completors.length];
89         for (int i = 0; i < completors.length; i++)
90         {
91             // clone and save the candidate list
92
copies [i] = new LinkedList (cand);
93             positions [i] = completors [i].complete (buffer, pos, copies [i]);
94         }
95
96         int maxposition = -1;
97         for (int i = 0; i < positions.length; i++)
98             maxposition = Math.max (maxposition, positions [i]);
99
100         // now we have the max cursor value: build up all the
101
// candidate lists that have the same cursor value
102
for (int i = 0; i < copies.length; i++)
103         {
104             if (positions [i] == maxposition)
105                 cand.addAll (copies [i]);
106         }
107
108         return maxposition;
109     }
110
111
112     public void setCompletors (final Completor [] completors)
113     {
114         this.completors = completors;
115     }
116
117
118     public Completor [] getCompletors ()
119     {
120         return this.completors;
121     }
122 }
123
Popular Tags