abb087a732d3df3b53b1f6936d0e57baee22d923
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / SimpleFieldSetBuilder.java
1 /*
2  * Sone - SimpleFieldSetBuilder.java - Copyright © 2011–2016 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 public class SimpleFieldSetBuilder {
28
29         /** The simple field set that is being constructed. */
30         private final SimpleFieldSet simpleFieldSet;
31
32         /**
33          * Creates a new simple field set builder using a new, empty simple field
34          * set.
35          */
36         public SimpleFieldSetBuilder() {
37                 this(new SimpleFieldSet(true));
38         }
39
40         /**
41          * Creates a new simple field set builder that will return the given simple
42          * field set on {@link #get()}.
43          *
44          * @param simpleFieldSet
45          *            The simple field set to build
46          */
47         public SimpleFieldSetBuilder(SimpleFieldSet simpleFieldSet) {
48                 this.simpleFieldSet = checkNotNull(simpleFieldSet, "simpleFieldSet must not be null");
49         }
50
51         /**
52          * Returns the constructed simple field set.
53          *
54          * @return The construct simple field set
55          */
56         public SimpleFieldSet get() {
57                 return simpleFieldSet;
58         }
59
60         /**
61          * Copies the given simple field set into the simple field set being built
62          * in this builder, overwriting all previously existing values.
63          *
64          * @param simpleFieldSet
65          *            The simple field set to copy
66          * @return This simple field set builder
67          */
68         public SimpleFieldSetBuilder put(SimpleFieldSet simpleFieldSet) {
69                 this.simpleFieldSet.putAllOverwrite(simpleFieldSet);
70                 return this;
71         }
72
73         /**
74          * Stores the given value under the given key, overwriting any previous
75          * value.
76          *
77          * @param key
78          *            The key of the value
79          * @param value
80          *            The value to store
81          * @return This simple field set builder
82          */
83         public SimpleFieldSetBuilder put(String key, String value) {
84                 simpleFieldSet.putOverwrite(key, value);
85                 return this;
86         }
87
88         /**
89          * Stores the given value under the given key, overwriting any previous
90          * value.
91          *
92          * @param key
93          *            The key of the value
94          * @param value
95          *            The value to store
96          * @return This simple field set builder
97          */
98         public SimpleFieldSetBuilder put(String key, int value) {
99                 simpleFieldSet.put(key, value);
100                 return this;
101         }
102
103         /**
104          * Stores the given value under the given key, overwriting any previous
105          * value.
106          *
107          * @param key
108          *            The key of the value
109          * @param value
110          *            The value to store
111          * @return This simple field set builder
112          */
113         public SimpleFieldSetBuilder put(String key, long value) {
114                 simpleFieldSet.put(key, value);
115                 return this;
116         }
117
118 }