All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feilong.lib.xstream.converters.extended.DurationConverter Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.0.8
Show newest version
/*
 * Copyright (C) 2007, 2008, 2011, 2018 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 21.09.2007 by Joerg Schaible
 */
package com.feilong.lib.xstream.converters.extended;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;

import com.feilong.lib.xstream.converters.basic.AbstractSingleValueConverter;

/**
 * A Converter for the XML Schema datatype duration and the Java type
 * {@link Duration}. The implementation uses a {@link DatatypeFactory} to create Duration
 * objects. If no factory is provided and the instantiation of the internal factory fails with a
 * {@link DatatypeConfigurationException}, the converter will not claim the responsibility for
 * Duration objects.
 * 
 * @author John Kristian
 * @author Jörg Schaible
 * @since 1.3
 */
public class DurationConverter extends AbstractSingleValueConverter{

    private final DatatypeFactory factory;

    public DurationConverter(){
        this(new Object(){

            DatatypeFactory getFactory(){
                try{
                    return DatatypeFactory.newInstance();
                }catch (final DatatypeConfigurationException e){
                    return null;
                }
            }
        }.getFactory());
    }

    public DurationConverter(DatatypeFactory factory){
        this.factory = factory;
    }

    @Override
    public boolean canConvert(Class type){
        return factory != null && type != null && Duration.class.isAssignableFrom(type);
    }

    @Override
    public Object fromString(String s){
        return factory.newDuration(s);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy