Add method to set a long value.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / SimpleFieldSetBuilder.java
1 /*
2  * Sone - SimpleFieldSetBuilder.java - Copyright © 2011 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.freenet;
19
20 import net.pterodactylus.util.validation.Validation;
21 import freenet.support.SimpleFieldSet;
22
23 /**
24  * Helper class to construct {@link SimpleFieldSet} objects in a single call.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class SimpleFieldSetBuilder {
29
30         /** The simple field set that is being constructed. */
31         private final SimpleFieldSet simpleFieldSet;
32
33         /**
34          * Creates a new simple field set builder using a new, empty simple field
35          * set.
36          */
37         public SimpleFieldSetBuilder() {
38                 this(new SimpleFieldSet(true));
39         }
40
41         /**
42          * Creates a new simple field set builder that will return the given simple
43          * field set on {@link #get()}.
44          *
45          * @param simpleFieldSet
46          *            The simple field set to build
47          */
48         public SimpleFieldSetBuilder(SimpleFieldSet simpleFieldSet) {
49                 Validation.begin().isNotNull("Simple Field Set", simpleFieldSet).check();
50                 this.simpleFieldSet = simpleFieldSet;
51         }
52
53         /**
54          * Returns the constructed simple field set.
55          *
56          * @return The construct simple field set
57          */
58         public SimpleFieldSet get() {
59                 return simpleFieldSet;
60         }
61
62         /**
63          * Stores the given value under the given key, overwriting any previous
64          * value.
65          *
66          * @param key
67          *            The key of the value
68          * @param value
69          *            The value to store
70          * @return This simple field set builder
71          */
72         public SimpleFieldSetBuilder put(String key, String value) {
73                 simpleFieldSet.putOverwrite(key, value);
74                 return this;
75         }
76
77         /**
78          * Stores the given value under the given key, overwriting any previous
79          * value.
80          *
81          * @param key
82          *            The key of the value
83          * @param value
84          *            The value to store
85          * @return This simple field set builder
86          */
87         public SimpleFieldSetBuilder put(String key, int value) {
88                 simpleFieldSet.put(key, value);
89                 return this;
90         }
91
92         /**
93          * Stores the given value under the given key, overwriting any previous
94          * value.
95          *
96          * @param key
97          *            The key of the value
98          * @param value
99          *            The value to store
100          * @return This simple field set builder
101          */
102         public SimpleFieldSetBuilder put(String key, long value) {
103                 simpleFieldSet.put(key, value);
104                 return this;
105         }
106
107 }