a256edb16574607db96acad2c28bc37184d136e5
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / SimpleFieldSetBuilder.java
1 /*
2  * Sone - SimpleFieldSetBuilder.java - Copyright © 2011–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.freenet;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import freenet.support.SimpleFieldSet;
23
24 /**
25  * Helper class to construct {@link SimpleFieldSet} objects in a single call.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class SimpleFieldSetBuilder {
30
31         /** The simple field set that is being constructed. */
32         private final SimpleFieldSet simpleFieldSet;
33
34         /**
35          * Creates a new simple field set builder using a new, empty simple field
36          * set.
37          */
38         public SimpleFieldSetBuilder() {
39                 this(new SimpleFieldSet(true));
40         }
41
42         /**
43          * Creates a new simple field set builder that will return the given simple
44          * field set on {@link #get()}.
45          *
46          * @param simpleFieldSet
47          *            The simple field set to build
48          */
49         public SimpleFieldSetBuilder(SimpleFieldSet simpleFieldSet) {
50                 this.simpleFieldSet = checkNotNull(simpleFieldSet, "simpleFieldSet must not be null");
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          * Copies the given simple field set into the simple field set being built
64          * in this builder, overwriting all previously existing values.
65          *
66          * @param simpleFieldSet
67          *            The simple field set to copy
68          * @return This simple field set builder
69          */
70         public SimpleFieldSetBuilder put(SimpleFieldSet simpleFieldSet) {
71                 this.simpleFieldSet.putAllOverwrite(simpleFieldSet);
72                 return this;
73         }
74
75         /**
76          * Stores the given value under the given key, overwriting any previous
77          * value.
78          *
79          * @param key
80          *            The key of the value
81          * @param value
82          *            The value to store
83          * @return This simple field set builder
84          */
85         public SimpleFieldSetBuilder put(String key, String value) {
86                 simpleFieldSet.putOverwrite(key, value);
87                 return this;
88         }
89
90         /**
91          * Stores the given value under the given key, overwriting any previous
92          * value.
93          *
94          * @param key
95          *            The key of the value
96          * @param value
97          *            The value to store
98          * @return This simple field set builder
99          */
100         public SimpleFieldSetBuilder put(String key, int value) {
101                 simpleFieldSet.put(key, value);
102                 return this;
103         }
104
105         /**
106          * Stores the given value under the given key, overwriting any previous
107          * value.
108          *
109          * @param key
110          *            The key of the value
111          * @param value
112          *            The value to store
113          * @return This simple field set builder
114          */
115         public SimpleFieldSetBuilder put(String key, long value) {
116                 simpleFieldSet.put(key, value);
117                 return this;
118         }
119
120 }