Skip to content

Commit b932556

Browse files
committed
Added generics type
1 parent 0b28c44 commit b932556

File tree

7 files changed

+56
-49
lines changed

7 files changed

+56
-49
lines changed

AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="ua.at.tsvetkov.data_processor"
3-
android:versionCode="2"
4-
android:versionName="1.0.01" >
3+
android:versionCode="3"
4+
android:versionName="1.0.02" >
55

66
<uses-sdk
77
android:minSdkVersion="8"

src/ua/at/tsvetkov/data_processor/DataProcessor.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
//import ua.at.tsvetkov.data_processor.interfaces.InputStreamDataInterface;
2727
//import ua.at.tsvetkov.data_processor.interfaces.StringDataInterface;
28+
import ua.at.tsvetkov.data_processor.ProcessingCentre.Callback;
2829
import ua.at.tsvetkov.data_processor.requests.Request;
2930
import ua.at.tsvetkov.netchecker.Net;
3031
import ua.at.tsvetkov.netchecker.NetChecker;
@@ -100,50 +101,43 @@ public synchronized boolean isSiteAccessible(Activity activity) {
100101

101102
/**
102103
* Execute the request, process the results in instance of <b>clazz</b> and return result object
104+
* @param <T>
105+
* @param <T>
103106
*
104107
* @param request
105108
* @param clazz
106109
* @return
107110
*/
108-
public synchronized Object execute(Request request, Class<?> clazz) {
111+
public synchronized <T> T execute(Request request, Class<T> clazz) {
109112
checkConfiguration();
110-
return new ProcessingCentre(request, clazz).execute();
113+
return new ProcessingCentre<T>(request, clazz).execute();
111114
}
112115

113116
/**
114117
* Execute async request, process the results in instance of <b>clazz</b> and return result in callback
118+
* @param <T>
119+
* @param <T>
115120
*
116121
* @param request
117122
* @param clazz
118123
* @param handler
119124
*/
120-
public synchronized void executeAsync(Request request, Class<?> clazz) {
125+
public synchronized <T> void executeAsync(Request request, Class<T> clazz) {
121126
checkConfiguration();
122-
new ProcessingCentre(request, clazz, null).executeAsync();
127+
new ProcessingCentre<T>(request, clazz, null).executeAsync();
123128
}
124129

125130
/**
126131
* Execute async request, process the results in instance of <b>clazz</b> and return result in callback
132+
* @param <T>
127133
*
128134
* @param request
129135
* @param clazz
130136
* @param handler
131137
*/
132-
public synchronized void executeAsync(Request request, Class<?> clazz, Callback callback) {
138+
public synchronized <T> void executeAsync(Request request, Class<T> clazz, Callback<T> callback) {
133139
checkConfiguration();
134-
new ProcessingCentre(request, clazz, callback).executeAsync();
135-
}
136-
137-
public static interface Callback {
138-
139-
/**
140-
* Runs on the UI thread.
141-
*
142-
* @param obj
143-
* @param what
144-
*/
145-
public abstract void onFinish(Object obj, int what);
146-
140+
new ProcessingCentre<T>(request, clazz, callback).executeAsync();
147141
}
148142

149143
}

src/ua/at/tsvetkov/data_processor/ProcessingCentre.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import org.apache.http.HttpStatus;
3535

36-
import ua.at.tsvetkov.data_processor.DataProcessor.Callback;
3736
import ua.at.tsvetkov.data_processor.interfaces.InputStreamDataInterface;
3837
import ua.at.tsvetkov.data_processor.interfaces.StringDataInterface;
3938
import ua.at.tsvetkov.data_processor.processors.InputStreamProcessor;
@@ -45,7 +44,7 @@
4544
import android.os.Handler;
4645
import android.os.Looper;
4746

48-
public class ProcessingCentre {
47+
public class ProcessingCentre<T> {
4948

5049
private static final String INVALID_CLASS_PARAMETER = "Invalid class parameter. A class for data processing must implement InputStreamDataInterface or StringDataInterface either extend AbstractProcessor";
5150
private static final String INVALID_PARAMETER = "Invalid parameter. Request or Class can't be eq null";
@@ -60,19 +59,19 @@ public class ProcessingCentre {
6059
public static final int ERROR = -1;
6160

6261
private Request request;
63-
private AbstractProcessor processor;
62+
private AbstractProcessor<T> processor;
6463
private InputStream inputStream;
6564
private String cacheFileName;
66-
private Class<?> clazz;
67-
private Callback callback;
65+
private Class<T> clazz;
66+
private Callback<T> callback;
6867
private Thread thread;
6968
private final Handler handler;
7069

7170
/**
7271
* @param request
7372
* @param clazz
7473
*/
75-
public ProcessingCentre(Request request, Class<?> clazz) {
74+
public ProcessingCentre(Request request, Class<T> clazz) {
7675
if (request == null || clazz == null) {
7776
throw new InvalidParameterException(INVALID_PARAMETER);
7877
}
@@ -92,7 +91,7 @@ public ProcessingCentre(Request request, Class<?> clazz) {
9291
* @param clazz
9392
* @param callback
9493
*/
95-
public ProcessingCentre(Request request, Class<?> clazz, Callback callback) {
94+
public ProcessingCentre(Request request, Class<T> clazz, Callback<T> callback) {
9695
if (request == null || clazz == null) {
9796
throw new InvalidParameterException(INVALID_PARAMETER);
9897
}
@@ -107,7 +106,7 @@ public ProcessingCentre(Request request, Class<?> clazz, Callback callback) {
107106
thread = Thread.currentThread();
108107
}
109108

110-
public Object execute() {
109+
public T execute() {
111110
if (!isCorrectClass()) {
112111
throw new InvalidParameterException(INVALID_CLASS_PARAMETER);
113112
}
@@ -123,15 +122,14 @@ public Object execute() {
123122
}
124123
if (processor != null) {
125124
processor.parse(inputStream);
126-
sendMessage(request.getStatusCode(), processor.getResult());
125+
sendMessage(request.getStatusCode(), (T) processor.getResult());
127126
} else {
128-
sendMessage(request.getStatusCode(), inputStream);
127+
sendMessage(request.getStatusCode(), null);
128+
// sendMessage(request.getStatusCode(), inputStream); WTF???
129129
}
130130
} catch (Exception e) {
131-
if (DataProcessor.getInstance().getConfiguration().isLogEnabled()) {
132-
Log.e(e);
133-
}
134-
sendMessage(ERROR, e);
131+
Log.e(e);
132+
sendMessage(ERROR, null);
135133
} finally {
136134
try {
137135
if (inputStream != null)
@@ -146,15 +144,15 @@ public Object execute() {
146144
Log.v("Processing time = " + time + " ms. [ " + request + " ]");
147145
}
148146
if (processor != null)
149-
return processor.getResult();
147+
return (T) processor.getResult();
150148
else
151149
return null;
152150
}
153151

154152
/**
155153
*
156154
*/
157-
@SuppressWarnings("rawtypes")
155+
@SuppressWarnings({ "rawtypes", "unchecked" })
158156
private void createProcessor() {
159157
try {
160158
if (AbstractProcessor.class.isAssignableFrom(clazz)) {
@@ -187,16 +185,16 @@ private boolean isCorrectClass() {
187185
return false;
188186
}
189187

190-
private void sendMessage(final int statusCode, final Object obj) {
188+
private void sendMessage(final int statusCode, final T object) {
191189
if (callback != null) {
192190
if (thread == Thread.currentThread()) {
193-
callback.onFinish(obj, statusCode);
191+
callback.onFinish(object, statusCode);
194192
} else {
195193
handler.post(new Runnable() {
196194

197195
@Override
198196
public void run() {
199-
callback.onFinish(obj, statusCode);
197+
callback.onFinish(object, statusCode);
200198
}
201199
});
202200
}
@@ -236,4 +234,16 @@ private void saveToFile() throws IOException {
236234
inputStream = new FileInputStream(cacheFileName);
237235
}
238236

237+
public static interface Callback<T> {
238+
239+
/**
240+
* Runs on the UI thread.
241+
*
242+
* @param obj
243+
* @param what
244+
*/
245+
public abstract void onFinish(T obj, int what);
246+
247+
}
248+
239249
}

src/ua/at/tsvetkov/data_processor/processors/InputStreamProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import ua.at.tsvetkov.data_processor.interfaces.InputStreamDataInterface;
2929
import ua.at.tsvetkov.data_processor.processors.abstractclasses.AbstractProcessor;
3030

31-
public class InputStreamProcessor extends AbstractProcessor {
31+
public class InputStreamProcessor<T> extends AbstractProcessor<T> {
3232

3333
private InputStreamDataInterface object;
3434

@@ -41,9 +41,10 @@ public void parse(InputStream inputStream) throws Exception {
4141
object.fillFromInputStream(inputStream);
4242
}
4343

44+
@SuppressWarnings("unchecked")
4445
@Override
45-
public Object getResult() {
46-
return object;
46+
public T getResult() {
47+
return (T) object;
4748
}
4849

4950
}

src/ua/at/tsvetkov/data_processor/processors/StringProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import ua.at.tsvetkov.data_processor.interfaces.StringDataInterface;
2727
import ua.at.tsvetkov.data_processor.processors.abstractclasses.StringAbstractProcessor;
2828

29-
public class StringProcessor extends StringAbstractProcessor {
29+
public class StringProcessor<T> extends StringAbstractProcessor<T> {
3030

3131
private StringDataInterface object;
3232

@@ -39,9 +39,10 @@ public void process(String src) throws Exception {
3939
object.fillFromString(src);
4040
}
4141

42+
@SuppressWarnings("unchecked")
4243
@Override
43-
public Object getResult() {
44-
return object;
44+
public T getResult() {
45+
return (T) object;
4546
}
4647

4748
}

src/ua/at/tsvetkov/data_processor/processors/abstractclasses/AbstractProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import java.io.InputStream;
2727

28-
public abstract class AbstractProcessor {
28+
public abstract class AbstractProcessor<T> {
2929

3030
/**
3131
* Parse the input stream with implemented parser.
@@ -40,6 +40,6 @@ public abstract class AbstractProcessor {
4040
*
4141
* @return resulting object
4242
*/
43-
public abstract Object getResult();
43+
public abstract T getResult();
4444

4545
}

src/ua/at/tsvetkov/data_processor/processors/abstractclasses/StringAbstractProcessor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
* Base String parser. Can be implemented for parse JSON, CSV and etc. data.
3434
*
3535
* @author lordtao
36+
* @param <T>
3637
*/
37-
public abstract class StringAbstractProcessor extends AbstractProcessor {
38+
public abstract class StringAbstractProcessor<T> extends AbstractProcessor<T> {
3839

3940
@Override
4041
public void parse(InputStream inputStream) throws Exception {
@@ -52,7 +53,7 @@ public void parse(InputStream inputStream) throws Exception {
5253
}
5354

5455
@Override
55-
public abstract Object getResult();
56+
public abstract T getResult();
5657

5758
/**
5859
* Processing the received string.

0 commit comments

Comments
 (0)