View Javadoc

1   package io.github.reggert.reb4j.charclass;
2   
3   import static fj.Ord.charOrd;
4   import io.github.reggert.reb4j.Literal;
5   
6   
7   import fj.data.LazyString;
8   import fj.data.Set;
9   
10  /**
11   * Character class consisting of a single character.
12   */
13  public final class SingleChar extends CharClass
14  {
15  	private static final long serialVersionUID = 1L;
16  	public final char character;
17  	
18  	SingleChar(final char character)
19  	{
20  		this.character = character;
21  	}
22  
23  	@Override
24  	public Negated<SingleChar> negated()
25  	{
26  		return new Negated<SingleChar>(this);
27  	}
28  
29  	@Override
30  	public LazyString unitableForm()
31  	{
32  		return Literal.escapeChar(character);
33  	}
34  
35  	@Override
36  	public LazyString independentForm()
37  	{
38  		return unitableForm();
39  	}
40  
41  	@Override
42  	public int hashCode()
43  	{
44  		final int prime = 31;
45  		int result = 1;
46  		result = prime * result + character;
47  		return result;
48  	}
49  
50  	@Override
51  	public boolean equals(final Object obj)
52  	{
53  		if (this == obj)
54  			return true;
55  		if (obj == null)
56  			return false;
57  		if (getClass() != obj.getClass())
58  			return false;
59  		final SingleChar other = (SingleChar) obj;
60  		return character == other.character;
61  	}
62  	
63  	/**
64  	 * Overloaded version of {@link CharClass#union(CharClass)} for arguments 
65  	 * of type {@link SingleChar} that simply merges the two objects into
66  	 * a single instance of {@link MultiChar}.
67  	 * 
68  	 * @param right another instance of {@link MultiChar}; must not be <code>null</code>.
69  	 * @return a new instance of {@link MultiChar}.
70  	 * @throws NullPointerException
71  	 * 	if the argument is <code>null</code>.
72  	 */
73  	public MultiChar union(final SingleChar right)
74  	{
75  		return new MultiChar(Set.set(charOrd, character, right.character));
76  	}
77  	
78  	/**
79  	 * Overloaded version of {@link CharClass#union(CharClass)} for arguments 
80  	 * of type {@link MultiChar} that simply merges the two objects into
81  	 * a single instance of {@link MultiChar}.
82  	 * 
83  	 * @param right another instance of {@link MultiChar}; must not be <code>null</code>.
84  	 * @return a new instance of {@link MultiChar}.
85  	 * @throws NullPointerException
86  	 * 	if the argument is <code>null</code>.
87  	 */
88  	public MultiChar union(final MultiChar right)
89  	{
90  		return new MultiChar(right.characters.insert(character));
91  	}
92  }