ratpack.parse.Parse Maven / Gradle / Ivy
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ratpack.parse;
import com.google.common.reflect.TypeToken;
import ratpack.util.Types;
import java.util.Optional;
/**
* The specification of a particular parse.
*
* Construct instances via the {@link #of} methods.
*
* @param the type of object to construct from the request body
* @param the type of object that provides options/configuration for the parsing
* @see ratpack.handling.Context#parse(Parse)
* @see Parser
* @see ParserSupport
*/
public class Parse {
private final TypeToken type;
private final Optional opts;
private Parse(TypeToken type, Optional opts) {
this.type = type;
this.opts = opts;
}
/**
* The type of object to construct from the request body.
*
* @return the type of object to construct from the request body
*/
public TypeToken getType() {
return type;
}
/**
* The type of object that provides options/configuration for the parsing.
*
* For any parse request, no options may be specified.
* Parser implementations should throw an exception if they require an options object when none is supplied.
*
* @return the type of object that provides options/configuration for the parsing
*/
public Optional getOpts() {
return opts;
}
/**
* Creates a parse object.
*
* @param type the type of object to construct from the request body
* @param opts the options object
* @param the type of object to construct from the request body
* @param the type of object that provides options/configuration for the parsing
* @return a parse instance from the given arguments
*/
public static Parse of(TypeToken type, O opts) {
return new Parse<>(type, Optional.of(opts));
}
/**
* Creates a parse object, with no options.
*
* @param type the type of object to construct from the request body
* @param the type of object to construct from the request body
* @return a parse instance to the given type
*/
public static Parse of(TypeToken type) {
return new Parse<>(type, Optional.empty());
}
/**
* Creates a parse object.
*
* @param type the type of object to construct from the request body
* @param opts the options object
* @param the type of object to construct from the request body
* @param the type of object that provides options/configuration for the parsing
* @return a parse instance from the given arguments
*/
public static Parse of(Class type, O opts) {
return of(Types.token(type), opts);
}
/**
* Creates a parse object, with no options.
*
* @param type the type of object to construct from the request body
* @param the type of object to construct from the request body
* @return a parse instance to the given type
*/
public static Parse of(Class type) {
return of(Types.token(type));
}
}