View Javadoc

1   package io.github.reggert.reb4j;
2   
3   import java.util.regex.Pattern;
4   
5   import fj.data.LazyString;
6   
7   /**
8    * Expression constructed by "adopting" the expression string from
9    * an instance of {@link Pattern}.
10   *
11   * Note that adopted expressions must be constructed from instances of
12   * {@link Pattern} and not raw {@link String}s, because the
13   * {@link Pattern} class ensures that the expressions parse properly.
14   */
15  public final class Adopted extends AbstractExpression 
16  {
17  	private static final long serialVersionUID = 1L;
18  	public final LazyString expression;
19  	
20  	private Adopted(final String expression)
21  	{this.expression = LazyString.str(expression);}
22  	
23  	@Override
24  	public LazyString expression() 
25  	{return expression;}
26  	
27  	/**
28  	 * Adopts the regular expression represented by the specified
29  	 * {@link java.util.regex.Pattern}.
30  	 * 
31  	 * @param pattern
32  	 * 	the regular expression to adopt; must not be <code>null</code>.
33  	 * @throws NullPointerException
34  	 * 	if <var>pattern</var> is <code>null</code>.
35  	 */
36  	public static Adopted fromPattern(final Pattern pattern)
37  	{
38  		if (pattern == null)
39  			throw new NullPointerException("pattern");
40  		return new Adopted(pattern.pattern());
41  	}
42  
43  	@Override
44  	public int hashCode()
45  	{
46  		final int prime = 31;
47  		int result = 1;
48  		result = prime * result + (expression.toString().hashCode());
49  		return result;
50  	}
51  
52  	@Override
53  	public boolean equals(final Object obj)
54  	{
55  		if (this == obj)
56  			return true;
57  		if (obj == null)
58  			return false;
59  		if (getClass() != obj.getClass())
60  			return false;
61  		final Adopted other = (Adopted) obj;
62  		return expression.toString().equals(other.expression.toString());
63  	}
64  
65  	@Override
66  	public Integer boundedLength() 
67  	{
68  		return null; // We don't have enough information to tell whether it is bounded.
69  	}
70  
71  	@Override
72  	public boolean repetitionInvalidatesBounds() 
73  	{
74  		return true;
75  	}
76  
77  	@Override
78  	public boolean possiblyZeroLength() 
79  	{
80  		return true;
81  	}
82  }