001/* 002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>. 003 * 004 * This file is part of 005 * CleanSheets - a spreadsheet application for the Java platform. 006 * 007 * CleanSheets is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 2 of the License, or 010 * (at your option) any later version. 011 * 012 * CleanSheets is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 */ 021package csheets.core.formula; 022 023import csheets.core.Value; 024import csheets.core.formula.util.ExpressionVisitor; 025 026/** 027 * A literal value in a formula. 028 * @author Einar Pehrson 029 */ 030public class Literal implements Expression { 031 032 /** The unique version identifier used for serialization */ 033 private static final long serialVersionUID = 7854180857828149858L; 034 035 /** The value of the literal */ 036 private Value value; 037 038 /** 039 * Creates a new literal. 040 * @param value the value of literal 041 */ 042 public Literal(Value value) { 043 this.value = value; 044 } 045 046 public Value evaluate() { 047 return value; 048 } 049 050 /** 051 * Returns the value of the literal. 052 * @return the value of the literal 053 */ 054 public Value getValue() { 055 return value; 056 } 057 058 public Object accept(ExpressionVisitor visitor) { 059 return visitor.visitLiteral(this); 060 } 061 062 public String toString() { 063 if (value.getType() == Value.Type.TEXT 064 || value.getType() == Value.Type.DATE) 065 return "\"" + value.toString() + "\""; 066 else 067 return value.toString(); 068 } 069}