blob: d5451e7660d06e72e279701cd513e71d3687abd5 (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language.process;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
/**
* A list of strings which does not allow for duplicate elements.
*
* @author steinar
*/
public class StemList extends AbstractList<String> {
private final List<String> stems;
public StemList() {
this(new String[0]);
}
public StemList(String... stems) {
super();
this.stems = new ArrayList<>(Math.max(stems.length, 3));
for (String word : stems) {
add(word);
}
}
@Override
public String get(int i) {
return stems.get(i);
}
@Override
public int size() {
return stems.size();
}
@Override
public String set(int i, String element) {
int existing = stems.indexOf(element);
if (existing >= 0 && existing != i) {
// the element already exists
return element;
} else {
return stems.set(i, element);
}
}
@Override
public void add(int i, String element) {
int existing = stems.indexOf(element);
if (existing < 0) {
stems.add(i, element);
}
}
@Override
public String remove(int i) {
return stems.remove(i);
}
}
|