/*
- * Sone - ImageLinkFilter.java - Copyright © 2011–2013 David Roden
+ * Sone - ImageLinkFilter.java - Copyright © 2011–2015 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package net.pterodactylus.sone.template;
+import static java.lang.Integer.MAX_VALUE;
+import static java.lang.String.valueOf;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.util.number.Numbers;
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.template.TemplateContextFactory;
import net.pterodactylus.util.template.TemplateParser;
+import com.google.common.base.Function;
import com.google.common.base.Optional;
/**
if (image == null) {
return null;
}
- String imageClass = String.valueOf(parameters.get("class"));
- int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
- int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
- String mode = String.valueOf(parameters.get("mode"));
- String title = String.valueOf(parameters.get("title"));
+ String imageClass = Optional.fromNullable(parameters.get("class")).transform(getStringValue()).orNull();
+ int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE);
+ int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE);
+ String mode = valueOf(parameters.get("mode"));
+ String title = Optional.fromNullable(parameters.get("title")).transform(getStringValue()).orNull();
TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
linkTemplateContext.set("class", imageClass);
return stringWriter.toString();
}
+ private Function<Object, String> getStringValue() {
+ return new Function<Object, String>() {
+ @Override
+ public String apply(Object input) {
+ return (input != null) ? input.toString() : null;
+ }
+ };
+ }
+
}