Remove javadoc comments from overriding methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SubstringFilter.java
1 /*
2  * Sone - SubstringFilter.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.util.Map;
21
22 import net.pterodactylus.util.template.Filter;
23 import net.pterodactylus.util.template.TemplateContext;
24
25 /**
26  * {@link Filter} implementation that executes
27  * {@link String#substring(int, int)} on the given data. It has two parameters:
28  * “start” and “length.” “length” is optional and defaults to “the rest of the
29  * string.” “start” starts at {@code 0} and can be negative to denote starting
30  * at the end of the string.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class SubstringFilter implements Filter {
35
36         @Override
37         public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
38                 String startString = String.valueOf(parameters.get("start"));
39                 String lengthString = String.valueOf(parameters.get("length"));
40                 int start = 0;
41                 try {
42                         start = Integer.parseInt(startString);
43                 } catch (NumberFormatException nfe1) {
44                         /* ignore. */
45                 }
46                 String dataString = String.valueOf(data);
47                 int dataLength = dataString.length();
48                 int length = Integer.MAX_VALUE;
49                 try {
50                         length = Integer.parseInt(lengthString);
51                 } catch (NumberFormatException nfe1) {
52                         /* ignore. */
53                 }
54                 if (start < 0) {
55                         return dataString.substring(dataLength + start, Math.min(dataLength, dataLength + start + length));
56                 }
57                 return dataString.substring(start, Math.min(dataLength, start + length));
58         }
59
60 }