Skip to content

Commit dd1cdd8

Browse files
committed
added type inference to Model methods to avoid casting
1 parent f3bb09f commit dd1cdd8

File tree

8 files changed

+160
-194
lines changed

8 files changed

+160
-194
lines changed

src/main/java/org/codebrewery/ApiInterface.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
/**
66
* Created by ejeserl on 9/19/15.
77
*/
8-
public interface ApiInterface<T> {
8+
public interface ApiInterface {
99

10-
Model save(Model model) throws JavaOrmenException;
10+
<M extends Model> M save(M model) throws JavaOrmenException;
1111

12-
Model update(Model model) throws JavaOrmenException;
12+
<M extends Model> M update(M model) throws JavaOrmenException;
1313

14-
Model insert(Model model) throws JavaOrmenException;
14+
<M extends Model> M insert(M model) throws JavaOrmenException;
1515

16-
void delete(Model model) throws JavaOrmenException;
16+
<M extends Model> void delete(M model) throws JavaOrmenException;
1717

18-
Model fetch(Model model) throws JavaOrmenException;
18+
<M extends Model> M fetch(M model) throws JavaOrmenException;
1919

20-
List<Model> getAll(Class<? extends Model> aClass) throws JavaOrmenException;
20+
<M extends Model> List<M> getAll(Class<M> aClass) throws JavaOrmenException;
2121

2222
}
Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package org.codebrewery;
22

3-
import com.ning.http.client.AsyncHttpClient;
4-
import com.ning.http.client.Response;
5-
63
import java.io.IOException;
7-
import java.util.ArrayList;
84
import java.util.List;
95
import java.util.concurrent.ExecutionException;
106

7+
import com.ning.http.client.AsyncHttpClient;
8+
import com.ning.http.client.Response;
9+
1110
/**
1211
* Created by ejeserl on 9/19/15.
1312
*
14-
*19520622-1482
13+
* 19520622-1482
1514
*/
1615
public class DefaultApiImplementation implements ApiInterface {
1716

@@ -28,25 +27,26 @@ public class DefaultApiImplementation implements ApiInterface {
2827

2928
String generateCompleteBaseUrl() {
3029

31-
return "http://"+config.getHost() + ":" + config.getPort() + "/" + config.getApiLocation() + "/";
30+
return "http://" + config.getHost() + ":" + config.getPort() + "/" + config.getApiLocation() + "/";
3231
}
3332

3433
String generateInstanceUrl(Model model) {
3534
return generateCollectionUrl(model.getClass()) + "/" + model.getIdentifierValue();
3635
}
36+
3737
@Override
38-
public Model save(Model model) throws JavaOrmenException {
38+
public <T extends Model> T save(T model) throws JavaOrmenException {
3939
try {
4040

41-
String url = generateCollectionUrl(model.getClass());
41+
String url = generateCollectionUrl(model.getClass());
4242
// execute the query
4343
Response response = execute(new AsyncHttpClient().prepareGet(url));
4444
// convert the response to an Model
45-
return JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
45+
return (T) JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
4646

47-
} catch (InterruptedException | ExecutionException | IOException e) {
47+
} catch (InterruptedException | ExecutionException | IOException e) {
4848
// wrap the exception in a javaOrmenException
49-
throw new JavaOrmenException("failed to save model with identifier" + model.getIdentifierValue(),e);
49+
throw new JavaOrmenException("failed to save model with identifier" + model.getIdentifierValue(), e);
5050
}
5151
}
5252

@@ -68,71 +68,71 @@ private String generateCollectionUrl(Class<? extends Model> aClass) {
6868
}
6969

7070
@Override
71-
public Model update(Model model) throws JavaOrmenException {
71+
public <T extends Model> T update(T model) throws JavaOrmenException {
7272
try {
7373

7474
String url = generateInstanceUrl(model);
7575
// execute the query
7676
Response response = execute(new AsyncHttpClient().preparePut(url).setBody(JSONConverter.marshall(model)));
7777
// convert the response to an Model
78-
return JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
78+
return (T) JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
7979

80-
} catch (InterruptedException | ExecutionException | IOException e) {
80+
} catch (InterruptedException | ExecutionException | IOException e) {
8181
// wrap the exception in a javaOrmenException
82-
throw new JavaOrmenException("failed to update model with identifier " + model.getIdentifierValue(),e);
82+
throw new JavaOrmenException("failed to update model with identifier " + model.getIdentifierValue(), e);
8383
}
8484
}
8585

8686
@Override
87-
public Model insert(Model model) throws JavaOrmenException {
87+
public <T extends Model> T insert(T model) throws JavaOrmenException {
8888

89-
try {
89+
try {
9090

91-
String url = generateInstanceUrl(model);
92-
// execute the query
93-
Response response = execute(new AsyncHttpClient().preparePost(url).setBody(JSONConverter.marshall(model)));
94-
// convert the response to an Model
95-
return JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
91+
String url = generateInstanceUrl(model);
92+
// execute the query
93+
Response response = execute(new AsyncHttpClient().preparePost(url).setBody(JSONConverter.marshall(model)));
94+
// convert the response to an Model
95+
return (T) JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
9696

97-
} catch (InterruptedException | ExecutionException | IOException e) {
98-
// wrap the exception in a javaOrmenException
99-
throw new JavaOrmenException("failed to insert model with identifier " + model.getIdentifierValue(),e);
100-
}
97+
} catch (InterruptedException | ExecutionException | IOException e) {
98+
// wrap the exception in a javaOrmenException
99+
throw new JavaOrmenException("failed to insert model with identifier " + model.getIdentifierValue(), e);
100+
}
101101
}
102102

103103
@Override
104-
public void delete(Model model) throws JavaOrmenException {
104+
public <T extends Model> void delete(T model) throws JavaOrmenException {
105105
try {
106106

107107
String url = generateInstanceUrl(model);
108108
// execute the query
109109
Response response = execute(new AsyncHttpClient().prepareDelete(url));
110110
// convert the response to an Model
111111

112-
} catch (InterruptedException | ExecutionException e) {
112+
} catch (InterruptedException | ExecutionException e) {
113113
// wrap the exception in a javaOrmenException
114-
throw new JavaOrmenException("failed to delete model with identifier " + model.getIdentifierValue(),e);
114+
throw new JavaOrmenException("failed to delete model with identifier " + model.getIdentifierValue(), e);
115115
}
116116
}
117117

118118
@Override
119-
public Model fetch(Model model) throws JavaOrmenException {
119+
public <T extends Model> T fetch(T model) throws JavaOrmenException {
120120
try {
121121

122122
String url = generateInstanceUrl(model);
123123
// execute the query
124124
Response response = execute(new AsyncHttpClient().prepareGet(url));
125125
// convert the response to an Model
126-
return JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
126+
return (T) JSONConverter.unMarshall(response.getResponseBody(), model.getClass());
127127

128-
} catch (InterruptedException | ExecutionException | IOException e) {
128+
} catch (InterruptedException | ExecutionException | IOException e) {
129129
// wrap the exception in a javaOrmenException
130-
throw new JavaOrmenException("failed to fetch model with identifier " + model.getIdentifierValue(),e);
130+
throw new JavaOrmenException("failed to fetch model with identifier " + model.getIdentifierValue(), e);
131131
}
132132
}
133133

134134
@Override
135-
public List<Model> getAll(Class aClass) throws JavaOrmenException {
135+
public <M extends Model> List<M> getAll(Class<M> aClass) throws JavaOrmenException {
136136
try {
137137

138138
String url = generateCollectionUrl(aClass);
@@ -141,11 +141,10 @@ public List<Model> getAll(Class aClass) throws JavaOrmenException {
141141
// convert the response to an Model
142142
return JSONConverter.unMarshallList(response.getResponseBody(), aClass);
143143

144-
} catch (InterruptedException | ExecutionException | IOException e) {
144+
} catch (InterruptedException | ExecutionException | IOException e) {
145145
// wrap the exception in a javaOrmenException
146-
throw new JavaOrmenException("failed to fech list of models of class " + aClass.getSimpleName(),e);
146+
throw new JavaOrmenException("failed to fech list of models of class " + aClass.getSimpleName(), e);
147147
}
148148
}
149149

150-
151150
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.codebrewery;
22

3-
import org.codehaus.jackson.map.ObjectMapper;
4-
53
import java.io.IOException;
64
import java.util.List;
75

6+
import org.codehaus.jackson.map.ObjectMapper;
7+
88
/**
99
* Created by jepp3 on 2015-09-06.
1010
*
@@ -18,35 +18,34 @@ public class JSONConverter {
1818
/**
1919
* Converts a AbstractRestModel to a json string. It will look at the anotations on the elements.
2020
*
21-
* @param restModel the model that should be marshalled
21+
* @param restModel
22+
* the model that should be marshalled
2223
* @return
2324
* @throws IOException
2425
*/
2526
public static String marshall(Model restModel) throws IOException {
26-
2727
ObjectMapper mapper = new ObjectMapper();
2828
return mapper.writeValueAsString(restModel);
2929
}
3030

3131
/**
32-
* Converts an json String into a object of the specific class type.
33-
* @param json json string
34-
* @param klazz the AbstractRestModel class to be used
32+
* Converts an json String into a object of the specific class type.
33+
*
34+
* @param json
35+
* json string
36+
* @param klazz
37+
* the AbstractRestModel class to be used
3538
* @return
3639
* @throws IOException
3740
*/
38-
public static Model unMarshall(String json, Class<? extends Model> klazz) throws IOException {
39-
41+
public static <T extends Model> T unMarshall(String json, Class<T> klazz) throws IOException {
4042
ObjectMapper mapper = new ObjectMapper();
4143
return mapper.readValue(json, klazz);
4244
}
4345

44-
public static List<Model> unMarshallList(String json, Class<? extends Model> klazz) throws IOException {
45-
46+
public static <T extends Model> List<T> unMarshallList(String json, Class<T> klazz) throws IOException {
4647
ObjectMapper mapper = new ObjectMapper();
4748
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, klazz));
4849
}
4950

50-
51-
5251
}

0 commit comments

Comments
 (0)