aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java
blob: e363fdb4571ee5b695e4ce5475e373565da0bda8 (plain) (blame)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2000-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
package com.ericsson.otp.erlang;

import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Provides a Java representation of Erlang lists. Lists are created from zero
 * or more arbitrary Erlang terms.
 * 
 * <p>
 * The arity of the list is the number of elements it contains.
 */
public class OtpErlangList extends OtpErlangObject implements
	Iterable<OtpErlangObject>, Serializable, Cloneable {
    // don't change this!
    static final long serialVersionUID = 5999112769036676548L;

    private static final OtpErlangObject[] NO_ELEMENTS = new OtpErlangObject[0];

    final private OtpErlangObject[] elems;

    private OtpErlangObject lastTail = null;

    /**
     * Create an empty list.
     */
    public OtpErlangList() {
	elems = NO_ELEMENTS;
    }

    /**
     * Create a list of Erlang integers representing Unicode codePoints.
     * This method does not check if the string contains valid code points.
     * 
     * @param str
     *            the characters from which to create the list.
     */
    public OtpErlangList(final String str) {
	if (str == null || str.length() == 0) {
	    elems = NO_ELEMENTS;
	} else {
	    final int[] codePoints = OtpErlangString.stringToCodePoints(str);
	    elems = new OtpErlangObject[codePoints.length];
	    for (int i = 0;  i < elems.length;  i++) {
		elems[i] = new OtpErlangInt(codePoints[i]);
	    }
	}
    }

    /**
     * Create a list containing one element.
     * 
     * @param elem
     *            the elememet to make the list from.
     */
    public OtpErlangList(final OtpErlangObject elem) {
	elems = new OtpErlangObject[] { elem };
    }

    /**
     * Create a list from an array of arbitrary Erlang terms.
     * 
     * @param elems
     *            the array of terms from which to create the list.
     */
    public OtpErlangList(final OtpErlangObject[] elems) {
	this(elems, 0, elems.length);
    }

    /**
     * Create a list from an array of arbitrary Erlang terms. Tail can be
     * specified, if not null, the list will not be proper.
     * 
     * @param elems
     *            array of terms from which to create the list
     * @param lastTail
     * @throws OtpErlangException
     */
    public OtpErlangList(final OtpErlangObject[] elems,
	    final OtpErlangObject lastTail) throws OtpErlangException {
	this(elems, 0, elems.length);
	if (elems.length == 0 && lastTail != null) {
	    throw new OtpErlangException("Bad list, empty head, non-empty tail");
	}
	this.lastTail = lastTail;
    }

    /**
     * Create a list from an array of arbitrary Erlang terms.
     * 
     * @param elems
     *            the array of terms from which to create the list.
     * @param start
     *            the offset of the first term to insert.
     * @param count
     *            the number of terms to insert.
     */
    public OtpErlangList(final OtpErlangObject[] elems, final int start,
	    final int count) {
	if (elems != null && count > 0) {
	    this.elems = new OtpErlangObject[count];
	    System.arraycopy(elems, start, this.elems, 0, count);
	} else {
	    this.elems = NO_ELEMENTS;
	}
    }

    /**
     * Create a list from a stream containing an list encoded in Erlang external
     * format.
     * 
     * @param buf
     *            the stream containing the encoded list.
     * 
     * @exception OtpErlangDecodeException
     *                if the buffer does not contain a valid external
     *                representation of an Erlang list.
     */
    public OtpErlangList(final OtpInputStream buf)
	    throws OtpErlangDecodeException {
	final int arity = buf.read_list_head();
	if (arity > 0) {
	    elems = new OtpErlangObject[arity];
	    for (int i = 0; i < arity; i++) {
		elems[i] = buf.read_any();
	    }
	    /* discard the terminating nil (empty list) or read tail */
	    if (buf.peek1() == OtpExternal.nilTag) {
		buf.read_nil();
	    } else {
		lastTail = buf.read_any();
	    }
	} else {
	    elems = NO_ELEMENTS;
	}
    }

    /**
     * Get the arity of the list.
     * 
     * @return the number of elements contained in the list.
     */
    public int arity() {
	return elems.length;
    }

    /**
     * Get the specified element from the list.
     * 
     * @param i
     *            the index of the requested element. List elements are numbered
     *            as array elements, starting at 0.
     * 
     * @return the requested element, of null if i is not a valid element index.
     */
    public OtpErlangObject elementAt(final int i) {
	if (i >= arity() || i < 0) {
	    return null;
	}
	return elems[i];
    }

    /**
     * Get all the elements from the list as an array.
     * 
     * @return an array containing all of the list's elements.
     */
    public OtpErlangObject[] elements() {
	if (arity() == 0) {
	    return NO_ELEMENTS;
	} else {
	    final OtpErlangObject[] res = new OtpErlangObject[arity()];
	    System.arraycopy(elems, 0, res, 0, res.length);
	    return res;
	}
    }

    /**
     * Get the string representation of the list.
     * 
     * @return the string representation of the list.
     */

    @Override
    public String toString() {
	return toString(0);
    }

    protected String toString(final int start) {
	final StringBuffer s = new StringBuffer();
	s.append("[");

	for (int i = start; i < arity(); i++) {
	    if (i > start) {
		s.append(",");
	    }
	    s.append(elems[i].toString());
	}
	if (lastTail != null) {
	    s.append("|").append(lastTail.toString());
	}
	s.append("]");

	return s.toString();
    }

    /**
     * Convert this list to the equivalent Erlang external representation. Note
     * that this method never encodes lists as strings, even when it is possible
     * to do so.
     * 
     * @param buf
     *            An output stream to which the encoded list should be written.
     * 
     */

    @Override
    public void encode(final OtpOutputStream buf) {
	encode(buf, 0);
    }

    protected void encode(final OtpOutputStream buf, final int start) {
	final int arity = arity() - start;

	if (arity > 0) {
	    buf.write_list_head(arity);

	    for (int i = start; i < arity + start; i++) {
		buf.write_any(elems[i]);
	    }
	}
	if (lastTail == null) {
	    buf.write_nil();
	} else {
	    buf.write_any(lastTail);
	}
    }

    /**
     * Determine if two lists are equal. Lists are equal if they have the same
     * arity and all of the elements are equal.
     * 
     * @param o
     *            the list to compare to.
     * 
     * @return true if the lists have the same arity and all the elements are
     *         equal.
     */

    @Override
    public boolean equals(final Object o) {

	/*
	 * Be careful to use methods even for "this", so that equals work also
	 * for sublists
	 */

	if (!(o instanceof OtpErlangList)) {
	    return false;
	}

	final OtpErlangList l = (OtpErlangList) o;

	final int a = arity();
	if (a != l.arity()) {
	    return false;
	}
	for (int i = 0; i < a; i++) {
	    if (!elementAt(i).equals(l.elementAt(i))) {
		return false; // early exit
	    }
	}
	final OtpErlangObject otherTail = l.getLastTail();
	if (getLastTail() == null && otherTail == null) {
	    return true;
	}
	if (getLastTail() == null) {
	    return false;
	}
	return getLastTail().equals(l.getLastTail());
    }

    public OtpErlangObject getLastTail() {
	return lastTail;
    }
    
    @Override
    protected int doHashCode() {
	OtpErlangObject.Hash hash = new OtpErlangObject.Hash(4);
	final int a = arity();
	if (a == 0) {
	    return (int)3468870702L;
	}
	for (int i = 0; i < a; i++) {
	    hash.combine(elementAt(i).hashCode());
	}
	final OtpErlangObject t = getLastTail();
	if (t != null) {
	    int h = t.hashCode();
	    hash.combine(h, h);
	}
	return hash.valueOf();
    }
    
    @Override
    public Object clone() {
	try {
	    return new OtpErlangList(elements(), getLastTail());
	} catch (final OtpErlangException e) {
			throw new AssertionError(this);
	}
    }

    public Iterator<OtpErlangObject> iterator() {
	return iterator(0);
    }

    private Iterator<OtpErlangObject> iterator(final int start) {
	return new Itr(start);
    }

    /**
     * @return true if the list is proper, i.e. the last tail is nil
     */
    public boolean isProper() {
	return lastTail == null;
    }

    public OtpErlangObject getHead() {
	if (arity() > 0) {
	    return elems[0];
	}
	return null;
    }

    public OtpErlangObject getTail() {
	return getNthTail(1);
    }

    public OtpErlangObject getNthTail(final int n) {
	final int arity = arity();
	if (arity >= n) {
	    if (arity == n && lastTail != null) {
		return lastTail;
	    } else {
		return new SubList(this, n);
	    }
	}
	return null;
    }

    /**
     * Convert a list of integers into a Unicode string,
     * interpreting each integer as a Unicode code point value.
     * 
     * @return A java.lang.String object created through its
     *         constructor String(int[], int, int).
     *
     * @exception OtpErlangException
     *                    for non-proper and non-integer lists.
     *
     * @exception OtpErlangRangeException
     *                    if any integer does not fit into a Java int.
     *
     * @exception java.security.InvalidParameterException
     *                    if any integer is not within the Unicode range.
     *
     * @see String#String(int[], int, int)
     *
     */

    public String stringValue() throws OtpErlangException {
	if (! isProper()) {
	    throw new OtpErlangException("Non-proper list: " + this);
	}
	final int[] values = new int[arity()];
	for (int i = 0; i < values.length; ++i) {
	    final OtpErlangObject o = elementAt(i);
	    if (! (o instanceof OtpErlangLong)) {
		throw new OtpErlangException("Non-integer term: " + o);
	    }
	    final OtpErlangLong l = (OtpErlangLong) o;
	    values[i] = l.intValue();
	}
	return new String(values, 0, values.length);
    }



    public static class SubList extends OtpErlangList {
	private static final long serialVersionUID = OtpErlangList.serialVersionUID;

	private final int start;

	private final OtpErlangList parent;

	private SubList(final OtpErlangList parent, final int start) {
	    super();
	    this.parent = parent;
	    this.start = start;
	}

	@Override
	public int arity() {
	    return parent.arity() - start;
	}

	@Override
	public OtpErlangObject elementAt(final int i) {
	    return parent.elementAt(i + start);
	}

	@Override
	public OtpErlangObject[] elements() {
	    final int n = parent.arity() - start;
	    final OtpErlangObject[] res = new OtpErlangObject[n];
	    for (int i = 0; i < res.length; i++) {
		res[i] = parent.elementAt(i + start);
	    }
	    return res;
	}

	@Override
	public boolean isProper() {
	    return parent.isProper();
	}

	@Override
	public OtpErlangObject getHead() {
	    return parent.elementAt(start);
	}

	@Override
	public OtpErlangObject getNthTail(final int n) {
	    return parent.getNthTail(n + start);
	}

	@Override
	public String toString() {
	    return parent.toString(start);
	}

	@Override
	public void encode(final OtpOutputStream stream) {
	    parent.encode(stream, start);
	}

	@Override
	public OtpErlangObject getLastTail() {
	    return parent.getLastTail();
	}

	@Override
	public Iterator<OtpErlangObject> iterator() {
	    return parent.iterator(start);
	}
    }

    private class Itr implements Iterator<OtpErlangObject> {
	/**
	 * Index of element to be returned by subsequent call to next.
	 */
	private int cursor;

	private Itr(final int cursor) {
	    this.cursor = cursor;
	}

	public boolean hasNext() {
	    return cursor < elems.length;
	}

	public OtpErlangObject next() {
	    try {
		return elems[cursor++];
	    } catch (final IndexOutOfBoundsException e) {
		throw new NoSuchElementException();
	    }
	}

	public void remove() {
	    throw new UnsupportedOperationException(
		    "OtpErlangList cannot be modified!");
	}
    }
}