001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.foundation;
022:
023: /**
024: * @sharpen.ignore
025: */
026: public class Coercion4 {
027: public static Object toSByte(Object obj) {
028: if (obj instanceof Byte) {
029: return obj;
030: }
031: if (obj instanceof Number) {
032: Number number = (Number) obj;
033: if (number.byteValue() == number.doubleValue()) {
034: return new Byte((number).byteValue());
035: }
036: }
037: return No4.INSTANCE;
038: }
039:
040: public static Object toShort(Object obj) {
041: if (obj instanceof Short) {
042: return obj;
043: }
044: if (obj instanceof Number) {
045: Number number = (Number) obj;
046: if (number.shortValue() == number.doubleValue()) {
047: return new Short((number).shortValue());
048: }
049: }
050: return No4.INSTANCE;
051: }
052:
053: public static Object toInt(Object obj) {
054: if (obj instanceof Integer) {
055: return obj;
056: }
057: if (obj instanceof Number) {
058: Number number = (Number) obj;
059: if (number.intValue() == number.doubleValue()) {
060: return new Integer((number).intValue());
061: }
062: }
063: return No4.INSTANCE;
064: }
065:
066: public static Object toLong(Object obj) {
067: if (obj instanceof Long) {
068: return obj;
069: }
070: if (obj instanceof Number) {
071: Number number = (Number) obj;
072: if (number.longValue() == number.doubleValue()) {
073: return new Long((number).longValue());
074: }
075: }
076: return No4.INSTANCE;
077: }
078:
079: public static Object toFloat(Object obj) {
080: if (obj instanceof Float) {
081: return obj;
082: }
083: if (obj instanceof Number) {
084: Number number = (Number) obj;
085: if (number.floatValue() == number.doubleValue()) {
086: return new Float((number).floatValue());
087: }
088: }
089: return No4.INSTANCE;
090: }
091:
092: public static Object toDouble(Object obj) {
093: if (obj instanceof Double) {
094: return obj;
095: }
096: if (obj instanceof Number) {
097: Number number = (Number) obj;
098: return new Double((number).doubleValue());
099: }
100: return No4.INSTANCE;
101: }
102: }
|