View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.api.core.domain;
17  
18  import java.io.ObjectStreamException;
19  import java.io.Serializable;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  /**
24   * Provides information about the expression to evaluate a default value for a
25   * property.
26   */
27  public final class PropertyExpression implements Serializable
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    /**
34     * The class version identifier.
35     */
36    private static final long serialVersionUID = 1L;
37  
38    /**
39     * The expression instance to signal that there is no property expression.
40     */
41    public static final PropertyExpression NO_EXPRESSION =
42        new PropertyExpression(null);
43  
44    // --- members --------------------------------------------------------------
45  
46    /**
47     * The expression string. May be <code>null</code>.
48     *
49     * @serial
50     */
51    private final String expression;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     *
60     * @param expression the expression string.
61     */
62    private PropertyExpression(final String expression)
63    {
64      this.expression = expression;
65    }
66  
67    // ****************************** Inner Classes *****************************
68  
69    // ********************************* Methods ********************************
70  
71    // --- init -----------------------------------------------------------------
72  
73    // --- create ---------------------------------------------------------------
74  
75    /**
76     * Creates an instance of {@link PropertyExpression} with the given expression
77     * string.
78     *
79     * @param expression the expression string.
80     * @return the created instance. If the {@code expression} is blank, the
81     *         singleton expression {@link #NO_EXPRESSION} is returned.
82     */
83    public static PropertyExpression create(final String expression)
84    {
85      if (StringUtils.isBlank(expression))
86      {
87        return NO_EXPRESSION;
88      }
89  
90      return new PropertyExpression(expression);
91    }
92  
93    // --- get&set --------------------------------------------------------------
94  
95    /**
96     * Checks if an expression string is present.
97     *
98     * @return <code>true</code> if the expression string is not <code>null</code>
99     *         , <code>false</code> if it is <code>null</code>.
100    */
101   public boolean hasExpression()
102   {
103     return expression != null;
104   }
105 
106   /**
107    * Returns the expression string.
108    *
109    * @return the expression string. May be <code>null</code>.
110    */
111   public String getExpression()
112   {
113     return expression;
114   }
115 
116   // --- business -------------------------------------------------------------
117 
118   // --- object basics --------------------------------------------------------
119 
120   /**
121    * Special treatment for serializable singletons.
122    *
123    * @return returns a reference to the (truly) unique singleton.
124    */
125   private Object readResolve() throws ObjectStreamException
126   {
127     if (StringUtils.isBlank(expression))
128     {
129       return NO_EXPRESSION;
130     }
131 
132     return this;
133   }
134 
135   /**
136    * Returns the name of the type.
137    *
138    * @return the name of the type.
139    */
140   @Override
141   public String toString()
142   {
143     return expression != null ? expression : StringUtils.EMPTY;
144   }
145 }