1 package io.github.reggert.reb4j.charclass;
2
3 import io.github.reggert.reb4j.Literal;
4
5 import fj.F2;
6 import fj.data.LazyString;
7 import fj.data.Set;
8
9
10
11
12 public final class MultiChar extends CharClass
13 {
14 private static final long serialVersionUID = 1L;
15 public final Set<Character> characters;
16
17 public MultiChar(final Set<Character> characters)
18 {
19 if (characters == null) throw new NullPointerException("characters");
20 this.characters = characters;
21 }
22
23 @Override
24 public Negated<MultiChar> negated()
25 {
26 return new Negated<MultiChar>(this);
27 }
28
29 @Override
30 public LazyString unitableForm()
31 {
32 return characters.toStream().foldLeft(
33 new F2<LazyString, Character, LazyString>()
34 {
35 @Override
36 public LazyString f(final LazyString a, final Character b)
37 {return a.append(Literal.escapeChar(b));}
38 },
39 LazyString.empty
40 );
41 }
42
43 @Override
44 public LazyString independentForm()
45 {
46 return LazyString.str("[").append(unitableForm()).append("]");
47 }
48
49
50
51
52
53
54
55
56
57
58
59 public MultiChar union(final MultiChar right)
60 {
61 if (right == null) throw new NullPointerException("right == null");
62 return new MultiChar(characters.union(right.characters));
63 }
64
65
66
67
68
69
70
71
72
73
74
75 public MultiChar union(final SingleChar right)
76 {
77 if (right == null) throw new NullPointerException("right == null");
78 return new MultiChar(characters.insert(right.character));
79 }
80
81 }