KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > LazyLines


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.text;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ListIterator JavaDoc;
26
27 /** Lazy List that delegates to another instance of itself.
28  */

29 final class LazyLines extends Object JavaDoc implements List JavaDoc<Line> {
30     private List JavaDoc<Line> delegate;
31     private DocumentLine.Set set;
32
33     public LazyLines(DocumentLine.Set set) {
34         this.set = set;
35     }
36
37     /** Override this to create the delegate
38      */

39     private List JavaDoc<Line> createDelegate() {
40         int cnt = set.listener.getOriginalLineCount();
41         List JavaDoc<Line> l = new ArrayList JavaDoc<Line>(cnt);
42
43         for (int i = 0; i < cnt; i++) {
44             l.add(set.getOriginal(i));
45         }
46
47         return l;
48     }
49
50     private synchronized List JavaDoc<Line> getDelegate() {
51         if (delegate == null) {
52             delegate = createDelegate();
53         }
54
55         return delegate;
56     }
57
58     public int indexOf(Object JavaDoc o) {
59         if (o instanceof DocumentLine) {
60             Line find = set.findLine((DocumentLine) o);
61
62             if (find != null) {
63                 int indx = set.listener.getOld(find.getLineNumber());
64
65                 if (set.getOriginal(indx).equals(o)) {
66                     // just to verify that the index really exists
67
return indx;
68                 }
69             }
70         }
71
72         return -1;
73     }
74
75     public int lastIndexOf(Object JavaDoc o) {
76         return indexOf(o);
77     }
78
79     //
80
// Pure delegate methods
81
//
82
public int hashCode() {
83         return getDelegate().hashCode();
84     }
85
86     public boolean addAll(java.util.Collection JavaDoc c) {
87         throw new UnsupportedOperationException JavaDoc();
88     }
89
90     public boolean removeAll(java.util.Collection JavaDoc c) {
91         throw new UnsupportedOperationException JavaDoc();
92     }
93
94     public ListIterator JavaDoc<Line> listIterator() {
95         return getDelegate().listIterator();
96     }
97
98     public Object JavaDoc[] toArray() {
99         return getDelegate().toArray();
100     }
101
102     public <T> T[] toArray(T[] a) {
103         return getDelegate().toArray(a);
104     }
105
106     public ListIterator JavaDoc<Line> listIterator(int index) {
107         return getDelegate().listIterator(index);
108     }
109
110     public boolean remove(Object JavaDoc o) {
111         throw new UnsupportedOperationException JavaDoc();
112     }
113
114     public boolean equals(Object JavaDoc obj) {
115         return getDelegate().equals(obj);
116     }
117
118     public boolean contains(Object JavaDoc o) {
119         return getDelegate().contains(o);
120     }
121
122     public void add(int index, Line element) {
123         throw new UnsupportedOperationException JavaDoc();
124     }
125
126     public void clear() {
127         getDelegate().clear();
128     }
129
130     public Line set(int index, Line element) {
131         throw new UnsupportedOperationException JavaDoc();
132     }
133
134     public int size() {
135         return getDelegate().size();
136     }
137
138     public Line get(int index) {
139         return getDelegate().get(index);
140     }
141
142     public boolean containsAll(Collection JavaDoc<?> c) {
143         return getDelegate().containsAll(c);
144     }
145
146     public boolean add(Line o) {
147         throw new UnsupportedOperationException JavaDoc();
148     }
149
150     public boolean isEmpty() {
151         return getDelegate().isEmpty();
152     }
153
154     public boolean retainAll(Collection JavaDoc<?> c) {
155         throw new UnsupportedOperationException JavaDoc();
156     }
157
158     public List JavaDoc<Line> subList(int fromIndex, int toIndex) {
159         return getDelegate().subList(fromIndex, toIndex);
160     }
161
162     public Line remove(int index) {
163         return getDelegate().remove(index);
164     }
165
166     public Iterator JavaDoc<Line> iterator() {
167         return getDelegate().iterator();
168     }
169
170     public boolean addAll(int index, java.util.Collection JavaDoc c) {
171         throw new UnsupportedOperationException JavaDoc();
172     }
173 }
174
Popular Tags