TileRectConverter.java
2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* 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);
}
}