Skip to content

Commit c421a0e

Browse files
committed
Refactor Value API (hide internal implementation)
1 parent 089e23b commit c421a0e

File tree

15 files changed

+184
-150
lines changed

15 files changed

+184
-150
lines changed

jooby/src/main/java/io/jooby/Formdata.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@
1717

1818
import io.jooby.internal.HashValue;
1919

20+
import java.util.Collection;
21+
2022
/**
2123
* Formdata class for direct MVC parameter provisioning.
2224
*
2325
* HTTP request must be encoded as {@link MediaType#FORM_URLENCODED}.
2426
*/
25-
public class Formdata extends HashValue {
27+
public interface Formdata extends Value {
28+
29+
Formdata put(String path, Value value);
30+
31+
Formdata put(String path, String value);
32+
33+
Formdata put(String path, Collection<String> values);
34+
35+
static Formdata create() {
36+
return new HashValue(null).setObjectType("formdata");
37+
}
2638
}

jooby/src/main/java/io/jooby/Multipart.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
*/
1616
package io.jooby;
1717

18+
import io.jooby.internal.HashValue;
19+
1820
/**
1921
* Multipart class for direct MVC parameter provisioning.
2022
*
2123
* HTTP request must be encoded as {@link MediaType#MULTIPART_FORMDATA}.
2224
*/
23-
public class Multipart extends Formdata {
25+
public interface Multipart extends Formdata {
26+
27+
static Multipart create() {
28+
return new HashValue(null).setObjectType("multipart");
29+
}
2430
}

jooby/src/main/java/io/jooby/QueryString.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,39 @@
1515
*/
1616
package io.jooby;
1717

18-
import io.jooby.internal.HashValue;
18+
import io.jooby.internal.UrlParser;
1919

2020
import javax.annotation.Nonnull;
21+
import javax.annotation.Nullable;
2122

2223
/**
2324
* Query string class for direct MVC parameter provisioning.
2425
*
2526
* @author edgar
2627
* @since 2.0.0
2728
*/
28-
public class QueryString extends HashValue {
29-
/** Empty query string. */
30-
public static final QueryString EMPTY = new QueryString("");
31-
32-
private final String queryString;
29+
public interface QueryString extends Value {
3330

3431
/**
35-
* Creates a query string object.
32+
* Query string with the leading <code>?</code> or empty string.
3633
*
37-
* @param queryString Raw string (no decoded it).
34+
* @return Query string with the leading <code>?</code> or empty string.
3835
*/
39-
public QueryString(@Nonnull String queryString) {
40-
this.queryString = queryString;
41-
}
36+
@Nonnull String queryString();
4237

4338
/**
44-
* Query string with the leading <code>?</code> or empty string.
39+
* Query string hash value.
4540
*
46-
* @return Query string with the leading <code>?</code> or empty string.
41+
* <pre>{@code q=foo&sort=name}</pre>
42+
*
43+
* Produces:
44+
*
45+
* <pre>{@code {q: foo, sort: name}}</pre>
46+
*
47+
* @param queryString Query string.
48+
* @return A query string.
4749
*/
48-
public String queryString() {
49-
return queryString;
50+
static @Nonnull QueryString create(@Nullable String queryString) {
51+
return UrlParser.queryString(queryString);
5052
}
5153
}

jooby/src/main/java/io/jooby/Value.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -706,20 +706,4 @@ default int size() {
706706
static @Nonnull Value hash(@Nonnull Map<String, Collection<String>> values) {
707707
return new HashValue(null).put(values);
708708
}
709-
710-
/**
711-
* Query string hash value.
712-
*
713-
* <pre>{@code /path?q=foo&sort=name}</pre>
714-
*
715-
* Produces:
716-
*
717-
* <pre>{@code {q: foo, sort: name}}</pre>
718-
*
719-
* @param queryString Query string.
720-
* @return A query string.
721-
*/
722-
static QueryString queryString(@Nonnull String queryString) {
723-
return UrlParser.queryString(queryString);
724-
}
725709
}

jooby/src/main/java/io/jooby/internal/FileBody.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
2929
import java.util.Collections;
30-
import java.util.Iterator;
3130
import java.util.List;
3231
import java.util.Map;
3332

jooby/src/main/java/io/jooby/internal/HashValue.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import io.jooby.Err;
1919
import io.jooby.FileUpload;
20+
import io.jooby.Formdata;
21+
import io.jooby.Multipart;
2022
import io.jooby.Value;
2123

2224
import javax.annotation.Nonnull;
@@ -30,13 +32,15 @@
3032
import java.util.TreeMap;
3133
import java.util.function.BiConsumer;
3234

33-
public class HashValue implements Value {
35+
public class HashValue implements Value, Multipart {
3436
private static final Map<String, Value> EMPTY = Collections.emptyMap();
3537

3638
private Map<String, Value> hash = EMPTY;
3739

3840
private final String name;
3941

42+
private String objectType;
43+
4044
public HashValue(String name) {
4145
this.name = name;
4246
}
@@ -49,11 +53,11 @@ protected HashValue() {
4953
return name;
5054
}
5155

52-
public HashValue put(String path, String value) {
56+
public Formdata put(String path, String value) {
5357
return put(path, Collections.singletonList(value));
5458
}
5559

56-
public HashValue put(String path, FileUpload upload) {
60+
public HashValue put(String path, Value upload) {
5761
put(path, (name, scope) -> {
5862
Value existing = scope.get(name);
5963
if (existing == null) {
@@ -180,7 +184,12 @@ public int size() {
180184

181185
@Override public String value() {
182186
String name = name();
183-
throw new Err.TypeMismatch(name == null ? getClass().getSimpleName() : name, String.class);
187+
throw new Err.TypeMismatch(name == null ? objectType : name, String.class);
188+
}
189+
190+
public HashValue setObjectType(String type) {
191+
this.objectType = type;
192+
return this;
184193
}
185194

186195
@Override public Iterator<Value> iterator() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
16+
package io.jooby.internal;
17+
18+
import io.jooby.QueryString;
19+
20+
import javax.annotation.Nonnull;
21+
22+
public class QueryStringValue extends HashValue implements QueryString {
23+
private String queryString;
24+
25+
public QueryStringValue(String queryString) {
26+
setObjectType("queryString");
27+
this.queryString = queryString;
28+
}
29+
30+
@Nonnull @Override public String queryString() {
31+
return queryString;
32+
}
33+
}

jooby/src/main/java/io/jooby/internal/UrlParser.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@
2626
import java.nio.charset.StandardCharsets;
2727

2828
public final class UrlParser {
29+
private static final QueryStringValue NO_QUERY_STRING = new QueryStringValue("");
2930
private static final char SPACE = 0x20;
3031

31-
public static QueryString queryString(String path) {
32-
if (path == null) {
33-
return QueryString.EMPTY;
32+
public static QueryString queryString(String queryString) {
33+
if (queryString == null || queryString.length() == 0) {
34+
return NO_QUERY_STRING;
3435
}
35-
int start = path.indexOf('?');
36-
if (start < 0) {
37-
return QueryString.EMPTY;
38-
}
39-
int len = path.length();
40-
QueryString result = new QueryString(path.substring(start));
41-
parse(path, start + 1, len, result);
36+
QueryStringValue result = new QueryStringValue("?" + queryString);
37+
parse(queryString, 0, queryString.length(), result);
4238
return result;
4339
}
4440

jooby/src/main/kotlin/io/jooby/Kooby.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,23 @@ open class Kooby constructor() : Jooby() {
206206
return super.route(method, pattern, handler)
207207
}
208208

209-
fun serverOptions(options: ServerOptions.() -> Unit): Kooby {
209+
fun serverOptions(configurer: ServerOptions.() -> Unit): Kooby {
210210
val options = ServerOptions()
211-
options(options)
211+
configurer(options)
212212
setServerOptions(options)
213213
return this
214214
}
215215

216-
fun routerOptions(options: RouterOptions.() -> Unit): Kooby {
216+
fun routerOptions(configurer: RouterOptions.() -> Unit): Kooby {
217217
val options = RouterOptions()
218-
options(options)
218+
configurer(options)
219219
this.routerOptions = options
220220
return this
221221
}
222222

223-
fun environmentOptions(options: EnvironmentOptions.() -> Unit): Environment {
223+
fun environmentOptions(configurer: EnvironmentOptions.() -> Unit): Environment {
224224
val options = EnvironmentOptions()
225-
options(options)
225+
configurer(options)
226226
val env = Environment.loadEnvironment(options)
227227
environment = env
228228
return env

jooby/src/test/java/io/jooby/MockContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public class MockContext implements Context {
3333

3434
private Map<String, String> pathMap;
3535

36-
private QueryString query = UrlParser.queryString("?");
36+
private QueryString query = QueryString.create("");
3737

3838
private String queryString;
3939

4040
private Map<String, Collection<String>> headers = new HashMap<>();
4141

42-
private Formdata formdata = new Formdata();
42+
private Formdata formdata = Formdata.create();
4343

44-
private Multipart multipart = new Multipart();
44+
private Multipart multipart = Multipart.create();
4545

4646
private Body body;
4747

0 commit comments

Comments
 (0)