PI constant added to GREL

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1904 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-11-18 23:53:07 +00:00
parent ed724fd191
commit 1ec7cb9f7b

View File

@ -23,8 +23,8 @@ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@ -51,37 +51,38 @@ import com.google.refine.model.Row;
public class ExpressionUtils { public class ExpressionUtils {
static protected Set<Binder> s_binders = new HashSet<Binder>(); static protected Set<Binder> s_binders = new HashSet<Binder>();
static public void registerBinder(Binder binder) { static public void registerBinder(Binder binder) {
s_binders.add(binder); s_binders.add(binder);
} }
static public Properties createBindings(Project project) { static public Properties createBindings(Project project) {
Properties bindings = new Properties(); Properties bindings = new Properties();
bindings.put("true", true); bindings.put("true", true);
bindings.put("false", false); bindings.put("false", false);
bindings.put("PI", Math.PI);
bindings.put("project", project); bindings.put("project", project);
for (Binder binder : s_binders) { for (Binder binder : s_binders) {
binder.initializeBindings(bindings, project); binder.initializeBindings(bindings, project);
} }
return bindings; return bindings;
} }
static public void bind(Properties bindings, Row row, int rowIndex, String columnName, Cell cell) { static public void bind(Properties bindings, Row row, int rowIndex, String columnName, Cell cell) {
Project project = (Project) bindings.get("project"); Project project = (Project) bindings.get("project");
bindings.put("rowIndex", rowIndex); bindings.put("rowIndex", rowIndex);
bindings.put("row", new WrappedRow(project, rowIndex, row)); bindings.put("row", new WrappedRow(project, rowIndex, row));
bindings.put("cells", new CellTuple(project, row)); bindings.put("cells", new CellTuple(project, row));
if (columnName != null) { if (columnName != null) {
bindings.put("columnName", columnName); bindings.put("columnName", columnName);
} }
if (cell == null) { if (cell == null) {
bindings.remove("cell"); bindings.remove("cell");
bindings.remove("value"); bindings.remove("value");
@ -93,12 +94,12 @@ public class ExpressionUtils {
bindings.put("value", cell.value); bindings.put("value", cell.value);
} }
} }
for (Binder binder : s_binders) { for (Binder binder : s_binders) {
binder.bind(bindings, row, rowIndex, columnName, cell); binder.bind(bindings, row, rowIndex, columnName, cell);
} }
} }
static public boolean isError(Object o) { static public boolean isError(Object o) {
return o != null && o instanceof EvalError; return o != null && o instanceof EvalError;
} }
@ -108,19 +109,19 @@ public class ExpressionUtils {
} }
*/ */
static public boolean isNonBlankData(Object o) { static public boolean isNonBlankData(Object o) {
return return
o != null && o != null &&
!(o instanceof EvalError) && !(o instanceof EvalError) &&
(!(o instanceof String) || ((String) o).length() > 0); (!(o instanceof String) || ((String) o).length() > 0);
} }
static public boolean isTrue(Object o) { static public boolean isTrue(Object o) {
return o != null && return o != null &&
(o instanceof Boolean ? (o instanceof Boolean ?
((Boolean) o).booleanValue() : ((Boolean) o).booleanValue() :
Boolean.parseBoolean(o.toString())); Boolean.parseBoolean(o.toString()));
} }
static public boolean sameValue(Object v1, Object v2) { static public boolean sameValue(Object v1, Object v2) {
if (v1 == null) { if (v1 == null) {
return (v2 == null) return (v2 == null)
@ -132,7 +133,7 @@ public class ExpressionUtils {
return v1.equals(v2); return v1.equals(v2);
} }
} }
static public boolean isStorable(Object v) { static public boolean isStorable(Object v) {
return v == null || return v == null ||
v instanceof Number || v instanceof Number ||
@ -142,36 +143,36 @@ public class ExpressionUtils {
v instanceof Calendar || v instanceof Calendar ||
v instanceof EvalError; v instanceof EvalError;
} }
static public Serializable wrapStorable(Object v) { static public Serializable wrapStorable(Object v) {
if (v instanceof JSONArray) { if (v instanceof JSONArray) {
return ((JSONArray) v).toString(); return ((JSONArray) v).toString();
} else if (v instanceof JSONObject) { } else if (v instanceof JSONObject) {
return ((JSONObject) v).toString(); return ((JSONObject) v).toString();
} else { } else {
return isStorable(v) ? return isStorable(v) ?
(Serializable) v : (Serializable) v :
new EvalError(v.getClass().getSimpleName() + " value not storable"); new EvalError(v.getClass().getSimpleName() + " value not storable");
} }
} }
static public boolean isArray(Object v) { static public boolean isArray(Object v) {
return v != null && v.getClass().isArray(); return v != null && v.getClass().isArray();
} }
static public boolean isArrayOrCollection(Object v) { static public boolean isArrayOrCollection(Object v) {
return v != null && (v.getClass().isArray() || v instanceof Collection<?>); return v != null && (v.getClass().isArray() || v instanceof Collection<?>);
} }
static public boolean isArrayOrList(Object v) { static public boolean isArrayOrList(Object v) {
return v != null && (v.getClass().isArray() || v instanceof List<?>); return v != null && (v.getClass().isArray() || v instanceof List<?>);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static public List<Object> toObjectList(Object v) { static public List<Object> toObjectList(Object v) {
return (List<Object>) v; return (List<Object>) v;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static public Collection<Object> toObjectCollection(Object v) { static public Collection<Object> toObjectCollection(Object v) {
return (Collection<Object>) v; return (Collection<Object>) v;