/*******************************************************************************
*
* Copyright 2010 Alexandru Craciun, and individual contributors as indicated
* by the @authors tag.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
******************************************************************************/
package org.netxilia.api.formula;
import java.io.Serializable;
/**
* This class contains a Excel-like formula
*
* @author <a href='mailto:ax.craciun@gmail.com'>Alexandru Craciun</a>
*
*/
public class Formula implements Serializable {
private static final long serialVersionUID = -2235033412927337805L;
private final String formula;
public Formula(String formula) {
if (!isFormula(formula)) {
throw new IllegalArgumentException("The formula has to start with a '='");
}
this.formula = formula;
}
public String getFormula() {
return formula;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((formula == null) ? 0 : formula.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Formula other = (Formula) obj;
if (formula == null) {
if (other.formula != null)
return false;
} else if (!formula.equals(other.formula))
return false;
return true;
}
@Override
public String toString() {
return formula;
}
public boolean isEmpty() {
return formula.isEmpty();
}
public static boolean isFormula(String formula) {
return formula != null && formula.length() > 0 && formula.startsWith("=");
}
public static Formula valueOf(String s) {
if (s == null)
return null;
return new Formula(s);
}
}