From: <lh...@us...> - 2008-09-05 12:01:33
|
Revision: 156 http://tinytim.svn.sourceforge.net/tinytim/?rev=156&view=rev Author: lheuer Date: 2008-09-05 12:01:41 +0000 (Fri, 05 Sep 2008) Log Message: ----------- Added dedicated DecimalLiteral and IntegerLiteral Modified Paths: -------------- tinytim/trunk/src/main/java/org/tinytim/core/Literal.java Added Paths: ----------- tinytim/trunk/src/main/java/org/tinytim/core/DecimalLiteral.java tinytim/trunk/src/main/java/org/tinytim/core/IntegerLiteral.java Added: tinytim/trunk/src/main/java/org/tinytim/core/DecimalLiteral.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/core/DecimalLiteral.java (rev 0) +++ tinytim/trunk/src/main/java/org/tinytim/core/DecimalLiteral.java 2008-09-05 12:01:41 UTC (rev 156) @@ -0,0 +1,115 @@ +/* + * This is tinyTiM, a tiny Topic Maps engine. + * + * Copyright (C) 2008 Lars Heuer (heuer[at]semagia.com) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ +package org.tinytim.core; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import org.tinytim.voc.XSD; +import org.tmapi.core.Locator; + +/** + * + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev:$ - $Date:$ + */ +final class DecimalLiteral implements ILiteral { + + private final BigDecimal _decimal; + private final String _lexicalForm; + + public DecimalLiteral(BigDecimal decimal) { + _decimal = decimal; + _lexicalForm = LiteralNormalizer.normalizeDecimal(decimal.toPlainString()); + } + + public DecimalLiteral(String value) { + _lexicalForm = LiteralNormalizer.normalizeDecimal(value); + _decimal = new BigDecimal(_lexicalForm); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#decimalValue() + */ + public BigDecimal decimalValue() { + return _decimal; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#floatValue() + */ + public float floatValue() { + return _decimal.floatValue(); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#getDatatype() + */ + public Locator getDatatype() { + return XSD.DECIMAL; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#getValue() + */ + public String getValue() { + return _lexicalForm; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#intValue() + */ + public int intValue() { + return _decimal.intValue(); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#integerValue() + */ + public BigInteger integerValue() { + return _decimal.toBigInteger(); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#longValue() + */ + public long longValue() { + return _decimal.longValue(); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return _decimal.hashCode(); + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + return this == obj || (obj instanceof DecimalLiteral) && ((DecimalLiteral) obj)._lexicalForm.equals(_lexicalForm); + } + +} Property changes on: tinytim/trunk/src/main/java/org/tinytim/core/DecimalLiteral.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Added: tinytim/trunk/src/main/java/org/tinytim/core/IntegerLiteral.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/core/IntegerLiteral.java (rev 0) +++ tinytim/trunk/src/main/java/org/tinytim/core/IntegerLiteral.java 2008-09-05 12:01:41 UTC (rev 156) @@ -0,0 +1,119 @@ +/* + * This is tinyTiM, a tiny Topic Maps engine. + * + * Copyright (C) 2008 Lars Heuer (heuer[at]semagia.com) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ +package org.tinytim.core; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import org.tinytim.voc.XSD; +import org.tmapi.core.Locator; + +/** + * + * + * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a> + * @version $Rev:$ - $Date:$ + */ +final class IntegerLiteral implements ILiteral { + + private final BigInteger _integer; + private final String _lexicalForm; + private BigDecimal _decimal; + + public IntegerLiteral(BigInteger value) { + _integer = value; + _lexicalForm = LiteralNormalizer.normalizeInteger(value.toString()); + } + + public IntegerLiteral(String value) { + _lexicalForm = LiteralNormalizer.normalizeInteger(value); + _integer = new BigInteger(_lexicalForm); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#decimalValue() + */ + public BigDecimal decimalValue() { + if (_decimal == null) { + _decimal = new BigDecimal(_integer); + } + return _decimal; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#floatValue() + */ + public float floatValue() { + return _integer.floatValue(); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#getDatatype() + */ + public Locator getDatatype() { + return XSD.INTEGER; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#getValue() + */ + public String getValue() { + return _lexicalForm; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#intValue() + */ + public int intValue() { + return _integer.intValue(); + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#integerValue() + */ + public BigInteger integerValue() { + return _integer; + } + + /* (non-Javadoc) + * @see org.tinytim.core.ILiteral#longValue() + */ + public long longValue() { + return _integer.longValue(); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return _integer.hashCode(); + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + return this == obj || (obj instanceof IntegerLiteral) && ((IntegerLiteral) obj)._lexicalForm.equals(_lexicalForm); + } + +} Property changes on: tinytim/trunk/src/main/java/org/tinytim/core/IntegerLiteral.java ___________________________________________________________________ Added: svn:keywords + Rev Date Id Added: svn:eol-style + native Modified: tinytim/trunk/src/main/java/org/tinytim/core/Literal.java =================================================================== --- tinytim/trunk/src/main/java/org/tinytim/core/Literal.java 2008-09-04 13:28:44 UTC (rev 155) +++ tinytim/trunk/src/main/java/org/tinytim/core/Literal.java 2008-09-05 12:01:41 UTC (rev 156) @@ -107,6 +107,12 @@ if (XSD.STRING.equals(datatype)) { return create(value); } + if (XSD.DECIMAL.equals(datatype)) { + return createDecimal(value); + } + if (XSD.INTEGER.equals(datatype)) { + return createInteger(value); + } ILiteral literal = new Literal(value, datatype); ILiteral existing = _OTHERS.get(literal); if (existing != null) { @@ -127,11 +133,11 @@ } public static ILiteral create(BigDecimal value) { - return create(value, XSD.DECIMAL); + return createDecimal(value); } public static ILiteral create(BigInteger value) { - return create(value, XSD.INTEGER); + return createInteger(value); } public static ILiteral create(float value) { @@ -174,6 +180,58 @@ return iri; } + public static synchronized ILiteral createDecimal(String value) { + if (value == null) { + throw new IllegalArgumentException("The value must not be null"); + } + ILiteral lit = new DecimalLiteral(value); + ILiteral existing = _OTHERS.get(lit); + if (existing != null) { + return existing; + } + _OTHERS.add(lit); + return lit; + } + + public static synchronized ILiteral createDecimal(BigDecimal value) { + if (value == null) { + throw new IllegalArgumentException("The value must not be null"); + } + ILiteral lit = new DecimalLiteral(value); + ILiteral existing = _OTHERS.get(lit); + if (existing != null) { + return existing; + } + _OTHERS.add(lit); + return lit; + } + + public static synchronized ILiteral createInteger(String value) { + if (value == null) { + throw new IllegalArgumentException("The value must not be null"); + } + ILiteral lit = new IntegerLiteral(value); + ILiteral existing = _OTHERS.get(lit); + if (existing != null) { + return existing; + } + _OTHERS.add(lit); + return lit; + } + + public static synchronized ILiteral createInteger(BigInteger value) { + if (value == null) { + throw new IllegalArgumentException("The value must not be null"); + } + ILiteral lit = new IntegerLiteral(value); + ILiteral existing = _OTHERS.get(lit); + if (existing != null) { + return existing; + } + _OTHERS.add(lit); + return lit; + } + public BigDecimal decimalValue() { return new BigDecimal(_value); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |