summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/java/org/apache/tuscany/IterableUtil.java
blob: 2366d79af6c995875dd662f8367dc6135aee328f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * 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
 * 
 *   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 org.apache.tuscany;

import static java.util.Arrays.*;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Utility functions to help work efficiently with iterable lists, inspired from Lisp.
 */
public class IterableUtil {

    /**
     * Convert an array or a variable list of arguments to an iterable list.
     */
    public static <T> Iterable<T> list(final Object... a) {
        return new ArrayIterable<T>(a, 0);
    }

    /**
     * Convert an iterable list to a java.util.Collection.
     */
    @SuppressWarnings("unchecked")
    public static <T> Collection<T> collection(final Object l) {
        final Collection<T> c = new ArrayList<T>();
        for(final Object x : (Iterable<?>)l)
            c.add((T)x);
        return c;
    }

    /**
     * Construct a new list from an element and a list.
     */
    public static <T> Iterable<T> cons(final Object car, final Iterable<?> cdr) {
        return new PairIterable<T>(car, cdr);
    }

    /**
     * Return true if a list is nil (empty).
     */
    public static boolean isNil(final Object l) {
        if(l instanceof BasicIterable<?>)
            return ((BasicIterable<?>)l).isNil();
        if(l instanceof Collection<?>)
            return ((Collection<?>)l).isEmpty();
        return !((Iterable<?>)l).iterator().hasNext();
    }

    /**
     * Return the car (first element) of a list.
     */
    @SuppressWarnings("unchecked")
    public static <T> T car(final Object l) {
        if(l instanceof BasicIterable<?>)
            return ((BasicIterable<T>)l).car();
        if(l instanceof List<?>)
            return (T)((List<?>)l).get(0);
        return (T)((Iterable<?>)l).iterator().next();
    }

    /**
     * Return the cdr (rest after the first element) of a list.
     */
    @SuppressWarnings("unchecked")
    public static <T> Iterable<T> cdr(final Object l) {
        if(l instanceof BasicIterable<?>)
            return ((BasicIterable<T>)l).cdr();
        if(l instanceof List<?>)
            return new ListIterable<T>((List<?>)l, 1);
        if(l instanceof Collection<?>)
            return new ArrayIterable<T>(((Collection<?>)l).toArray(), 1);
        return new Iterable<T>() {
            public Iterator<T> iterator() {
                final Iterator<T> i = ((Iterable<T>)l).iterator();
                i.next();
                return i;
            }
        };
    }

    /**
     * Return the car of the cdr of a list.
     */
    @SuppressWarnings("unchecked")
    public static <T> T cadr(final Object l) {
        return (T)car(cdr(l));
    }

    /**
     * Return the cdr of the cdr of a list.
     */
    public static <T> Iterable<T> cddr(final Object l) {
        return cdr(cdr(l));
    }

    /**
     * Return the cdr of the cdr of the cdr of a list.
     */
    public static <T> Iterable<T> cdddr(final Object l) {
        return cdr(cdr(cdr(l)));
    }

    /**
     * Return the car of the cdr of the cdr of a list.
     */
    @SuppressWarnings("unchecked")
    public static <T> T caddr(final Object l) {
        return (T)car(cddr(l));
    }

    /**
     * Return the car of the cdr of the cdr of the cdr of a list.
     */
    @SuppressWarnings("unchecked")
    public static <T> T cadddr(final Object l) {
        return (T)car(cdddr(l));
    }

    /**
     * Appends a list and another list.
     */
    @SuppressWarnings("unchecked")
    public static <T> Iterable<T> append(final Object a, final Object b) {
        if (isNil(a))
            return (Iterable<T>)b;
        return cons(car(a), append(cdr(a), b));
    }

    /**
     * Return the first pair matching a key from a list of key value pairs.
     */
    public static <T> Iterable<T> assoc(final Object k, final Object l) {
        if(isNil(l))
            return list();
        if(k.equals(car(car(l))))
            return car(l);
        return assoc(k, cdr(l));
    }

    /**
     * Internal base implementation class for iterable and immutable lists.
     */
    static abstract class BasicIterable<T> extends AbstractList<T> {
        abstract T car();

        abstract Iterable<T> cdr();

        abstract Boolean isNil();

        @Override
        public int size() {
            return this.isNil()? 0 : 1 + ((List<T>)this.cdr()).size();
        }

        @Override
        public T get(final int index) {
            throw new UnsupportedOperationException();
        }
    }

    /**
     * Internal implementation of a list backed by an array.
     */
    static class ArrayIterable<T> extends BasicIterable<T> {
        final Object[] a;
        final int start;

        ArrayIterable(final Object[] a, final int start) {
            this.a = a;
            this.start = start;
        }

