Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Expression |
|
| 1.7;1.7 |
1 | /* | |
2 | * $Id: Expression.java 788032 2009-06-24 14:08:32Z apetrelli $ | |
3 | * | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | package org.apache.tiles; | |
23 | ||
24 | import static org.apache.tiles.CompareUtil.*; | |
25 | ||
26 | /** | |
27 | * It is an expression, along with the expression language (e.g. EL, MVEL, OGNL) | |
28 | * it is expressed with. | |
29 | * | |
30 | * @version $Rev: 788032 $ $Date: 2009-06-25 00:08:32 +1000 (Thu, 25 Jun 2009) $ | |
31 | * @since 2.2.0 | |
32 | */ | |
33 | public class Expression { | |
34 | ||
35 | /** | |
36 | * The expression itself. | |
37 | */ | |
38 | private String expression; | |
39 | ||
40 | /** | |
41 | * The language of the expression. | |
42 | */ | |
43 | private String language; | |
44 | ||
45 | /** | |
46 | * Constructor. | |
47 | * | |
48 | * @param expression The expression itself. | |
49 | * @param language The language of the expression. | |
50 | * @since 2.2.0 | |
51 | */ | |
52 | 30 | public Expression(String expression, String language) { |
53 | 30 | this.expression = expression; |
54 | 30 | this.language = language; |
55 | 30 | } |
56 | ||
57 | /** | |
58 | * Constructor, using the default (i.e. <code>null</code>) language. | |
59 | * | |
60 | * @param expression The expression itself. | |
61 | * @since 2.2.0 | |
62 | */ | |
63 | public Expression(String expression) { | |
64 | 5 | this(expression, null); |
65 | 5 | } |
66 | ||
67 | /** | |
68 | * Copy constructor. | |
69 | * | |
70 | * @param toCopy The expression to copy. | |
71 | * @since 2.2.0 | |
72 | */ | |
73 | 11 | public Expression(Expression toCopy) { |
74 | 11 | this.expression = toCopy.expression; |
75 | 11 | this.language = toCopy.language; |
76 | 11 | } |
77 | ||
78 | /** | |
79 | * Creates an Expression object from a string in the form | |
80 | * <code>LANGUAGE:EXPRESSION</code>. | |
81 | * | |
82 | * @param describedExpression The expression in the form | |
83 | * <code>LANGUAGE:EXPRESSION</code>. The LANGUAGE part should be expressed | |
84 | * only with letters and numbers. | |
85 | * @return The created object, or <code>null</code> if the expression is null. | |
86 | * @since 2.2.0 | |
87 | */ | |
88 | public static Expression createExpressionFromDescribedExpression(String describedExpression) { | |
89 | 5 | if (describedExpression != null) { |
90 | 4 | String language = null; |
91 | 4 | String expression = describedExpression; |
92 | 4 | if (describedExpression.matches("[a-zA-Z0-9]+:.+")) { |
93 | 2 | language = describedExpression.substring(0, describedExpression.indexOf(':')); |
94 | 2 | expression = describedExpression.substring(describedExpression.indexOf(':') + 1); |
95 | } | |
96 | 4 | return new Expression(expression, language); |
97 | } | |
98 | ||
99 | 1 | return null; |
100 | } | |
101 | ||
102 | /** | |
103 | * Creates an Expression object from the expression and its language. | |
104 | * | |
105 | * @param expression The expression itself. | |
106 | * @param language The language of the expression. | |
107 | * @return The created object, or <code>null</code> if the expression is null. | |
108 | * @since 2.2.0 | |
109 | */ | |
110 | public static Expression createExpression(String expression, String language) { | |
111 | 7 | if (expression != null) { |
112 | 6 | return new Expression(expression, language); |
113 | } | |
114 | ||
115 | 1 | return null; |
116 | } | |
117 | ||
118 | /** | |
119 | * Returns the expression string. | |
120 | * | |
121 | * @return The expression itself. | |
122 | * @since 2.2.0 | |
123 | */ | |
124 | public String getExpression() { | |
125 | 19 | return expression; |
126 | } | |
127 | ||
128 | /** | |
129 | * Returns the language in which the expression is expressed. | |
130 | * | |
131 | * @return The expression language. | |
132 | * @since 2.2.0 | |
133 | */ | |
134 | public String getLanguage() { | |
135 | 17 | return language; |
136 | } | |
137 | ||
138 | /** {@inheritDoc} */ | |
139 | @Override | |
140 | public boolean equals(Object obj) { | |
141 | 6 | Expression exp = (Expression) obj; |
142 | 6 | return nullSafeEquals(expression, exp.expression) |
143 | && nullSafeEquals(language, exp.language); | |
144 | } | |
145 | ||
146 | /** {@inheritDoc} */ | |
147 | @Override | |
148 | public int hashCode() { | |
149 | 3 | return nullSafeHashCode(expression) + nullSafeHashCode(language); |
150 | } | |
151 | ||
152 | /** {@inheritDoc} */ | |
153 | @Override | |
154 | public String toString() { | |
155 | 2 | return (language == null ? "DEFAULT" : language) + ":" + expression; |
156 | } | |
157 | } |