org.opentestsystem.delivery.testreg.domain.EnumBasedXStreamConverter.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.domain.EnumBasedXStreamConverter.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 *
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/

package org.opentestsystem.delivery.testreg.domain;

import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.apache.commons.lang.StringUtils.replaceEach;
import static org.apache.commons.lang.WordUtils.capitalize;
import static org.apache.commons.lang.WordUtils.capitalizeFully;

import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;

@SuppressWarnings("rawtypes")
public class EnumBasedXStreamConverter<T extends Enum> extends AbstractSingleValueConverter {

    final Class<T> enumType;

    final CaseStyle caseStyleSource;

    final CaseStyle caseStyleTarget;

    final boolean isFullyCapitalize;

    public EnumBasedXStreamConverter(final Class<T> enumType) {
        this(enumType, (String) null);
    }

    public EnumBasedXStreamConverter(final Class<T> enumType, final String caseStyle) {
        this(enumType, caseStyle, caseStyle);
    }

    public EnumBasedXStreamConverter(final Class<T> enumType, final String caseStyle1, final String caseStyle2) {
        this(enumType, CaseStyle.fromName(caseStyle1), CaseStyle.fromName(caseStyle2));
    }

    private EnumBasedXStreamConverter(final Class<T> enumType, final CaseStyle caseStyleSource,
            final CaseStyle caseStyleTarget) {
        this(enumType, caseStyleSource, caseStyleTarget, true);
    }

    private EnumBasedXStreamConverter(final Class<T> enumType, final CaseStyle caseStyleSource,
            final CaseStyle caseStyleTarget, final boolean isFullyCapitalize) {
        this.enumType = enumType;
        this.caseStyleSource = caseStyleSource;
        this.caseStyleTarget = caseStyleTarget;
        this.isFullyCapitalize = isFullyCapitalize;
    }

    @Override
    public boolean canConvert(final Class type) {
        return type.equals(String.class);
    }

    @Override
    @SuppressWarnings("unchecked")
    public String toString(final Object obj) {
        String name = (caseStyleSource == null) ? obj.toString()
                : (isFullyCapitalize ? caseStyleSource.changeCaseFully(obj.toString())
                        : caseStyleSource.changeCase(obj.toString()));

        if (caseStyleTarget.equals(CaseStyle.TITLECASE_HANDLE_NULL_EMPTY) && isNullOrEmpty(name)) {
            return "";
        }
        try {
            Enum enumVal = Enum.valueOf(enumType, name);
            return caseStyleTarget.changeCase(enumVal.toString());
        } catch (IllegalArgumentException iae) {
            return name;
        }
    }

    private boolean isNullOrEmpty(final String name) {
        return (name == null || isEmpty(name));
    }

    @Override
    public Object fromString(final String str) {
        throw new UnsupportedOperationException("Support not yet added to convert a String to Date: " + str);
    }

    public enum CaseStyle {
        UPPERCASE {
            @Override
            public String changeCase(final String name) {
                return name.toUpperCase();
            }

            @Override
            public String changeCaseFully(final String name) {
                return changeCase(name);
            }
        },
        TITLECASE {
            @Override
            public String changeCase(final String name) {
                return replaceEach(capitalize(name, new char[] { '-', '_', ' ' }), new String[] { "-", "_", " " },
                        new String[] { "", "", "" });
            }

            @Override
            public String changeCaseFully(final String name) {
                return replaceEach(capitalizeFully(name, new char[] { '-', '_', ' ' }),
                        new String[] { "-", "_", " " }, new String[] { "", "", "" });
            }
        },
        LOWERCASE {
            @Override
            public String changeCase(final String name) {
                return name.toLowerCase();
            }

            @Override
            public String changeCaseFully(final String name) {
                return changeCase(name);
            }
        },
        TITLECASE_HANDLE_NULL_EMPTY {
            @Override
            public String changeCase(final String name) {
                return TITLECASE.changeCase(name);
            }

            @Override
            public String changeCaseFully(final String name) {
                return TITLECASE.changeCaseFully(name);
            }
        };

        private static CaseStyle fromName(final String name) {
            for (CaseStyle caseStyle : values()) {
                if (caseStyle.name().equalsIgnoreCase(name)) {
                    return caseStyle;
                }
            }
            return null;
        }

        public abstract String changeCase(String name);

        public abstract String changeCaseFully(String name);
    }

}