aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/cycling74/max/Atom.java
blob: c1949df58e0fb63ad55d7734bce43e9302146078 (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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package com.cycling74.max;

import java.io.*;
import java.util.*;

/**
 * PD element that is used in message or arguments. It can contains a 
 * float, a int (always map to a float in pd) or a string.
 */
public abstract class Atom implements Comparable, Serializable {
    
    /**
     * Empty array to use with the API when theres is no arguments.
     */
    public static final Atom[] emptyArray = new Atom[] {};
    
	int type;
	
	Atom(int type) {
		this.type = type;
	}

	// Atom factories
	///////////////////////////////////////////////////////////////////
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(int value) {
		return new AtomFloat(value);
	}

	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(int value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}

	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(long value) {
		return new AtomFloat(value);
	}

	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(long value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
		
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(short value) {
		return new AtomFloat(value);
	}

	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(short value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
		
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(byte value) {
		return new AtomFloat(value);
	}

	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(byte value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(char value) {
		return new AtomString(String.valueOf(value));
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(char value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(boolean value) {
		return new AtomFloat( value ? 1:0 );
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(boolean value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(float value) {
		return new AtomFloat(value);
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(float value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(double value) {
		return new AtomFloat((float) value);
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(double value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom newAtom(String value) {
		return new AtomString(value);
	}
	
	/**
	 * Creates a new atom with the specified type.
	 * @param value the value of the atom
	 * @return the new Atom
	 */
	public static Atom[] newAtom(String value[]) {
		Atom[] ret = new Atom[value.length];
		for(int i=0;i<value.length;i++) {
			ret[i] = newAtom(value[i]);
		}
		return ret;
	}
    
	/**
	 * Creates an array of atoms from string tokens. 
	 * @param values the atoms value seperated by string
	 * @return the array of atoms created from the list
	 */
	public static Atom[] parse(String values) {
	    return parse(values, false);
	}

	/**
	 * Creates an array of atoms from strings tokens. 
	 * @param values the atoms value seperated by string
	 * @param skipfirst skip the first token
	 * @return the array of atoms created from the list
	 */
	public static Atom[] parse(String values, boolean skipfirst) {
	    ArrayList list = new ArrayList();
	    StringTokenizer token = new StringTokenizer(values);
	    
	    if ( skipfirst && token.hasMoreTokens() ) {
	        token.nextToken();
	    }
	    
	    while(token.hasMoreTokens()) {
	        list.add(newAtom(token.nextToken()));
	    }
	    
	    Iterator i = list.iterator();
	    Atom[] ret = new Atom[list.size()];
	    for(int idx=0; i.hasNext(); idx++) {
	        ret[idx] = (Atom) i.next();
	    }
	    
	    return ret;
	}
	
	// Atom methods
	///////////////////////////////////////////////////////////////////
	
	/**
	 * Get string value for this Atom. If the Atom does not contains a 
	 * string it will throw an UnsupportedOperationException.
	 */
	public String getString() {
		throw new UnsupportedOperationException("pdj: this atom is not a string");
	}

	/**
	 * Get int value for this Atom. If the Atom does not contains a 
	 * int it will throw an UnsupportedOperationException.
	 */
	public int getInt() {
		throw new UnsupportedOperationException("pdj: this atom is not a int");
	}
	
	/**
	 * Get float value for this Atom. If the Atom does not contains a 
	 * float it will throw an UnsupportedOperationException.
	 */
	public float getFloat() {
		throw new UnsupportedOperationException("pdj: this atom is not a float");
	}
	
	/**
	 * Returns the Object representation of the Atom. If it is an int or a 
	 * float, it will return a Int/Float object.
	 * @return The java.lang.* representation of the object
	 */
	abstract Object toObject();
	
	/**
	 * Returns the double value of this atom.
	 * @return the double value
	 */
	public double toDouble() {
		return 0;
	}
	
	/**
	 * Transform an array of Atom into an array of doubles.
	 * @param values the array of atoms
	 * @return array of doubles
	 */
	public static double[] toDouble(Atom[] values) {
	    double[] ret = new double[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toDouble();
	    }
	    return ret;
	}
	
	/**
	 * Returns the int value of this atom.
	 * @return the int value
	 */
	public int toInt() {
		return 0;
	}

	/**
	 * Transform an array of Atom into an array of ints.
	 * @param values the array of atoms
	 * @return array of ints
	 */
	public static int[] toInt(Atom[] values) {
	    int[] ret = new int[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toInt();
	    }
	    return ret;
	}

	
	/**
	 * Returns the char value of this atom.
	 * @return the char value
	 */
	public char toChar() {
		return 0;
	}
	
	/**
	 * Transform an array of Atom into an array of chars.
	 * @param values the array of atoms
	 * @return array of chars
	 */
	public static char[] toChar(Atom[] values) {
	    char[] ret = new char[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toChar();
	    }
	    return ret;
	}
	
	
	/**
	 * Returns the byte value of this atom.
	 * @return the byte value
	 */
	public byte toByte() {
		return 0;
	}
	
	/**
	 * Transform an array of Atom into an array of bytes.
	 * @param values the array of atoms
	 * @return array of bytes
	 */
	public static byte[] toByte(Atom[] values) {
	    byte[] ret = new byte[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toByte();
	    }
	    return ret;
	}
	
	/**
	 * Returns the long value of this atom.
	 * @return the long value
	 */
	public long toLong() {
		return 0;
	}
	
	/**
	 * Transform an array of Atom into an array of longs.
	 * @param values the array of atoms
	 * @return array of longs
	 */
	public static long[] toLong(Atom[] values) {
	    long[] ret = new long[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toLong();
	    }
	    return ret;
	}
	
	/**
	 * Returns the short value of this atom.
	 * @return the short value
	 */
	public short toShort() {
		return 0;
	}
	
	/**
	 * Transform an array of Atom into an array of shorts.
	 * @param values the array of atoms
	 * @return array of shorts
	 */
	public static short[] toShort(Atom[] values) {
	    short[] ret = new short[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toShort();
	    }
	    return ret;
	}
	
	/**
	 * Returns the float value of this atom.
	 * @return the float value
	 */
	public float toFloat() {
		return 0;
	}
	
	/**
	 * Transform an array of Atom into an array of floats.
	 * @param values the array of atoms
	 * @return array of floats
	 */
	public static float[] toFloat(Atom[] values) {
	    float[] ret = new float[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toFloat();
	    }
	    return ret;
	}
	
	/**
	 * Returns the boolean value of this atom.
	 * @return the boolean value
	 */
	public boolean toBoolean() {
		return true;
	}
	
	/**
	 * Transform an array of Atom into an array of booleans.
	 * @param values the array of atoms
	 * @return array of booleans
	 */
	public static boolean[] toBoolean(Atom[] values) {
	    boolean[] ret = new boolean[values.length];
	    
	    for(int i=0;i<values.length;i++) {
	        ret[i] = values[i].toBoolean();
	    }
	    return ret;
	}

	/**
	 * Transform an array of Atom into an array of strings.
	 * @param array the array of atoms
	 * @return array of strings
	 */
	public static String[] toString(Atom[] array) {
	    String[] ret = new String[array.length];
	    
	    for(int i=0;i<array.length;i++) {
	        ret[i] = array[i].toString();
	    }
	    return ret;
	}
	
	/**
	 * Returns true if the Atom has been created with a float.
	 * @return true if the Atom has been created with a float
	 */
	public boolean isFloat() {
		return false;
	}
	
	/**
	 * Returns true if the Atom has been created with a String.
	 * @return true if the Atom has been created with a String
	 */
	public boolean isString() {
		return false;
	}
	
	/**
	 * Returns true if the Atom has been created with a int.
	 * @return true if the Atom has been created with a int
	 */
	public boolean isInt() {
		return false;
	}
	
	/**
	 * Returns the array of Atom into one string. Atom elements are 
	 * seperated by spaces.
	 * @param array the array of atom
	 * @return one string representation of the atom
	 */
	public static String toOneString(Atom[] array) {
	    StringBuffer sb = new StringBuffer();
	    boolean appendSpace = false;
	    
	    for(int i=0;i<array.length;i++) {
	        if ( !appendSpace ) { 
	            sb.append(' ');
	            appendSpace = true;
	        }
	        sb.append(array[i].toString());
	    }
	    
	    return sb.toString();
	}
	
	/**
	 * Returns true if the instance has the same value of the object
	 * <code>object</code>. Similar to <code>"ok".equals("ok");</code>
	 */
	public abstract boolean equals(Object object);

	/**
	 * Returns the hashCode representation for this Atom. If it is an float,
	 * the float into bit value is return and if it is an String, the hashcode
	 * value of the string is returned.
	 */
	public abstract int hashCode();
	
	// Array utility classes
	///////////////////////////////////////////////////////////////////

	/**
	 * Returns the index of the first atom that is found in the list.
	 * @param org the atom to find
	 * @param list the list of atom to search
	 * @return the index of the atom found. -1 if not found.
	 */
	public static int isIn(Atom org, Atom[] list) {
		return isIn(org, list, 0, list.length-1);
	}

	/**
	 * Returns the index of the first atom that is found in the list.
	 * @param org the atom to find
	 * @param list the list of atom to search
	 * @param from the start index to check
	 * @param to the last index to check
	 * @return the index of the atom found. -1 if not found.
	 */
	public static int isIn(Atom org, Atom[] list, int from, int to) {
		for(int i=from;i<to+1;i++) {
			if ( list[i].equals(org) ) 
				return i;
		}
		return -1;
	}

	/**
	 * Remove at index <code>from</code> to index <code>to</code>.
	 * @param list the list to strip
	 * @param from the start index
	 * @param to the last index
	 * @return the stripped array
	 */
	public static Atom[] removeSome(Atom[] list, int from, int to) {
		if ( from == 0 && from == list.length - 1 ) 
			return new Atom[] {};

		Atom[] ret = new Atom[list.length - (to-from+1)];
		System.arraycopy(list, 0, ret, 0, from);
	    System.arraycopy(list, to + 1, ret, from, list.length - to - 1);
		
		return ret;
	}
	
	/**
	 * Removes one atom in the list.
	 * @param list the list to strip
	 * @param i the index of the atom to remove
	 * @return the stripped array
	 */
	public static Atom[] removeOne(Atom[] list, int i) {
		return removeSome(list, i, i);
	}
	
	/**
	 * Removes the first atom in the list.
	 * @param list the list to strip
	 * @return the stripped array
	 */
	public static Atom[] removeFirst(Atom[] list) {
	    return removeFirst(list, 1);
	}
	
	/**
	 * Removes the first <code>howmany</code> atoms in the list. 
	 * @param list the list to strip
	 * @param howmany how many element to remove
	 * @return the stripped array
	 */
	public static Atom[] removeFirst(Atom[] list, int howmany) {
	    return removeSome(list, 0, howmany-1);
	}

	/**
	 * Remove the last atom in the list.
	 * @param list the list to strip
	 * @return the stripped array
	 */
	public static Atom[] removeLast(Atom[] list) {
	    return removeLast(list, 1);
	}
	
	/**
	 * Removes the last <code>howmany</code> atoms in the list.
	 * @param list the list to strip
	 * @param howmany how many element to remove
	 * @return the stripped array
	 */
	public static Atom[] removeLast(Atom[] list, int howmany) {
	    return removeSome(list, list.length-howmany, list.length-1);
	}
	
	/**
	 * Reverses the element content; the first element is the last and so on.
	 * @param list the list to reverse
	 * @return the reversed list
	 */
	public static Atom[] reverse(Atom[] list) {
	    Atom[] ret = new Atom[list.length];
	    int last = list.length - 1;
	    for(int i=0;i<list.length;i++) {
	        ret[last-i] = list[i];
	    }
	    return ret;
	}
	
	/**
	 * Rotates array content x number of times.
	 * @param list the list to rotate
	 * @param nbTimes the number of time that the array must be rotated
	 * @return the rotated list
	 */
	public static Atom[] rotate(Atom[] list, int nbTimes) {
	    Atom ret[] = new Atom[list.length];
	    
	    for(int i=0;i<list.length;i++) {
	        ret[(i+nbTimes) % list.length] = list[i];
	    }
	    
	    return ret;
	}

	/**
	 * Don't know what this does. Max/MSP says: it does an union. If
	 * you want me to support this, send me an email and what it does.
	 * @param first
	 * @param second
	 * @return nothing yet
	 * @throws UnsupportedOperationException
	 */
	public static Atom[] union(Atom[] first, Atom[] second) throws UnsupportedOperationException {
	    throw new UnsupportedOperationException("not implemented.");
	}
	
	/**
	 * Don't know what this does. Max/MSP says: it does an intersection. 
	 * If you want me to support this, send me an email and what it does.
	 * @param first
	 * @param second
	 * @return nothing yet
	 * @throws UnsupportedOperationException
	 */
	public static Atom[] intersection(Atom[] first, Atom[] second)  throws UnsupportedOperationException {
	    throw new UnsupportedOperationException("not implemented.");
	}
	
	/**
	 * Used to return a string representation of the list with atom type.
	 * @param values the array of atoms
	 * @return the string representation of the array
	 */
	public static String toDebugString(Atom[] values) {
	    StringBuffer sb = new StringBuffer();
	    
	    sb.append("Atom[");
	    sb.append(values.length);
	    sb.append("]=");
	    for(int i=0;i<values.length;i++) {
	        sb.append('{');
	        sb.append(values[i].toString());
	        if ( values[i] instanceof AtomString ) 
	            sb.append(":S}");
	        else 
	            sb.append(":F}");
	    }
	    
	    return sb.toString();
	}
}