Skip to content

Commit 518b0cc

Browse files
committed
Merge pull request ReactiveX#87 from omo/auto-value
Fix ReactiveX#63: Use auto-value for Event classes.
2 parents 17be555 + 80de4b6 commit 518b0cc

16 files changed

+87
-113
lines changed

rxandroid/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// Multi-module modules aren't really supported
22
version = rootProject.version
33

4+
configurations {
5+
// This is a rough approximation of Android plug-in's 'provided' configuration, where
6+
// you can give compile-time only dependencies.
7+
compileOnly
8+
}
9+
10+
dependencies {
11+
compileOnly "com.google.auto.value:auto-value:1.0-rc1"
12+
}
13+
414
test {
515
testLogging {
616
exceptionFormat "full"
@@ -9,3 +19,9 @@ test {
919
}
1020
}
1121

22+
sourceSets {
23+
main {
24+
compileClasspath += configurations.compileOnly
25+
}
26+
}
27+

rxandroid/src/main/java/rx/android/view/OnCheckedChangeEvent.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
package rx.android.view;
1515

1616
import android.widget.CompoundButton;
17+
import com.google.auto.value.AutoValue;
1718

18-
public class OnCheckedChangeEvent {
19-
public final CompoundButton view;
20-
public final boolean value;
19+
@AutoValue
20+
public abstract class OnCheckedChangeEvent {
21+
public abstract CompoundButton view();
22+
public abstract boolean value();
2123

22-
public OnCheckedChangeEvent(final CompoundButton view) {
23-
this(view, view.isChecked());
24+
public static OnCheckedChangeEvent create(final CompoundButton view) {
25+
return create(view, view.isChecked());
2426
}
2527

26-
public OnCheckedChangeEvent(final CompoundButton view, final boolean value) {
27-
this.view = view;
28-
this.value = value;
28+
public static OnCheckedChangeEvent create(final CompoundButton view, final boolean value) {
29+
return new AutoValue_OnCheckedChangeEvent(view, value);
2930
}
3031
}

rxandroid/src/main/java/rx/android/view/OnClickEvent.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515

1616
import android.view.View;
1717

18-
public class OnClickEvent {
19-
public final View view;
18+
import com.google.auto.value.AutoValue;
2019

21-
public OnClickEvent(final View view) {
22-
this.view = view;
20+
@AutoValue
21+
public abstract class OnClickEvent {
22+
public abstract View view();
23+
24+
public static OnClickEvent create(View view) {
25+
return new AutoValue_OnClickEvent(view);
2326
}
2427
}

rxandroid/src/main/java/rx/android/view/OnSubscribeViewClick.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void call(final Subscriber<? super OnClickEvent> observer) {
4343
final View.OnClickListener listener = new View.OnClickListener() {
4444
@Override
4545
public void onClick(final View clicked) {
46-
observer.onNext(new OnClickEvent(view));
46+
observer.onNext(OnClickEvent.create(view));
4747
}
4848
};
4949

@@ -55,7 +55,7 @@ public void call() {
5555
});
5656

5757
if (emitInitialValue) {
58-
observer.onNext(new OnClickEvent(view));
58+
observer.onNext(OnClickEvent.create(view));
5959
}
6060

6161
composite.addOnClickListener(listener);

rxandroid/src/main/java/rx/android/widget/OnItemClickEvent.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
import android.widget.Adapter;
1818
import android.widget.AdapterView;
1919

20-
public class OnItemClickEvent {
21-
public final AdapterView<?> parent;
22-
public final View view;
23-
public final int position;
24-
public final long id;
20+
import com.google.auto.value.AutoValue;
2521

26-
public OnItemClickEvent(AdapterView<?> parent, View view, int position, long id) {
27-
this.parent = parent;
28-
this.view = view;
29-
this.position = position;
30-
this.id = id;
22+
@AutoValue
23+
public abstract class OnItemClickEvent {
24+
abstract public AdapterView<?> parent();
25+
abstract public View view();
26+
abstract public int position();
27+
abstract public long id();
28+
29+
public static OnItemClickEvent create(AdapterView<?> parent, View view, int position, long id) {
30+
return new AutoValue_OnItemClickEvent(parent, view, position, id);
3131
}
3232
}

rxandroid/src/main/java/rx/android/widget/OnListViewScrollEvent.java

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,70 +16,19 @@
1616

1717
import android.widget.AbsListView;
1818

19-
public class OnListViewScrollEvent {
20-
public final AbsListView listView;
21-
public final int scrollState;
22-
public final int firstVisibleItem;
23-
public final int visibleItemCount;
24-
public final int totalItemCount;
19+
import com.google.auto.value.AutoValue;
2520

26-
public OnListViewScrollEvent(
27-
AbsListView listView, int scrollState, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
28-
this.listView = listView;
29-
this.scrollState = scrollState;
30-
this.firstVisibleItem = firstVisibleItem;
31-
this.visibleItemCount = visibleItemCount;
32-
this.totalItemCount = totalItemCount;
33-
}
34-
35-
@Override
36-
public boolean equals(Object o) {
37-
if (this == o) {
38-
return true;
39-
}
40-
if (o == null || getClass() != o.getClass()) {
41-
return false;
42-
}
43-
44-
OnListViewScrollEvent that = (OnListViewScrollEvent) o;
45-
46-
if (firstVisibleItem != that.firstVisibleItem) {
47-
return false;
48-
}
49-
if (scrollState != that.scrollState) {
50-
return false;
51-
}
52-
if (totalItemCount != that.totalItemCount) {
53-
return false;
54-
}
55-
if (visibleItemCount != that.visibleItemCount) {
56-
return false;
57-
}
58-
if (!listView.equals(that.listView)) {
59-
return false;
60-
}
21+
@AutoValue
22+
public abstract class OnListViewScrollEvent {
23+
public abstract AbsListView listView();
24+
public abstract int scrollState();
25+
public abstract int firstVisibleItem();
26+
public abstract int visibleItemCount();
27+
public abstract int totalItemCount();
6128

62-
return true;
63-
}
64-
65-
@Override
66-
public int hashCode() {
67-
int result = listView.hashCode();
68-
result = 31 * result + scrollState;
69-
result = 31 * result + firstVisibleItem;
70-
result = 31 * result + visibleItemCount;
71-
result = 31 * result + totalItemCount;
72-
return result;
29+
public static OnListViewScrollEvent create(
30+
AbsListView listView, int scrollState, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
31+
return new AutoValue_OnListViewScrollEvent(listView, scrollState, firstVisibleItem, visibleItemCount, totalItemCount);
7332
}
7433

75-
@Override
76-
public String toString() {
77-
return "OnListViewScrollEvent{" +
78-
"listView=" + listView +
79-
", scrollState=" + scrollState +
80-
", firstVisibleItem=" + firstVisibleItem +
81-
", visibleItemCount=" + visibleItemCount +
82-
", totalItemCount=" + totalItemCount +
83-
'}';
84-
}
8534
}

rxandroid/src/main/java/rx/android/widget/OnSubscribeAdapterViewOnItemClick.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void call(final Subscriber<? super OnItemClickEvent> observer) {
4545
final AbsListView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
4646
@Override
4747
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
48-
observer.onNext(new OnItemClickEvent(parent, view, position, id));
48+
observer.onNext(OnItemClickEvent.create(parent, view, position, id));
4949
}
5050
};
5151

rxandroid/src/main/java/rx/android/widget/OnSubscribeCompoundButtonInput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void call(final Subscriber<? super OnCheckedChangeEvent> observer) {
4545
final CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
4646
@Override
4747
public void onCheckedChanged(final CompoundButton view, final boolean checked) {
48-
observer.onNext(new OnCheckedChangeEvent(button, checked));
48+
observer.onNext(OnCheckedChangeEvent.create(button, checked));
4949
}
5050
};
5151

@@ -57,7 +57,7 @@ public void call() {
5757
});
5858

5959
if (emitInitialValue) {
60-
observer.onNext(new OnCheckedChangeEvent(button));
60+
observer.onNext(OnCheckedChangeEvent.create(button));
6161
}
6262

6363
composite.addOnCheckedChangeListener(listener);

rxandroid/src/main/java/rx/android/widget/OnSubscribeListViewScroll.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {
5050

5151
@Override
5252
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
53-
OnListViewScrollEvent event = new OnListViewScrollEvent(view, this.currentScrollState, firstVisibleItem,
53+
OnListViewScrollEvent event = OnListViewScrollEvent.create(view, this.currentScrollState, firstVisibleItem,
5454
visibleItemCount, totalItemCount);
5555
observer.onNext(event);
5656
}

rxandroid/src/main/java/rx/android/widget/OnSubscribeTextViewInput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void call(final Subscriber<? super OnTextChangeEvent> observer) {
3838
final TextWatcher watcher = new SimpleTextWatcher() {
3939
@Override
4040
public void afterTextChanged(final Editable editable) {
41-
observer.onNext(new OnTextChangeEvent(input));
41+
observer.onNext(OnTextChangeEvent.create(input));
4242
}
4343
};
4444

@@ -50,7 +50,7 @@ public void call() {
5050
});
5151

5252
if (emitInitialValue) {
53-
observer.onNext(new OnTextChangeEvent(input));
53+
observer.onNext(OnTextChangeEvent.create(input));
5454
}
5555

5656
input.addTextChangedListener(watcher);

rxandroid/src/main/java/rx/android/widget/OnTextChangeEvent.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
import android.text.SpannableString;
1717
import android.widget.TextView;
1818

19-
public class OnTextChangeEvent {
20-
public final TextView view;
21-
public final CharSequence text;
19+
import com.google.auto.value.AutoValue;
2220

23-
public OnTextChangeEvent(final TextView view) {
24-
this(view, new SpannableString(view.getText()));
21+
@AutoValue
22+
public abstract class OnTextChangeEvent {
23+
public abstract TextView view();
24+
public abstract CharSequence text();
25+
26+
public static OnTextChangeEvent create(final TextView view) {
27+
return create(view, new SpannableString(view.getText()));
2528
}
2629

27-
public OnTextChangeEvent(final TextView view, final CharSequence text) {
28-
this.view = view;
29-
this.text = text;
30+
public static OnTextChangeEvent create(final TextView view, final CharSequence text) {
31+
return new AutoValue_OnTextChangeEvent(view, text);
3032
}
3133
}

rxandroid/src/test/java/rx/android/view/OperatorViewClickTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
@RunWith(RobolectricTestRunner.class)
3838
public class OperatorViewClickTest {
3939
private static OnClickEvent mkMockedEvent(final View view) {
40-
return refEq(new OnClickEvent(view));
40+
return refEq(OnClickEvent.create(view));
4141
}
4242

4343
@Test

rxandroid/src/test/java/rx/android/widget/OperatorAdapterViewOnItemClickTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ private void performTestAdapterViewClickAllViewsEmitAllEvents(AdapterView<? exte
228228
final InOrder inOrder = inOrder(observer);
229229

230230
for (int i = 0; i < adapter.getCount(); i++) {
231-
adapterView.performItemClick(any(View.class), i, i);
232-
inOrder.verify(observer, times(1)).onNext(new OnItemClickEvent(adapterView, any(View.class), i, i));
231+
View fakeItem = new View(adapterView.getContext());
232+
adapterView.performItemClick(fakeItem, i, i);
233+
inOrder.verify(observer, times(1)).onNext(OnItemClickEvent.create(adapterView, fakeItem, i, i));
233234
}
234235

235236
subscription.unsubscribe();
@@ -313,9 +314,10 @@ private void performTestAdapterViewMultipleSubscriptionsClickAllViewsEmitAllEven
313314

314315
final int count = adapter.getCount();
315316
for (int i = 0; i < count; i++) {
316-
adapterView.performItemClick(any(View.class), i, i);
317-
inOrder1.verify(observer1, times(1)).onNext(new OnItemClickEvent(adapterView, any(View.class), i, i));
318-
inOrder2.verify(observer2, times(1)).onNext(new OnItemClickEvent(adapterView, any(View.class), i, i));
317+
View fakeItem = new View(adapterView.getContext());
318+
adapterView.performItemClick(fakeItem, i, i);
319+
inOrder1.verify(observer1, times(1)).onNext(OnItemClickEvent.create(adapterView, fakeItem, i, i));
320+
inOrder2.verify(observer2, times(1)).onNext(OnItemClickEvent.create(adapterView, fakeItem, i, i));
319321
}
320322

321323
subscription1.unsubscribe();
@@ -342,9 +344,10 @@ private void performTestAdapterViewMultipleSubscriptionsClickAllViewsEmitAllEven
342344

343345
final int count = adapter.getCount();
344346
for (int i = 0; i < count; i++) {
345-
adapterView.performItemClick(any(View.class), i, i);
347+
View fakeItem = new View(adapterView.getContext());
348+
adapterView.performItemClick(fakeItem, i, i);
346349
inOrder1.verify(observer1, never()).onNext(any(OnItemClickEvent.class));
347-
inOrder2.verify(observer2, times(1)).onNext(new OnItemClickEvent(adapterView, any(View.class), i, i));
350+
inOrder2.verify(observer2, times(1)).onNext(OnItemClickEvent.create(adapterView, fakeItem, i, i));
348351
}
349352
subscription2.unsubscribe();
350353
}

rxandroid/src/test/java/rx/android/widget/OperatorCompoundButtonInputTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
@RunWith(RobolectricTestRunner.class)
3333
public class OperatorCompoundButtonInputTest {
3434
private static OnCheckedChangeEvent mkMockedEvent(final CompoundButton button, final boolean value) {
35-
return refEq(new OnCheckedChangeEvent(button, value));
35+
return refEq(OnCheckedChangeEvent.create(button, value));
3636
}
3737

3838
private static CompoundButton mkCompoundButton(final boolean value) {

rxandroid/src/test/java/rx/android/widget/OperatorTextViewInputTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public boolean matches(final Object argument) {
4343

4444
final OnTextChangeEvent event = (OnTextChangeEvent) argument;
4545

46-
if (event.view != view) {
46+
if (event.view() != view) {
4747
return false;
4848
}
4949

50-
return TextUtils.equals(event.text, text);
50+
return TextUtils.equals(event.text(), text);
5151
}
5252
});
5353
}

sample-app/src/main/java/rx/android/samples/ListFragmentActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
6464
.subscribe(new Action1<OnListViewScrollEvent>() {
6565
@Override
6666
public void call(OnListViewScrollEvent event) {
67-
if (event.totalItemCount == 0) {
67+
if (event.totalItemCount() == 0) {
6868
return;
6969
}
7070

7171
int progress =
72-
(int) ((100.0 * (event.firstVisibleItem + event.visibleItemCount)) / event.totalItemCount);
72+
(int) ((100.0 * (event.firstVisibleItem() + event.visibleItemCount())) / event.totalItemCount());
7373
progressBar.setProgress(progress);
7474
}
7575
});

0 commit comments

Comments
 (0)