View Javadoc

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   * Character class consisting of multiple characters.
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  	 * Overloaded version of {@link CharClass#union(CharClass)} for arguments 
51  	 * of type {@link MultiChar} that simply merges the two objects into
52  	 * a single instance of {@link MultiChar}.
53  	 * 
54  	 * @param right another instance of {@link MultiChar}; must not be <code>null</code>.
55  	 * @return a new instance of {@link MultiChar}.
56  	 * @throws NullPointerException
57  	 * 	if the argument is <code>null</code>.
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  	 * Overloaded version of {@link CharClass#union(CharClass)} for arguments 
67  	 * of type {@link SingleChar} that simply merges the two objects into
68  	 * a single instance of {@link MultiChar}.
69  	 * 
70  	 * @param right another instance of {@link MultiChar}; must not be <code>null</code>.
71  	 * @return a new instance of {@link MultiChar}.
72  	 * @throws NullPointerException
73  	 * 	if the argument is <code>null</code>.
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  }