Example usage for java.math MathContext MathContext

List of usage examples for java.math MathContext MathContext

Introduction

In this page you can find the example usage for java.math MathContext MathContext.

Prototype

public MathContext(String val) 

Source Link

Document

Constructs a new MathContext from a string.

Usage

From source file:org.alfresco.serializers.types.DefaultTypeConverter.java

@SuppressWarnings("rawtypes")
private DefaultTypeConverter() {
    ////w  w w .ja  v a2 s. co m
    // From string
    //
    addConverter(String.class, Class.class, new TypeConverter.Converter<String, Class>() {
        public Class convert(String source) {
            try {
                return Class.forName(source);
            } catch (ClassNotFoundException e) {
                throw new TypeConversionException("Failed to convert string to class: " + source, e);
            }
        }
    });
    addConverter(String.class, Boolean.class, new TypeConverter.Converter<String, Boolean>() {
        public Boolean convert(String source) {
            return Boolean.valueOf(source);
        }
    });
    addConverter(String.class, Character.class, new TypeConverter.Converter<String, Character>() {
        public Character convert(String source) {
            if ((source == null) || (source.length() == 0)) {
                return null;
            }
            return Character.valueOf(source.charAt(0));
        }
    });
    addConverter(String.class, Number.class, new TypeConverter.Converter<String, Number>() {
        public Number convert(String source) {
            try {
                return DecimalFormat.getNumberInstance().parse(source);
            } catch (ParseException e) {
                throw new TypeConversionException("Failed to parse number " + source, e);
            }
        }
    });
    addConverter(String.class, Byte.class, new TypeConverter.Converter<String, Byte>() {
        public Byte convert(String source) {
            return Byte.valueOf(source);
        }
    });
    addConverter(String.class, Short.class, new TypeConverter.Converter<String, Short>() {
        public Short convert(String source) {
            return Short.valueOf(source);
        }
    });
    addConverter(String.class, Integer.class, new TypeConverter.Converter<String, Integer>() {
        public Integer convert(String source) {
            return Integer.valueOf(source);
        }
    });
    addConverter(String.class, Long.class, new TypeConverter.Converter<String, Long>() {
        public Long convert(String source) {
            return Long.valueOf(source);
        }
    });
    addConverter(String.class, Float.class, new TypeConverter.Converter<String, Float>() {
        public Float convert(String source) {
            return Float.valueOf(source);
        }
    });
    addConverter(String.class, Double.class, new TypeConverter.Converter<String, Double>() {
        public Double convert(String source) {
            return Double.valueOf(source);
        }
    });
    addConverter(String.class, BigInteger.class, new TypeConverter.Converter<String, BigInteger>() {
        public BigInteger convert(String source) {
            return new BigInteger(source);
        }
    });
    addConverter(String.class, BigDecimal.class, new TypeConverter.Converter<String, BigDecimal>() {
        public BigDecimal convert(String source) {
            return new BigDecimal(source);
        }
    });
    addConverter(BasicDBObject.class, BigDecimal.class,
            new TypeConverter.Converter<BasicDBObject, BigDecimal>() {
                public BigDecimal convert(BasicDBObject source) {
                    String type = (String) source.get("t");
                    if (type.equals("FIXED_POINT")) {
                        String number = (String) source.get("n");
                        Integer precision = (Integer) source.get("p");
                        MathContext ctx = new MathContext(precision);
                        return new BigDecimal(number, ctx);
                    } else {
                        throw new IllegalArgumentException("Invalid source object for conversion " + source
                                + ", expected a Fixed Decimal object");
                    }
                }
            });
    addConverter(String.class, Date.class, new TypeConverter.Converter<String, Date>() {
        public Date convert(String source) {
            try {
                Date date = ISO8601DateFormat.parse(source);
                return date;
            } catch (PlatformRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            } catch (AlfrescoRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            }
        }
    });
    addConverter(String.class, Duration.class, new TypeConverter.Converter<String, Duration>() {
        public Duration convert(String source) {
            return new Duration(source);
        }
    });
    addConverter(String.class, QName.class, new TypeConverter.Converter<String, QName>() {
        public QName convert(String source) {
            return QName.createQName(source);
        }
    });
    addConverter(BasicDBObject.class, QName.class, new TypeConverter.Converter<BasicDBObject, QName>() {
        public QName convert(BasicDBObject source) {
            String type = (String) source.get("t");
            if (type.equals("QNAME")) {
                String qname = (String) source.get("v");
                return QName.createQName(qname);
            } else {
                throw new IllegalArgumentException();
            }
        }
    });
    addConverter(String.class, ContentData.class, new TypeConverter.Converter<String, ContentData>() {
        public ContentData convert(String source) {
            return ContentData.createContentProperty(source);
        }
    });
    addConverter(String.class, NodeRef.class, new TypeConverter.Converter<String, NodeRef>() {
        public NodeRef convert(String source) {
            return new NodeRef(source);
        }
    });
    addConverter(BasicDBObject.class, NodeRef.class, new TypeConverter.Converter<BasicDBObject, NodeRef>() {
        public NodeRef convert(BasicDBObject source) {
            String type = (String) source.get("t");
            if (!type.equals("NODEREF")) {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a NodeRef object");
            }
            String protocol = (String) source.get("p");
            String storeId = (String) source.get("s");
            String id = (String) source.get("id");
            NodeRef nodeRef = new NodeRef(new StoreRef(protocol, storeId), id);
            return nodeRef;
        }
    });
    addConverter(String.class, StoreRef.class, new TypeConverter.Converter<String, StoreRef>() {
        public StoreRef convert(String source) {
            return new StoreRef(source);
        }
    });
    addConverter(BasicDBObject.class, StoreRef.class, new TypeConverter.Converter<BasicDBObject, StoreRef>() {
        public StoreRef convert(BasicDBObject source) {
            String type = (String) source.get("t");
            if (!type.equals("STOREREF")) {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a StoreRef object");
            }
            String protocol = (String) source.get("p");
            String storeId = (String) source.get("s");
            return new StoreRef(protocol, storeId);
        }
    });
    addConverter(String.class, ChildAssociationRef.class,
            new TypeConverter.Converter<String, ChildAssociationRef>() {
                public ChildAssociationRef convert(String source) {
                    return new ChildAssociationRef(source);
                }
            });
    addConverter(String.class, AssociationRef.class, new TypeConverter.Converter<String, AssociationRef>() {
        public AssociationRef convert(String source) {
            return new AssociationRef(source);
        }
    });
    addConverter(String.class, InputStream.class, new TypeConverter.Converter<String, InputStream>() {
        public InputStream convert(String source) {
            try {
                return new ByteArrayInputStream(source.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new TypeConversionException("Encoding not supported", e);
            }
        }
    });
    addConverter(String.class, MLText.class, new TypeConverter.Converter<String, MLText>() {
        public MLText convert(String source) {
            return new MLText(source);
        }
    });
    addConverter(BasicDBObject.class, MLText.class, new TypeConverter.Converter<BasicDBObject, MLText>() {
        public MLText convert(BasicDBObject source) {
            String type = (String) source.get("t");
            if (!type.equals("MLTEXT")) {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a NodeRef object");
            }

            MLText mlText = new MLText();
            for (String languageTag : source.keySet()) {
                String text = (String) source.get(languageTag);
                Locale locale = Locale.forLanguageTag(languageTag);
                mlText.put(locale, text);
            }

            return mlText;
        }
    });
    //        addConverter(BasicDBObject.class, ContentDataWithId.class, new TypeConverter.Converter<BasicDBObject, ContentDataWithId>()
    //        {
    //            public ContentDataWithId convert(BasicDBObject source)
    //            {
    //                String type = (String)source.get("t");
    //                if(!type.equals("CONTENT_DATA_ID"))
    //                {
    //                    throw new IllegalArgumentException("Invalid source object for conversion "
    //                            + source 
    //                            + ", expected a ContentDataWithId object");
    //                }
    //                String contentUrl = (String)source.get("u");
    //                String mimeType = (String)source.get("m");
    //                Long size = (Long)source.get("s");
    //                String encoding = (String)source.get("e");
    //                String languageTag = (String)source.get("l");
    //                Long id = (Long)source.get("id");
    //                Locale locale = Locale.forLanguageTag(languageTag);
    //
    //                ContentData contentData = new ContentData(contentUrl, mimeType, size, encoding, locale);
    //                ContentDataWithId contentDataWithId = new ContentDataWithId(contentData, id);
    //                return contentDataWithId;
    //            }
    //        });
    addConverter(BasicDBObject.class, ContentData.class,
            new TypeConverter.Converter<BasicDBObject, ContentData>() {
                public ContentData convert(BasicDBObject source) {
                    ContentData contentData = null;

                    String type = (String) source.get("t");
                    if (type.equals("CONTENT")) {
                        String contentUrl = (String) source.get("u");
                        String mimeType = (String) source.get("m");
                        Long size = (Long) source.get("s");
                        String encoding = (String) source.get("e");
                        String languageTag = (String) source.get("l");
                        Locale locale = Locale.forLanguageTag(languageTag);
                        contentData = new ContentData(contentUrl, mimeType, size, encoding, locale);
                    } else if (type.equals("CONTENT_DATA_ID")) {
                        String contentUrl = (String) source.get("u");
                        String mimeType = (String) source.get("m");
                        Long size = (Long) source.get("s");
                        String encoding = (String) source.get("e");
                        String languageTag = (String) source.get("l");
                        Locale locale = Locale.forLanguageTag(languageTag);
                        contentData = new ContentData(contentUrl, mimeType, size, encoding, locale);
                    } else {
                        throw new IllegalArgumentException("Invalid source object for conversion " + source
                                + ", expected a ContentData object");
                    }

                    return contentData;
                }
            });
    addConverter(BasicDBObject.class, String.class, new TypeConverter.Converter<BasicDBObject, String>() {
        public String convert(BasicDBObject source) {
            // TODO distinguish between different BasicDBObject representations e.g. for MLText, ...

            Set<String> languageTags = source.keySet();
            if (languageTags.size() == 0) {
                throw new IllegalArgumentException("Persisted MLText is invalid " + source);
            } else if (languageTags.size() > 1) {
                // TODO
                logger.warn("Persisted MLText has more than 1 locale " + source);
            }

            String languageTag = languageTags.iterator().next();
            String text = source.getString(languageTag);
            return text;
        }
    });
    addConverter(String.class, Locale.class, new TypeConverter.Converter<String, Locale>() {
        public Locale convert(String source) {
            return I18NUtil.parseLocale(source);
        }
    });
    addConverter(String.class, Period.class, new TypeConverter.Converter<String, Period>() {
        public Period convert(String source) {
            return new Period(source);
        }
    });
    addConverter(String.class, VersionNumber.class, new TypeConverter.Converter<String, VersionNumber>() {
        public VersionNumber convert(String source) {
            return new VersionNumber(source);
        }
    });

    //
    // From Locale
    //
    addConverter(Locale.class, String.class, new TypeConverter.Converter<Locale, String>() {
        public String convert(Locale source) {
            String localeStr = source.toString();
            if (localeStr.length() < 6) {
                localeStr += "_";
            }
            return localeStr;
        }
    });

    //
    // From VersionNumber
    //
    addConverter(VersionNumber.class, String.class, new TypeConverter.Converter<VersionNumber, String>() {
        public String convert(VersionNumber source) {
            return source.toString();
        }
    });

    //
    // From MLText
    //
    addConverter(MLText.class, String.class, new TypeConverter.Converter<MLText, String>() {
        public String convert(MLText source) {
            return source.getDefaultValue();
        }
    });

    addConverter(MLText.class, BasicDBObject.class, new TypeConverter.Converter<MLText, BasicDBObject>() {
        public BasicDBObject convert(MLText source) {
            BasicDBObject dbObject = new BasicDBObject("t", "MLTEXT");
            for (Map.Entry<Locale, String> entry : source.entrySet()) {
                dbObject.put(entry.getKey().toLanguageTag(), entry.getValue());
            }
            return dbObject;
        }
    });

    //        addConverter(ContentDataWithId.class, BasicDBObject.class, new TypeConverter.Converter<ContentDataWithId, BasicDBObject>()
    //        {
    //            public BasicDBObject convert(ContentDataWithId source)
    //            {
    //                BasicDBObject dbObject = new BasicDBObject("t", "CONTENT_DATA_ID");
    //
    //                String contentUrl = source.getContentUrl();
    //                Long id = source.getId();
    //                String languageTag = source.getLocale().toLanguageTag();
    //                String encoding = source.getEncoding();
    //                long size = source.getSize();
    //                String mimeType = source.getMimetype();
    //
    //                dbObject.put("u", contentUrl);
    //                dbObject.put("m", mimeType);
    //                dbObject.put("s", size);
    //                dbObject.put("e", encoding);
    //                dbObject.put("l", languageTag);
    //                dbObject.put("id", id);
    //                return dbObject;
    //            }
    //        });

    addConverter(ContentData.class, BasicDBObject.class,
            new TypeConverter.Converter<ContentData, BasicDBObject>() {
                public BasicDBObject convert(ContentData source) {
                    BasicDBObject dbObject = new BasicDBObject("t", "CONTENT");

                    String contentUrl = source.getContentUrl();
                    String languageTag = source.getLocale().toLanguageTag();
                    String encoding = source.getEncoding();
                    long size = source.getSize();
                    String mimeType = source.getMimetype();

                    dbObject.put("u", contentUrl);
                    dbObject.put("m", mimeType);
                    dbObject.put("s", size);
                    dbObject.put("e", encoding);
                    dbObject.put("l", languageTag);
                    return dbObject;
                }
            });

    //
    // From enum
    //
    addConverter(Enum.class, String.class, new TypeConverter.Converter<Enum, String>() {
        public String convert(Enum source) {
            return source.toString();
        }
    });

    // From Period
    addConverter(Period.class, String.class, new TypeConverter.Converter<Period, String>() {
        public String convert(Period source) {
            return source.toString();
        }
    });

    // From Class
    addConverter(Class.class, String.class, new TypeConverter.Converter<Class, String>() {
        public String convert(Class source) {
            return source.getName();
        }
    });

    //
    // Number to Subtypes and Date
    //
    addConverter(Number.class, Boolean.class, new TypeConverter.Converter<Number, Boolean>() {
        public Boolean convert(Number source) {
            return new Boolean(source.longValue() > 0);
        }
    });
    addConverter(Number.class, Byte.class, new TypeConverter.Converter<Number, Byte>() {
        public Byte convert(Number source) {
            return Byte.valueOf(source.byteValue());
        }
    });
    addConverter(Number.class, Short.class, new TypeConverter.Converter<Number, Short>() {
        public Short convert(Number source) {
            return Short.valueOf(source.shortValue());
        }
    });
    addConverter(Number.class, Integer.class, new TypeConverter.Converter<Number, Integer>() {
        public Integer convert(Number source) {
            return Integer.valueOf(source.intValue());
        }
    });
    addConverter(Number.class, Long.class, new TypeConverter.Converter<Number, Long>() {
        public Long convert(Number source) {
            return Long.valueOf(source.longValue());
        }
    });
    addConverter(Number.class, Float.class, new TypeConverter.Converter<Number, Float>() {
        public Float convert(Number source) {
            return Float.valueOf(source.floatValue());
        }
    });
    addConverter(Number.class, Double.class, new TypeConverter.Converter<Number, Double>() {
        public Double convert(Number source) {
            return Double.valueOf(source.doubleValue());
        }
    });
    addConverter(Number.class, Date.class, new TypeConverter.Converter<Number, Date>() {
        public Date convert(Number source) {
            return new Date(source.longValue());
        }
    });
    addConverter(Number.class, String.class, new TypeConverter.Converter<Number, String>() {
        public String convert(Number source) {
            return source.toString();
        }
    });
    addConverter(Number.class, BigInteger.class, new TypeConverter.Converter<Number, BigInteger>() {
        public BigInteger convert(Number source) {
            if (source instanceof BigDecimal) {
                return ((BigDecimal) source).toBigInteger();
            } else {
                return BigInteger.valueOf(source.longValue());
            }
        }
    });
    addConverter(Number.class, BigDecimal.class, new TypeConverter.Converter<Number, BigDecimal>() {
        public BigDecimal convert(Number source) {
            if (source instanceof BigInteger) {
                return new BigDecimal((BigInteger) source);
            } else if (source instanceof Double) {
                return BigDecimal.valueOf((Double) source);
            } else if (source instanceof Float) {
                Float val = (Float) source;
                if (val.isInfinite()) {
                    // What else can we do here?  this is 3.4 E 38 so is fairly big
                    return new BigDecimal(Float.MAX_VALUE);
                }
                return BigDecimal.valueOf((Float) source);
            } else {
                return BigDecimal.valueOf(source.longValue());
            }
        }
    });
    addDynamicTwoStageConverter(Number.class, String.class, InputStream.class);

    //
    // Date, Timestamp ->
    //
    addConverter(Timestamp.class, Date.class, new TypeConverter.Converter<Timestamp, Date>() {
        public Date convert(Timestamp source) {
            return new Date(source.getTime());
        }
    });
    addConverter(Date.class, Number.class, new TypeConverter.Converter<Date, Number>() {
        public Number convert(Date source) {
            return Long.valueOf(source.getTime());
        }
    });
    addConverter(Date.class, String.class, new TypeConverter.Converter<Date, String>() {
        public String convert(Date source) {
            try {
                return ISO8601DateFormat.format(source);
            } catch (PlatformRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            }
        }
    });
    addConverter(Date.class, Calendar.class, new TypeConverter.Converter<Date, Calendar>() {
        public Calendar convert(Date source) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(source);
            return calendar;
        }
    });

    addConverter(Date.class, GregorianCalendar.class, new TypeConverter.Converter<Date, GregorianCalendar>() {
        public GregorianCalendar convert(Date source) {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(source);
            return calendar;
        }
    });
    addDynamicTwoStageConverter(Date.class, String.class, InputStream.class);

    //
    // Boolean ->
    //
    final Long LONG_FALSE = new Long(0L);
    final Long LONG_TRUE = new Long(1L);
    addConverter(Boolean.class, Long.class, new TypeConverter.Converter<Boolean, Long>() {
        public Long convert(Boolean source) {
            return source.booleanValue() ? LONG_TRUE : LONG_FALSE;
        }
    });
    addConverter(Boolean.class, String.class, new TypeConverter.Converter<Boolean, String>() {
        public String convert(Boolean source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Boolean.class, String.class, InputStream.class);

    //
    // Character ->
    //
    addConverter(Character.class, String.class, new TypeConverter.Converter<Character, String>() {
        public String convert(Character source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Character.class, String.class, InputStream.class);

    //
    // Duration ->
    //
    addConverter(Duration.class, String.class, new TypeConverter.Converter<Duration, String>() {
        public String convert(Duration source) {
            return source.toString();
        }

    });
    addDynamicTwoStageConverter(Duration.class, String.class, InputStream.class);

    //
    // Byte
    //
    addConverter(Byte.class, String.class, new TypeConverter.Converter<Byte, String>() {
        public String convert(Byte source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Byte.class, String.class, InputStream.class);

    //
    // Short
    //
    addConverter(Short.class, String.class, new TypeConverter.Converter<Short, String>() {
        public String convert(Short source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Short.class, String.class, InputStream.class);

    //
    // Integer
    //
    addConverter(Integer.class, String.class, new TypeConverter.Converter<Integer, String>() {
        public String convert(Integer source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Integer.class, String.class, InputStream.class);

    //
    // Long
    //
    addConverter(Long.class, String.class, new TypeConverter.Converter<Long, String>() {
        public String convert(Long source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Long.class, String.class, InputStream.class);

    //
    // Float
    //
    addConverter(Float.class, String.class, new TypeConverter.Converter<Float, String>() {
        public String convert(Float source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Float.class, String.class, InputStream.class);

    //
    // Double
    //
    addConverter(Double.class, String.class, new TypeConverter.Converter<Double, String>() {
        public String convert(Double source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Double.class, String.class, InputStream.class);

    //
    // BigInteger
    //
    addConverter(BigInteger.class, String.class, new TypeConverter.Converter<BigInteger, String>() {
        public String convert(BigInteger source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(BigInteger.class, String.class, InputStream.class);

    //
    // Calendar
    //
    addConverter(Calendar.class, Date.class, new TypeConverter.Converter<Calendar, Date>() {
        public Date convert(Calendar source) {
            return source.getTime();
        }
    });
    addConverter(Calendar.class, String.class, new TypeConverter.Converter<Calendar, String>() {
        public String convert(Calendar source) {
            try {
                return ISO8601DateFormat.format(source.getTime());
            } catch (PlatformRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            }
        }
    });

    //
    // BigDecimal
    //
    addConverter(BigDecimal.class, BasicDBObject.class,
            new TypeConverter.Converter<BigDecimal, BasicDBObject>() {
                public BasicDBObject convert(BigDecimal source) {
                    String number = source.toPlainString();
                    int precision = source.precision();
                    BasicDBObject dbObject = new BasicDBObject();
                    dbObject.put("t", "FIXED_POINT");
                    dbObject.put("n", number);
                    dbObject.put("p", precision);
                    return dbObject;
                }
            });
    addConverter(BigDecimal.class, String.class, new TypeConverter.Converter<BigDecimal, String>() {
        public String convert(BigDecimal source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(BigDecimal.class, String.class, InputStream.class);

    //
    // QName
    //
    addConverter(QName.class, String.class, new TypeConverter.Converter<QName, String>() {
        public String convert(QName source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(QName.class, String.class, InputStream.class);

    //
    // EntityRef (NodeRef, ChildAssociationRef, NodeAssociationRef)
    //
    addConverter(EntityRef.class, String.class, new TypeConverter.Converter<EntityRef, String>() {
        public String convert(EntityRef source) {
            return source.toString();
        }
    });
    addConverter(EntityRef.class, BasicDBObject.class, new TypeConverter.Converter<EntityRef, BasicDBObject>() {
        public BasicDBObject convert(EntityRef source) {
            BasicDBObject ret = null;

            if (source instanceof NodeRef) {
                NodeRef nodeRef = (NodeRef) source;

                BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("t", "NODEREF");
                builder.add("p", nodeRef.getStoreRef().getProtocol());
                builder.add("s", nodeRef.getStoreRef().getIdentifier());
                builder.add("id", nodeRef.getId());
                ret = (BasicDBObject) builder.get();
            } else if (source instanceof StoreRef) {
                StoreRef storeRef = (StoreRef) source;

                BasicDBObjectBuilder builder = BasicDBObjectBuilder.start("t", "STOREREF");
                builder.add("p", storeRef.getProtocol());
                builder.add("s", storeRef.getIdentifier());
                ret = (BasicDBObject) builder.get();
            } else {
                throw new IllegalArgumentException();
            }

            return ret;
        }
    });
    addDynamicTwoStageConverter(EntityRef.class, String.class, InputStream.class);

    //
    // ContentData
    //
    addConverter(ContentData.class, String.class, new TypeConverter.Converter<ContentData, String>() {
        public String convert(ContentData source) {
            return source.getInfoUrl();
        }
    });
    addDynamicTwoStageConverter(ContentData.class, String.class, InputStream.class);

    //
    // Path
    //
    addConverter(Path.class, String.class, new TypeConverter.Converter<Path, String>() {
        public String convert(Path source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Path.class, String.class, InputStream.class);

    //
    // Content Reader
    //
    addConverter(ContentReader.class, InputStream.class,
            new TypeConverter.Converter<ContentReader, InputStream>() {
                public InputStream convert(ContentReader source) {
                    return source.getContentInputStream();
                }
            });
    addConverter(ContentReader.class, String.class, new TypeConverter.Converter<ContentReader, String>() {
        public String convert(ContentReader source) {
            // Getting the string from the ContentReader binary is meaningless
            return source.toString();
        }
    });

    //
    // Content Writer
    //
    addConverter(ContentWriter.class, String.class, new TypeConverter.Converter<ContentWriter, String>() {
        public String convert(ContentWriter source) {
            return source.toString();
        }
    });

    addConverter(Collection.class, BasicDBList.class, new TypeConverter.Converter<Collection, BasicDBList>() {
        public BasicDBList convert(Collection source) {
            BasicDBList ret = new BasicDBList();
            for (Object o : source) {
                ret.add(o);
            }
            return ret;
        }
    });

    addConverter(BasicDBList.class, Collection.class, new TypeConverter.Converter<BasicDBList, Collection>() {
        @SuppressWarnings("unchecked")
        public Collection convert(BasicDBList source) {
            Collection ret = new LinkedList();
            for (Object o : source) {
                ret.add(o);
            }
            return ret;
        }
    });

    //
    // Input Stream
    //
    addConverter(InputStream.class, String.class, new TypeConverter.Converter<InputStream, String>() {
        public String convert(InputStream source) {
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[8192];
                int read;
                while ((read = source.read(buffer)) > 0) {
                    out.write(buffer, 0, read);
                }
                byte[] data = out.toByteArray();
                return new String(data, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new TypeConversionException("Cannot convert input stream to String.", e);
            } catch (IOException e) {
                throw new TypeConversionException("Conversion from stream to string failed", e);
            } finally {
                if (source != null) {
                    try {
                        source.close();
                    } catch (IOException e) {
                        //NOOP
                    }
                }
            }
        }
    });
    addDynamicTwoStageConverter(InputStream.class, String.class, Date.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Double.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Long.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Boolean.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, QName.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Path.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, NodeRef.class);

}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Euler-Mascheroni constant.//from   w  w  w  .ja  va 2 s. c om
 *
 * @param mc The required precision of the result.
 * @return 0.577...
 */
static public BigDecimal gamma(MathContext mc) {
    /* look it up if possible */
    if (mc.getPrecision() < GAMMA.precision()) {
        return GAMMA.round(mc);
    } else {
        double eps = prec2err(0.577, mc.getPrecision());
        /* Euler-Stieltjes as shown in Dilcher, Aequat Math 48 (1) (1994) 55-85
        14
         */
        MathContext mcloc = new MathContext(2 + mc.getPrecision());
        BigDecimal resul = BigDecimal.ONE;
        resul = resul.add(log(2, mcloc));
        resul = resul.subtract(log(3, mcloc));
        /* how many terms: zeta-1 falls as 1/2^(2n+1), so the
         * terms drop faster than 1/2^(4n+2). Set 1/2^(4kmax+2) < eps.
         * Leading term zeta(3)/(4^1*3) is 0.017. Leading zeta(3) is 1.2. Log(2) is 0.7
         */
        int kmax = (int) ((Math.log(eps / 0.7) - 2.) / 4.);
        mcloc = new MathContext(1 + err2prec(1.2, eps / kmax));
        for (int n = 1;; n++) {
            /* zeta is close to 1. Division of zeta-1 through
             * 4^n*(2n+1) means divion through roughly 2^(2n+1)
             */
            BigDecimal c = zeta(2 * n + 1, mcloc).subtract(BigDecimal.ONE);
            BigInteger fourn = new BigInteger("" + (2 * n + 1));
            fourn = fourn.shiftLeft(2 * n);
            c = divideRound(c, fourn);
            resul = resul.subtract(c);
            if (c.doubleValue() < 0.1 * eps) {
                break;
            }
        }
        return resul.round(mc);
    }
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The integer root.//from  w  ww. j a  va2 s  .c o  m
 *
 * @param n the positive argument.
 * @param x the non-negative argument.
 * @return The n-th root of the BigDecimal rounded to the precision implied by x, x^(1/n).
 */
static public BigDecimal root(final int n, final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        throw new ArithmeticException("negative argument " + x.toString() + " of root");
    }
    if (n <= 0) {
        throw new ArithmeticException("negative power " + n + " of root");
    }
    if (n == 1) {
        return x;
    }
    /* start the computation from a double precision estimate */
    BigDecimal s = new BigDecimal(Math.pow(x.doubleValue(), 1.0 / n));
    /* this creates nth with nominal precision of 1 digit
     */
    final BigDecimal nth = new BigDecimal(n);
    /* Specify an internal accuracy within the loop which is
     * slightly larger than what is demanded by eps below.
     */
    final BigDecimal xhighpr = scalePrec(x, 2);
    MathContext mc = new MathContext(2 + x.precision());
    /* Relative accuracy of the result is eps.
     */
    final double eps = x.ulp().doubleValue() / (2 * n * x.doubleValue());
    for (;;) {
        /* s = s -(s/n-x/n/s^(n-1)) = s-(s-x/s^(n-1))/n; test correction s/n-x/s for being
         * smaller than the precision requested. The relative correction is (1-x/s^n)/n,
         */
        BigDecimal c = xhighpr.divide(s.pow(n - 1), mc);
        c = s.subtract(c);
        MathContext locmc = new MathContext(c.precision());
        c = c.divide(nth, locmc);
        s = s.subtract(c);
        if (Math.abs(c.doubleValue() / s.doubleValue()) < eps) {
            break;
        }
    }
    return s.round(new MathContext(err2prec(eps)));
}

From source file:sbml.test.UploadUnzipTest.java

private final boolean tolerable(double expectedVal, double actualVal, double absTol, double relTol) {
    if (Double.isInfinite(expectedVal))
        return (expectedVal == actualVal);
    else if (Double.isNaN(expectedVal))
        return Double.isNaN(actualVal);
    else if (Double.isNaN(actualVal) || Double.isInfinite(actualVal))
        return false;
    else {/*from w  w w. j av  a2  s  . co m*/
        BigDecimal expected = new BigDecimal(expectedVal);
        MathContext mc = new MathContext(expected.precision());
        BigDecimal adjusted = new BigDecimal(actualVal).round(mc);
        BigDecimal actualDiff = expected.subtract(adjusted).abs();
        BigDecimal rtol = new BigDecimal(relTol);
        BigDecimal atol = new BigDecimal(absTol);
        BigDecimal allowableDiff = atol.add(rtol.multiply(expected.abs()));

        return (actualDiff.compareTo(allowableDiff) <= 0);
    }
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The hypotenuse./*from ww  w.j ava  2 s.c  om*/
 *
 * @param x the first argument.
 * @param y the second argument.
 * @return the square root of the sum of the squares of the two arguments, sqrt(x^2+y^2).
 */
static public BigDecimal hypot(final BigDecimal x, final BigDecimal y) {
    /* compute x^2+y^2
     */
    BigDecimal z = x.pow(2).add(y.pow(2));
    /* truncate to the precision set by x and y. Absolute error = 2*x*xerr+2*y*yerr,
     * where the two errors are 1/2 of the ulps. Two intermediate protectio digits.
     */
    BigDecimal zerr = x.abs().multiply(x.ulp()).add(y.abs().multiply(y.ulp()));
    MathContext mc = new MathContext(2 + err2prec(z, zerr));
    /* Pull square root */
    z = sqrt(z.round(mc));
    /* Final rounding. Absolute error in the square root is (y*yerr+x*xerr)/z, where zerr holds 2*(x*xerr+y*yerr).
     */
    mc = new MathContext(err2prec(z.doubleValue(), 0.5 * zerr.doubleValue() / z.doubleValue()));
    return z.round(mc);
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The hypotenuse.//from   w  w  w  . j av a2 s . c o m
 *
 * @param n the first argument.
 * @param x the second argument.
 * @return the square root of the sum of the squares of the two arguments, sqrt(n^2+x^2).
 */
static public BigDecimal hypot(final int n, final BigDecimal x) {
    /* compute n^2+x^2 in infinite precision
     */
    BigDecimal z = (new BigDecimal(n)).pow(2).add(x.pow(2));
    /* Truncate to the precision set by x. Absolute error = in z (square of the result) is |2*x*xerr|,
     * where the error is 1/2 of the ulp. Two intermediate protection digits.
     * zerr is a signed value, but used only in conjunction with err2prec(), so this feature does not harm.
     */
    double zerr = x.doubleValue() * x.ulp().doubleValue();
    MathContext mc = new MathContext(2 + err2prec(z.doubleValue(), zerr));
    /* Pull square root */
    z = sqrt(z.round(mc));
    /* Final rounding. Absolute error in the square root is x*xerr/z, where zerr holds 2*x*xerr.
     */
    mc = new MathContext(err2prec(z.doubleValue(), 0.5 * zerr / z.doubleValue()));
    return z.round(mc);
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The exponential function.//  w ww .ja v  a  2  s.  co  m
 *
 * @param x the argument.
 * @return exp(x).
 * The precision of the result is implicitly defined by the precision in the argument.
 * 16
 * In particular this means that "Invalid Operation" errors are thrown if catastrophic
 * cancellation of digits causes the result to have no valid digits left.
 */
static public BigDecimal exp(BigDecimal x) {
    /* To calculate the value if x is negative, use exp(-x) = 1/exp(x)
     */
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        final BigDecimal invx = exp(x.negate());
        /* Relative error in inverse of invx is the same as the relative errror in invx.
         * This is used to define the precision of the result.
         */
        MathContext mc = new MathContext(invx.precision());
        return BigDecimal.ONE.divide(invx, mc);
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        /* recover the valid number of digits from x.ulp(), if x hits the
         * zero. The x.precision() is 1 then, and does not provide this information.
         */
        return scalePrec(BigDecimal.ONE, -(int) (Math.log10(x.ulp().doubleValue())));
    } else {
        /* Push the number in the Taylor expansion down to a small
         * value where TAYLOR_NTERM terms will do. If x<1, the n-th term is of the order
         * x^n/n!, and equal to both the absolute and relative error of the result
         * since the result is close to 1. The x.ulp() sets the relative and absolute error
         * of the result, as estimated from the first Taylor term.
         * We want x^TAYLOR_NTERM/TAYLOR_NTERM! < x.ulp, which is guaranteed if
         * x^TAYLOR_NTERM < TAYLOR_NTERM*(TAYLOR_NTERM-1)*...*x.ulp.
         */
        final double xDbl = x.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue();
        if (Math.pow(xDbl, TAYLOR_NTERM) < TAYLOR_NTERM * (TAYLOR_NTERM - 1.0) * (TAYLOR_NTERM - 2.0)
                * xUlpDbl) {
            /* Add TAYLOR_NTERM terms of the Taylor expansion (Eulers sum formula)
             */
            BigDecimal resul = BigDecimal.ONE;
            /* x^i */
            BigDecimal xpowi = BigDecimal.ONE;
            /* i factorial */
            BigInteger ifac = BigInteger.ONE;
            /* TAYLOR_NTERM terms to be added means we move x.ulp() to the right
             * for each power of 10 in TAYLOR_NTERM, so the addition wont add noise beyond
             * whats already in x.
             */
            MathContext mcTay = new MathContext(err2prec(1., xUlpDbl / TAYLOR_NTERM));
            for (int i = 1; i <= TAYLOR_NTERM; i++) {
                ifac = ifac.multiply(new BigInteger("" + i));
                xpowi = xpowi.multiply(x);
                final BigDecimal c = xpowi.divide(new BigDecimal(ifac), mcTay);
                resul = resul.add(c);
                if (Math.abs(xpowi.doubleValue()) < i && Math.abs(c.doubleValue()) < 0.5 * xUlpDbl) {
                    break;
                }
            }
            /* exp(x+deltax) = exp(x)(1+deltax) if deltax is <<1. So the relative error
             * in the result equals the absolute error in the argument.
             */
            MathContext mc = new MathContext(err2prec(xUlpDbl / 2.));
            return resul.round(mc);
        } else {
            /* Compute exp(x) = (exp(0.1*x))^10. Division by 10 does not lead
             * to loss of accuracy.
             */
            int exSc = (int) (1.0 - Math.log10(TAYLOR_NTERM * (TAYLOR_NTERM - 1.0) * (TAYLOR_NTERM - 2.0)
                    * xUlpDbl / Math.pow(xDbl, TAYLOR_NTERM)) / (TAYLOR_NTERM - 1.0));
            BigDecimal xby10 = x.scaleByPowerOfTen(-exSc);
            BigDecimal expxby10 = exp(xby10);
            /* Final powering by 10 means that the relative error of the result
             * is 10 times the relative error of the base (First order binomial expansion).
             * This looses one digit.
             */
            MathContext mc = new MathContext(expxby10.precision() - exSc);
            /* Rescaling the powers of 10 is done in chunks of a maximum of 8 to avoid an invalid operation
            17
             * response by the BigDecimal.pow library or integer overflow.
             */
            while (exSc > 0) {
                int exsub = Math.min(8, exSc);
                exSc -= exsub;
                MathContext mctmp = new MathContext(expxby10.precision() - exsub + 2);
                int pex = 1;
                while (exsub-- > 0) {
                    pex *= 10;
                }
                expxby10 = expxby10.pow(pex, mctmp);
            }
            return expxby10.round(mc);
        }
    }
}

From source file:com.servoy.j2db.util.Utils.java

public static BigDecimal roundNumber(Object number, int precision, boolean throwOnFail) {
    MathContext mathContext = new MathContext(precision);
    if (number instanceof BigDecimal) {
        return ((BigDecimal) number).round(mathContext);
    }/*ww  w.jav a  2 s.  c  o m*/
    return new BigDecimal(Utils.getAsDouble(number, throwOnFail)).round(mathContext);
}

From source file:nl.strohalm.cyclos.entities.settings.LocalSettings.java

public MathContext getMathContext() {
    if (mathContext == null) {
        mathContext = new MathContext(BIG_DECIMAL_DIVISION_PRECISION);
    }/*from  ww w  .j av a2  s  . com*/
    return mathContext;
}

From source file:org.hyperic.hq.measurement.server.session.AvailabilityManagerImpl.java

private HighLowMetricValue round(HighLowMetricValue val) {
    final BigDecimal b = new BigDecimal(val.getValue(), new MathContext(10));
    val.setValue(b.doubleValue());
    return val;
}