        @Override
        Boolean isNil() {
            return this.a.length - this.start == 0;
        }

        @SuppressWarnings("unchecked")
        @Override
        T car() {
            return (T)this.a[this.start];
        }

        @Override
        BasicIterable<T> cdr() {
            return new ArrayIterable<T>(this.a, this.start + 1);
        }

        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int i = ArrayIterable.this.start;

                public boolean hasNext() {
                    return this.i < ArrayIterable.this.a.length;
                }

                @SuppressWarnings("unchecked")
                public T next() {
                    return (T)ArrayIterable.this.a[this.i++];
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }

    /**
     * Internal implementation of a list backed by a java.util.List.
     */
    static class ListIterable<T> extends BasicIterable<T> {
        final List<?> l;
        final int start;

        ListIterable(final List<?> l, final int start) {
            this.l = l;
            this.start = start;
        }

        @Override
        Boolean isNil() {
            return this.l.size() - this.start == 0;
        }

        @SuppressWarnings("unchecked")
        @Override
        T car() {
            return (T)this.l.get(this.start);
        }

        @Override
        BasicIterable<T> cdr() {
            return new ListIterable<T>(this.l, this.start + 1);
        }

        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int i = ListIterable.this.start;

                public boolean hasNext() {
                    return this.i < ListIterable.this.l.size();
                }

                @SuppressWarnings("unchecked")
                public T next() {
                    return (T)ListIterable.this.l.get(this.i++);
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }

    /**
     * Internal implementation of a list backed by an element / iterable pair.
     */
    static class PairIterable<T> extends BasicIterable<T> {
        final Object car;
        final Iterable<?> cdr;

        PairIterable(final Object car, final Iterable<?> cdr) {
            this.car = car;
            this.cdr = cdr;
        }

        @Override
        Boolean isNil() {
            return false;
        }

        @SuppressWarnings("unchecked")
        @Override
        T car() {
            return (T)this.car;
        }

        @SuppressWarnings("unchecked")
        @Override
        Iterable<T> cdr() {
            return (Iterable<T>)this.cdr;
        }

        @Override
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                boolean carIterator = true;
                Iterator<?> cdrIterator = PairIterable.this.cdr.iterator();

                public boolean hasNext() {
                    if(this.carIterator)
                        return true;
                    return this.cdrIterator.hasNext();
                }

                @SuppressWarnings("unchecked")
                public T next() {
                    if(this.carIterator) {
                        this.carIterator = false;
                        return (T)PairIterable.this.car;
                    }
                    return (T)this.cdrIterator.next();
                }

                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }

    /**
     * Test the list functions.
     */
    static class Test {
        Boolean testList() {
            final Iterable<Object> l = list(2, 3, 4);
            assert car(l) == Integer.valueOf(2);
            assert cadr(l) == Integer.valueOf(3);
            assert caddr(l) == Integer.valueOf(4);

            final Iterable<Object> c = cons(0, cons(1, l));
            assert car(c) == Integer.valueOf(0);
            assert cadr(c) == Integer.valueOf(1);
            assert caddr(c) == Integer.valueOf(2);
            assert c.toString().equals("[0, 1, 2, 3, 4]");

            final Iterable<Object> cl = cons(0, cons(1, new ArrayList<Object>(asList(2, 3, 4))));
            assert car(cl) == Integer.valueOf(0);
            assert cadr(cl) == Integer.valueOf(1);
            assert caddr(cl) == Integer.valueOf(2);
            assert cl.toString().equals("[0, 1, 2, 3, 4]");

            final List<Object> jl = new ArrayList<Object>(collection(cl));
            assert jl.size() == 5;
            assert jl.get(0) == Integer.valueOf(0);
            assert jl.get(1) == Integer.valueOf(1);
            assert jl.get(2) == Integer.valueOf(2);

            final Iterable<Object> n = list();
            assert isNil(n);
            assert n.toString().equals("[]");

            final Iterable<Object> cn = cons(0, n);
            assert !isNil(cn);
            assert isNil(cdr(cn));
            assert cn.toString().equals("[0]");

            final Iterable<Object> al = new ArrayList<Object>(Arrays.asList(1, 2, 3));
            assert car(al) == Integer.valueOf(1);
            assert cadr(al) == Integer.valueOf(2);
            assert caddr(al) == Integer.valueOf(3);

            final Iterable<Object> a = list(0, 1, 2);
            final Iterable<Object> b = list(3, 4);
            final Iterable<Object> ab = append(a, b);
            assert ab.toString().equals("[0, 1, 2, 3, 4]");
            return true;
        }
    }

    public static void main(final String[] args) {
        System.out.println("Testing...");

        Test.class.getClassLoader().setDefaultAssertionStatus(true);
        new Test().testList();

        System.out.println("OK");
    }

}