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

com.feilong.lib.xstream.converters.time.WeekFieldsConverter 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) 2017 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 22. February 2017 by Joerg Schaible
 */
package com.feilong.lib.xstream.converters.time;

import java.time.DayOfWeek;
import java.time.temporal.WeekFields;

import com.feilong.lib.xstream.converters.Converter;
import com.feilong.lib.xstream.converters.MarshallingContext;
import com.feilong.lib.xstream.converters.UnmarshallingContext;
import com.feilong.lib.xstream.converters.reflection.AbstractReflectionConverter.UnknownFieldException;
import com.feilong.lib.xstream.io.ExtendedHierarchicalStreamWriterHelper;
import com.feilong.lib.xstream.io.HierarchicalStreamReader;
import com.feilong.lib.xstream.io.HierarchicalStreamWriter;
import com.feilong.lib.xstream.mapper.Mapper;

/**
 * Converts a {@link WeekFields} instance, using two nested elements: minimalDays and minSmallest.
 *
 * @author Jörg Schaible
 * @since 1.4.10
 */
public class WeekFieldsConverter implements Converter{

    private final Mapper mapper;

    /**
     * Constructs a WeekFieldsConverter instance.
     * 
     * @param mapper
     *            the Mapper instance
     */
    public WeekFieldsConverter(final Mapper mapper){
        this.mapper = mapper;

    }

    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") final Class type){
        return type == WeekFields.class;
    }

    @Override
    public void marshal(final Object source,final HierarchicalStreamWriter writer,final MarshallingContext context){
        final WeekFields weekFields = (WeekFields) source;
        ExtendedHierarchicalStreamWriterHelper.startNode(writer, mapper.serializedMember(WeekFields.class, "minimalDays"), int.class);
        writer.setValue(String.valueOf(weekFields.getMinimalDaysInFirstWeek()));
        writer.endNode();
        ExtendedHierarchicalStreamWriterHelper
                        .startNode(writer, mapper.serializedMember(WeekFields.class, "firstDayOfWeek"), DayOfWeek.class);
        context.convertAnother(weekFields.getFirstDayOfWeek());
        writer.endNode();
    }

    @Override
    public Object unmarshal(final HierarchicalStreamReader reader,final UnmarshallingContext context){
        final boolean oldFormat = "custom".equals(reader.getAttribute(mapper.aliasForSystemAttribute("serialization")));
        if (oldFormat){
            reader.moveDown();
            reader.moveDown();
        }

        int minimalDays = 0;
        DayOfWeek firstDayOfWeek = null;
        while (reader.hasMoreChildren()){
            reader.moveDown();
            final String name = oldFormat ? reader.getNodeName() : mapper.realMember(WeekFields.class, reader.getNodeName());
            if ("minimalDays".equals(name)){
                minimalDays = Integer.parseInt(reader.getValue());
            }else if ("firstDayOfWeek".equals(name)){
                firstDayOfWeek = (DayOfWeek) context.convertAnother(null, DayOfWeek.class);
            }else{
                throw new UnknownFieldException(WeekFields.class.getName(), name);
            }
            reader.moveUp();
        }
        if (oldFormat){
            reader.moveUp();
            reader.moveUp();
        }
        return WeekFields.of(firstDayOfWeek, minimalDays);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy