-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
Parameter.java
153 lines (138 loc) · 5.83 KB
/
Parameter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2010-2021 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb;
import static com.restfb.util.StringUtils.isBlank;
import static com.restfb.util.StringUtils.trimToEmpty;
import static java.lang.String.format;
import com.restfb.exception.FacebookJsonMappingException;
import java.util.Optional;
/**
* Representation of a Facebook API request parameter.
*
* @author <a href="http://restfb.com">Mark Allen</a>
*/
public final class Parameter {
/**
* Parameter name.
*/
public final String name;
/**
* Parameter value.
*/
public final String value;
/**
* Creates a new parameter with the given {@code name} and {@code value}.
*
* @param name
* The parameter name.
* @param value
* The parameter value.
* @param jsonMapper
* Mapper for converting the parameter value to JSON.
* @throws IllegalArgumentException
* If {@code name} is {@code null} or a blank string or either {@code value} or {@code jsonMapper} is
* {@code null}.
*/
private Parameter(String name, Object value, JsonMapper jsonMapper) {
if (isBlank(name) || value == null) {
throw new IllegalArgumentException(Parameter.class + " instances must have a non-blank name and non-null value."
+ " Got instead name:" + name + ", value:" + value);
}
this.value = Optional.ofNullable(jsonMapper).orElseThrow(() -> new IllegalArgumentException("Provided " + JsonMapper.class + " must not be null.")).toJson(value, true);
this.name = trimToEmpty(name);
}
/**
* Factory method which provides an instance with the given {@code name} and {@code value}.
* <p>
* The {@code value} parameter is often a {@link String} or primitive type like {@link Integer}, but you may pass in a
* {@link java.util.List}, {@link java.util.Map}, or your own <tt>@Facebook</tt>-annotated Javabean and it will be
* converted to JSON automatically. See the "attachment" section of
* <a href="http://wiki.developers.facebook.com/index.php/Stream.publish">the stream.publish API documentation</a> for
* an example of where this is useful.
*
* @param name
* The parameter name.
* @param value
* The parameter value.
* @return A {@code Parameter} instance with the given {@code name} and {@code value}.
* @throws IllegalArgumentException
* If {@code name} or {@code value} is {@code null} or a blank string.
* @throws FacebookJsonMappingException
* If an error occurs when converting {@code value} to JSON.
*/
public static Parameter with(String name, Object value) {
return Parameter.with(name, value, new DefaultJsonMapper());
}
/**
* Factory method which provides an instance with the given {@code name} and {@code value}, using the provided
* {@code jsonMapper} to turn {@code value} into a JSON string.
* <p>
* The {@code value} parameter is often a {@link String} or primitive type like {@link Integer}, but you may pass in a
* {@link java.util.List}, {@link java.util.Map}, or your own <tt>@Facebook</tt>-annotated Javabean and it will be
* converted to JSON automatically. See the "attachment" section of
* <a href="http://wiki.developers.facebook.com/index.php/Stream.publish">the stream.publish API documentation</a> for
* an example of where this is useful.
*
* @param name
* The parameter name.
* @param value
* The parameter value.
* @param jsonMapper
* The jsonMapper
* @return A {@code Parameter} instance with the given {@code name} and {@code value}.
* @throws IllegalArgumentException
* If {@code name} or {@code value} is {@code null} or a blank string.
* @throws FacebookJsonMappingException
* If an error occurs when converting {@code value} to JSON.
*/
public static Parameter with(String name, Object value, JsonMapper jsonMapper) {
return new Parameter(name, value, jsonMapper);
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!getClass().equals(obj.getClass())) {
return false;
}
Parameter other = (Parameter) obj;
if (this.name != other.name && !this.name.equals(other.name)) {
return false;
}
return !(this.value != other.value && !this.value.equals(other.value));
}
@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + this.name.hashCode();
hash = 41 * hash + this.value.hashCode();
return hash;
}
@Override
public String toString() {
return format("Parameter[%s=%s]", name, value);
}
}