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

com.nikolavp.approval.converters.Converters Maven / Gradle / Ivy

Go to download

Approval is a Java library which will make you look at your testing from a whole different angle

There is a newer version: 0.3
Show newest version
package com.nikolavp.approval.converters;

/*
 * #%L
 * approval
 * %%
 * Copyright (C) 2014 Nikolavp
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import javax.annotation.Nonnull;
import java.lang.reflect.Array;

/**
 * Converters for primitive types. Most of these just call toString on the passed object and then get the raw representation of the string result.
 * .
 * User: nikolavp
 * Date: 28/02/14
 * Time: 17:25
 */
public final class Converters {
    /*
     Hack the type system to accept the types and prevent raw type warnings in the client side.
     */
    /**
     * A converter for the primitive or wrapper byte types.
     */
    public static final Converter BYTE = of();
    /**
     * A converter for the String object.
     */
    public static final Converter STRING = of();
    /**
     * A converter for the primitive or wrapper int object.
     */
    public static final Converter INTEGER = of();
    /**
     * A converter for the primitive or wrapper short object.
     */
    public static final Converter SHORT = of();
    /**
     * A converter for the primitive or wrapper long object.
     */
    public static final Converter LONG = of();
    /**
     * A converter for the primitive or wrapper float object.
     */
    public static final Converter FLOAT = of();
    /**
     * A converter for the primitive or wrapper boolean object.
     */
    public static final Converter BOOLEAN = of();
    /**
     * A converter for the primitive or wrapper double object.
     */
    public static final Converter DOUBLE = of();
    /**
     * A converter for the primitive or wrapper char object.
     */
    public static final Converter CHAR = of();

    /**
     * A converter for the primitive int arrays.
     */
    public static final Converter INTEGER_ARRAY = ofArray();
    /**
     * A converter for the primitive byte arrays.
     */
    public static final Converter BYTE_ARRAY = ofArray();
    /**
     * A converter for the primitive long arrays.
     */
    public static final Converter LONG_ARRAY = ofArray();
    /**
     * A converter for the primitive short arrays.
     */
    public static final Converter SHORT_ARRAY = ofArray();
    /**
     * A converter for the primitive double arrays.
     */
    public static final Converter DOUBLE_ARRAY = ofArray();
    /**
     * A converter for the primitive boolean arrays.
     */
    public static final Converter BOOLEAN_ARRAY = ofArray();
    /**
     * A converter for the primitive char arrays.
     */
    public static final Converter CHAR_ARRAY = ofArray();
    /**
     * A converter for the primitive float arrays.
     */
    public static final Converter FLOAT_ARRAY = ofArray();

    /**
     * A converter for an array of strings.
     */
    public static final Converter STRING_ARRAY = ofArray();

    private Converters() {
    }

    static  Converter ofArray() {
        return new AbstractStringConverter() {
            @Nonnull
            @Override
            protected String getStringForm(Object value) {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < Array.getLength(value); i++) {
                    builder.append("[")
                            .append(i)
                            .append("] = ")
                            .append(String.valueOf(Array.get(value, i)))
                            .append("\n");
                }
                return builder.toString();
            }
        };
    }

    static  Converter of() {
        return new AbstractStringConverter() {
            @Nonnull
            @Override
            protected String getStringForm(T value) {
                return value.toString();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy