001/* 002 * JKNIV, utils - Helper utilities for jdk code. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.reflect; 021 022import java.math.BigDecimal; 023import java.math.BigInteger; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.concurrent.atomic.AtomicInteger; 027import java.util.concurrent.atomic.AtomicLong; 028 029public final class NumberFactory 030{ 031 private static Map<String, Numerical> MAP = new HashMap<String, Numerical>(); 032 033 static { 034 MAP.put(Integer.class.getCanonicalName(), FactoryInteger.instance); 035 MAP.put(Long.class.getCanonicalName(), FactoryLong.instance); 036 MAP.put(Double.class.getCanonicalName(), FactoryDouble.instance); 037 MAP.put(Float.class.getCanonicalName(), FactoryFloat.instance); 038 MAP.put(BigDecimal.class.getCanonicalName(), FactoryBigDecimal.instance); 039 MAP.put(Short.class.getCanonicalName(), FactoryShort.instance); 040 MAP.put(BigInteger.class.getCanonicalName(), FactoryBigInteger.instance); 041 MAP.put(AtomicLong.class.getCanonicalName(), FactoryAtomicLong.instance); 042 MAP.put(AtomicInteger.class.getCanonicalName(), FactoryAtomicInteger.instance); 043 MAP.put(Byte.class.getCanonicalName(), FactoryByte.instance); 044 } 045 046 public static Numerical getInstance(Number n) 047 { 048 Numerical factory = new FactoryLong(); 049 if (n != null) 050 { 051 factory = MAP.get(n.getClass().getCanonicalName()); 052 if (factory == null) 053 throw new UnsupportedOperationException("Cannot build a factory number to ["+n.getClass()+"] type"); 054 /* 055 if (n instanceof Integer) 056 factory = new FactoryInteger(); 057 else if (n instanceof Long) 058 factory = new FactoryLong(); 059 else if (n instanceof Double) 060 factory = new FactoryDouble(); 061 else if (n instanceof Float) 062 factory = new FactoryFloat(); 063 else if (n instanceof BigDecimal) 064 factory = new FactoryBigDecimal(); 065 else if (n instanceof Short) 066 factory = new FactoryShort(); 067 else if (n instanceof BigInteger) 068 factory = new FactoryBigInteger(); 069 else if (n instanceof AtomicLong) 070 factory = new FactoryAtomicLong(); 071 else if (n instanceof AtomicInteger) 072 factory = new FactoryAtomicInteger(); 073 else if (n instanceof Byte) 074 factory = new FactoryByte(); 075 else 076 throw new UnsupportedOperationException("Cannot build a factory number to ["+n.getClass()+"] type"); 077 */ 078 // TODO new java 8 number types 079 /* 080 else if ("java.util.concurrent.atomic.DoubleAccumulator".equals(type.getCanonicalName())) 081 isNumber = true; 082 else if ("java.util.concurrent.atomic.DoubleAdder".equals(type.getCanonicalName())) 083 isNumber = true; 084 else if ("java.util.concurrent.atomic.LongAccumulator".equals(type.getCanonicalName())) 085 isNumber = true; 086 else if ("java.util.concurrent.atomic.LongAdder".equals(type.getCanonicalName())) 087 isNumber = true; 088 */ 089 } 090 return factory; 091 } 092 093 public static Numerical getInstance(String n) 094 { 095 Numerical factory = new FactoryLong(); 096 if (n != null) 097 { 098 factory = MAP.get(n); 099 if (factory == null) 100 throw new UnsupportedOperationException("Cannot build a factory number to ["+n.getClass()+"] type"); 101 102 /* 103 if (n.equals(Integer.class.getCanonicalName())) 104 factory = new FactoryInteger(); 105 else if (n.equals(Long.class.getCanonicalName())) 106 factory = new FactoryLong(); 107 else if (n.equals(Double.class.getCanonicalName())) 108 factory = new FactoryDouble(); 109 else if (n.equals(Float.class.getCanonicalName())) 110 factory = new FactoryFloat(); 111 else if (n.equals(BigDecimal.class.getCanonicalName())) 112 factory = new FactoryBigDecimal(); 113 else if (n.equals(Short.class.getCanonicalName())) 114 factory = new FactoryShort(); 115 else if (n.equals(BigInteger.class.getCanonicalName())) 116 factory = new FactoryBigInteger(); 117 else if (n.equals(AtomicLong.class.getCanonicalName())) 118 factory = new FactoryAtomicLong(); 119 else if (n.equals(AtomicInteger.class.getCanonicalName())) 120 factory = new FactoryAtomicInteger(); 121 else if (n.equals(Byte.class.getCanonicalName())) 122 factory = new FactoryByte(); 123 else 124 throw new UnsupportedOperationException("Cannot build a factory number to ["+n.getClass()+"] type"); 125 */ 126 //TODO new java 8 number types 127 /* 128 else if ("java.util.concurrent.atomic.DoubleAccumulator".equals(type.getCanonicalName())) 129 isNumber = true; 130 else if ("java.util.concurrent.atomic.DoubleAdder".equals(type.getCanonicalName())) 131 isNumber = true; 132 else if ("java.util.concurrent.atomic.LongAccumulator".equals(type.getCanonicalName())) 133 isNumber = true; 134 else if ("java.util.concurrent.atomic.LongAdder".equals(type.getCanonicalName())) 135 isNumber = true; 136 */ 137 } 138 return factory; 139 } 140 141}