-
Notifications
You must be signed in to change notification settings - Fork 585
/
CharAdapter.java
645 lines (577 loc) · 16.7 KB
/
CharAdapter.java
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
/*
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.string.immutable;
import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.CharIterable;
import org.eclipse.collections.api.LazyCharIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.block.function.primitive.CharToCharFunction;
import org.eclipse.collections.api.block.function.primitive.CharToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectCharIntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectCharToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.CharPredicate;
import org.eclipse.collections.api.block.procedure.primitive.CharIntProcedure;
import org.eclipse.collections.api.block.procedure.primitive.CharProcedure;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.iterator.CharIterator;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.CharList;
import org.eclipse.collections.api.list.primitive.ImmutableCharList;
import org.eclipse.collections.api.list.primitive.MutableCharList;
import org.eclipse.collections.api.set.primitive.MutableCharSet;
import org.eclipse.collections.api.tuple.primitive.CharCharPair;
import org.eclipse.collections.api.tuple.primitive.CharObjectPair;
import org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag;
import org.eclipse.collections.impl.factory.primitive.CharLists;
import org.eclipse.collections.impl.lazy.primitive.ReverseCharIterable;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.CharArrayList;
import org.eclipse.collections.impl.primitive.AbstractCharIterable;
import org.eclipse.collections.impl.set.mutable.primitive.CharHashSet;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.StringIterate;
/**
* Provides a view into the char[] stored in a String as an ImmutableCharList. This is a cleaner more OO way of
* providing many of the iterable protocols available in StringIterate for char values.
*
* @since 7.0
*/
public class CharAdapter
extends AbstractCharIterable
implements CharSequence, ImmutableCharList, Serializable
{
private static final long serialVersionUID = 1L;
private final String adapted;
public CharAdapter(String value)
{
this.adapted = value;
}
public static CharAdapter adapt(String value)
{
return new CharAdapter(value);
}
public static CharAdapter from(char... chars)
{
return new CharAdapter(new String(chars));
}
public static CharAdapter from(CharIterable iterable)
{
if (iterable instanceof CharAdapter)
{
return new CharAdapter(iterable.toString());
}
return new CharAdapter(iterable.makeString(""));
}
@Override
public char charAt(int index)
{
return this.adapted.charAt(index);
}
@Override
public int length()
{
return this.adapted.length();
}
@Override
public String subSequence(int start, int end)
{
return this.adapted.substring(start, end);
}
public StringBuilder toStringBuilder()
{
return new StringBuilder(this.adapted);
}
/**
* The value of toString must be strictly implemented as defined in CharSequence.
*/
@Override
public String toString()
{
return this.adapted;
}
@Override
public CharIterator charIterator()
{
return new InternalCharIterator();
}
@Override
public char[] toArray()
{
return this.adapted.toCharArray();
}
@Override
public char[] toArray(char[] target)
{
int size = this.adapted.length();
if (target.length < size)
{
target = new char[size];
}
for (int i = 0; i < size; i++)
{
target[i] = this.adapted.charAt(i);
}
return target;
}
@Override
public boolean contains(char expected)
{
return StringIterate.anySatisfyChar(this.adapted, value -> expected == value);
}
@Override
public void forEach(CharProcedure procedure)
{
this.each(procedure);
}
@Override
public void each(CharProcedure procedure)
{
StringIterate.forEachChar(this.adapted, procedure);
}
@Override
public CharAdapter distinct()
{
StringBuilder builder = new StringBuilder();
CharHashSet seenSoFar = new CharHashSet();
int size = this.size();
for (int i = 0; i < size; i++)
{
char each = this.get(i);
if (seenSoFar.add(each))
{
builder.append(each);
}
}
return new CharAdapter(builder.toString());
}
@Override
public CharAdapter newWith(char element)
{
return new CharAdapter(this.adapted + element);
}
@Override
public CharAdapter newWithout(char element)
{
StringBuilder builder = new StringBuilder(this.adapted);
int indexToRemove = this.indexOf(element);
if (indexToRemove < 0)
{
return this;
}
builder.deleteCharAt(indexToRemove);
return new CharAdapter(builder.toString());
}
@Override
public CharAdapter newWithAll(CharIterable elements)
{
MutableCharList mutableCharList = this.toList();
mutableCharList.addAll(elements);
return new CharAdapter(new String(mutableCharList.toArray()));
}
@Override
public CharAdapter newWithoutAll(CharIterable elements)
{
MutableCharList mutableCharList = this.toList();
mutableCharList.removeAll(elements);
return new CharAdapter(new String(mutableCharList.toArray()));
}
@Override
public CharAdapter toReversed()
{
StringBuilder builder = new StringBuilder(this.adapted);
return new CharAdapter(builder.reverse().toString());
}
@Override
public ImmutableCharList subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException("SubList is not implemented on CharAdapter");
}
@Override
public char get(int index)
{
return this.adapted.charAt(index);
}
public Character getCharacter(int index)
{
return Character.valueOf(this.get(index));
}
@Override
public long dotProduct(CharList list)
{
throw new UnsupportedOperationException("DotProduct is not implemented on CharAdapter");
}
@Override
public int binarySearch(char value)
{
throw new UnsupportedOperationException("BinarySearch is not implemented on CharAdapter");
}
@Override
public int lastIndexOf(char value)
{
for (int i = this.size() - 1; i >= 0; i--)
{
if (this.get(i) == value)
{
return i;
}
}
return -1;
}
@Override
public ImmutableCharList toImmutable()
{
return this;
}
@Override
public char getLast()
{
return this.get(this.size() - 1);
}
@Override
public LazyCharIterable asReversed()
{
return ReverseCharIterable.adapt(this);
}
@Override
public <T> T injectIntoWithIndex(T injectedValue, ObjectCharIntToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
int size = this.size();
for (int i = 0; i < size; i++)
{
result = function.valueOf(result, this.get(i), i);
}
return result;
}
@Override
public char getFirst()
{
return this.get(0);
}
@Override
public int indexOf(char value)
{
int size = this.size();
for (int i = 0; i < size; i++)
{
if (this.get(i) == value)
{
return i;
}
}
return -1;
}
@Override
public void forEachWithIndex(CharIntProcedure procedure)
{
int size = this.size();
for (int i = 0; i < size; i++)
{
procedure.value(this.get(i), i);
}
}
@Override
public CharAdapter select(CharPredicate predicate)
{
return new CharAdapter(StringIterate.selectChar(this.adapted, predicate));
}
@Override
public CharAdapter reject(CharPredicate predicate)
{
return new CharAdapter(StringIterate.rejectChar(this.adapted, predicate));
}
@Override
public <V> ImmutableList<V> collect(CharToObjectFunction<? extends V> function)
{
int size = this.size();
FastList<V> list = FastList.newList(size);
for (int i = 0; i < size; i++)
{
list.add(function.valueOf(this.get(i)));
}
return list.toImmutable();
}
public CharAdapter collectChar(CharToCharFunction function)
{
StringBuilder builder = new StringBuilder(this.length());
int size = this.size();
for (int i = 0; i < size; i++)
{
builder.append(function.valueOf(this.get(i)));
}
return new CharAdapter(builder.toString());
}
@Override
public char detectIfNone(CharPredicate predicate, char ifNone)
{
return StringIterate.detectCharIfNone(this.adapted, predicate, ifNone);
}
@Override
public int count(CharPredicate predicate)
{
return StringIterate.countChar(this.adapted, predicate);
}
@Override
public boolean anySatisfy(CharPredicate predicate)
{
return StringIterate.anySatisfyChar(this.adapted, predicate);
}
@Override
public boolean allSatisfy(CharPredicate predicate)
{
return StringIterate.allSatisfyChar(this.adapted, predicate);
}
@Override
public boolean noneSatisfy(CharPredicate predicate)
{
return StringIterate.noneSatisfyChar(this.adapted, predicate);
}
@Override
public MutableCharList toList()
{
int size = this.size();
CharArrayList list = new CharArrayList(size);
for (int i = 0; i < size; i++)
{
list.add(this.get(i));
}
return list;
}
@Override
public MutableCharSet toSet()
{
int size = this.size();
CharHashSet set = new CharHashSet(size);
for (int i = 0; i < size; i++)
{
set.add(this.get(i));
}
return set;
}
@Override
public MutableCharBag toBag()
{
int size = this.size();
CharHashBag bag = new CharHashBag(size);
for (int i = 0; i < size; i++)
{
bag.add(this.get(i));
}
return bag;
}
@Override
public <T> T injectInto(T injectedValue, ObjectCharToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
int size = this.size();
for (int i = 0; i < size; i++)
{
result = function.valueOf(result, this.get(i));
}
return result;
}
@Override
public RichIterable<CharIterable> chunk(int size)
{
if (size <= 0)
{
throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
}
MutableList<CharIterable> result = Lists.mutable.empty();
if (this.notEmpty())
{
CharIterator iterator = this.charIterator();
while (iterator.hasNext())
{
MutableCharList batch = CharLists.mutable.empty();
for (int i = 0; i < size && iterator.hasNext(); i++)
{
batch.add(iterator.next());
}
result.add(batch);
}
}
return result;
}
@Override
public long sum()
{
long sum = 0;
int size = this.size();
for (int i = 0; i < size; i++)
{
sum += this.get(i);
}
return sum;
}
@Override
public char max()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
char max = this.get(0);
int size = this.size();
for (int i = 1; i < size; i++)
{
char value = this.get(i);
if (max < value)
{
max = value;
}
}
return max;
}
@Override
public char min()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
char min = this.get(0);
int size = this.size();
for (int i = 1; i < size; i++)
{
char value = this.get(i);
if (value < min)
{
min = value;
}
}
return min;
}
@Override
public int size()
{
return this.adapted.length();
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
int size = this.size();
for (int i = 0; i < size; i++)
{
if (i > 0)
{
appendable.append(separator);
}
char value = this.get(i);
appendable.append(value);
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public boolean equals(Object otherList)
{
if (otherList == this)
{
return true;
}
if (!(otherList instanceof CharList))
{
return false;
}
CharList list = (CharList) otherList;
if (this.size() != list.size())
{
return false;
}
for (int i = 0; i < this.size(); i++)
{
if (this.get(i) != list.get(i))
{
return false;
}
}
return true;
}
@Override
public int hashCode()
{
int hashCode = 1;
for (int i = 0; i < this.size(); i++)
{
char item = this.get(i);
hashCode = 31 * hashCode + (int) item;
}
return hashCode;
}
/**
* @since 9.1.
*/
@Override
public ImmutableList<CharCharPair> zipChar(CharIterable iterable)
{
int size = this.size();
int othersize = iterable.size();
MutableList<CharCharPair> target = Lists.mutable.withInitialCapacity(Math.min(size, othersize));
CharIterator iterator = iterable.charIterator();
for (int i = 0; i < size && i < othersize; i++)
{
target.add(PrimitiveTuples.pair(this.get(i), iterator.next()));
}
return target.toImmutable();
}
/**
* @since 9.1.
*/
@Override
public <T> ImmutableList<CharObjectPair<T>> zip(Iterable<T> iterable)
{
int size = this.size();
int othersize = Iterate.sizeOf(iterable);
MutableList<CharObjectPair<T>> target = Lists.mutable.withInitialCapacity(Math.min(size, othersize));
Iterator<T> iterator = iterable.iterator();
for (int i = 0; i < size && i < othersize; i++)
{
target.add(PrimitiveTuples.pair(this.get(i), iterator.next()));
}
return target.toImmutable();
}
private class InternalCharIterator implements CharIterator
{
/**
* Index of element to be returned by subsequent call to next.
*/
private int currentIndex;
@Override
public boolean hasNext()
{
return this.currentIndex != CharAdapter.this.adapted.length();
}
@Override
public char next()
{
if (!this.hasNext())
{
throw new NoSuchElementException();
}
char next = CharAdapter.this.adapted.charAt(this.currentIndex);
this.currentIndex++;
return next;
}
}
}