TileRectConverter.java 2.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.util.Converter
 *  com.scene7.is.util.RectInt
 *  com.scene7.is.util.text.ParameterException
 *  com.scene7.is.util.text.ParsingException
 *  com.scene7.is.util.text.access.SimpleListAccess
 *  org.apache.commons.beanutils.ConversionException
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider.parsers;

import com.scene7.is.ps.provider.defs.TileRect;
import com.scene7.is.util.Converter;
import com.scene7.is.util.RectInt;
import com.scene7.is.util.text.ParameterException;
import com.scene7.is.util.text.ParsingException;
import com.scene7.is.util.text.access.SimpleListAccess;
import org.apache.commons.beanutils.ConversionException;
import org.jetbrains.annotations.NotNull;

public class TileRectConverter
extends Converter<String, TileRect> {
    private static final TileRectConverter INSTANCE = new TileRectConverter();

    @NotNull
    public static Converter<String, TileRect> tileRectConverter() {
        return INSTANCE;
    }

    @NotNull
    public TileRect convert(@NotNull String src) throws ConversionException {
        SimpleListAccess params = new SimpleListAccess(src);
        try {
            int x = params.getAsInt("x");
            int y = params.getAsInt("y");
            int w = TileRectConverter.getPositiveInt(params, "width");
            int h = TileRectConverter.getPositiveInt(params, "height");
            double scale = TileRectConverter.getPositiveDouble(params, "scale");
            if (params.contains("extra")) {
                throw new ConversionException("Unexpected value: " + params.getAsString("extra"));
            }
            return TileRect.tileRect(RectInt.rectInt((int)x, (int)y, (int)w, (int)h), scale);
        }
        catch (ParameterException e) {
            throw new ConversionException((Throwable)new ParsingException(4, src, (Throwable)e));
        }
    }

    private static double getPositiveDouble(SimpleListAccess params, String name) throws ParameterException {
        double value = params.getAsDouble(name, 1.0);
        if (value > 0.0) {
            return value;
        }
        throw new ConversionException("illegal " + name + ": " + value + " (must be > 0)");
    }

    private static int getPositiveInt(SimpleListAccess params, String name) throws ParameterException {
        int value = params.getAsInt(name);
        if (value > 0) {
            return value;
        }
        throw new ConversionException("illegal " + name + ": " + value + " (must be > 0)");
    }

    @NotNull
    public String revert(@NotNull TileRect src) throws ConversionException {
        return (Object)src.rect + "," + src.scale;
    }

    private TileRectConverter() {
        super(String.class, TileRect.class);
    }
}