Skip to content

Commit a6d5bfd

Browse files
author
lightsun
committed
update GroupDataManager
1 parent 7ee6ce5 commit a6d5bfd

File tree

6 files changed

+152
-41
lines changed

6 files changed

+152
-41
lines changed

data-mediator/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ publish {
3838
userOrg = 'lightsun' //bintray user name
3939
groupId = 'com.heaven7.java.data.mediator'
4040
artifactId = 'data-mediator'
41-
publishVersion = '1.4.5-beta3'
41+
publishVersion = '1.4.5-beta5'
4242
desc = 'this is a java lib of data mediator. '
4343
website = 'https://github.com/LightSun/data-mediator'
4444
}

data-mediator/src/main/java/com/heaven7/java/data/mediator/BaseMediator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ public synchronized void addCallback(DataMediatorCallback<? super T> o) {
146146
}
147147
}
148148

149+
/**
150+
* contains target callback or not.
151+
* @param o the data mediator callback
152+
* @return true if contains.
153+
* @since
154+
*/
155+
public synchronized boolean containsCallback(DataMediatorCallback<? super T> o){
156+
return _mCallbacks.contains(o);
157+
}
158+
149159
/**
150160
* remove the data mediator callback
151161
*

data-mediator/src/main/java/com/heaven7/java/data/mediator/DataMediator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,21 @@ public void removeDataMediatorCallbacks() {
310310
mediator.removeCallbacks();
311311
}
312312

313-
313+
/**
314+
* contains callback or not.
315+
* @param callback the target callback
316+
* @return true if contains
317+
* @since 1.4.5
318+
*/
319+
public boolean containsCallback(DataMediatorCallback<T> callback) {
320+
return mediator.containsCallback(callback);
321+
}
314322
//============================ private methods ============================
323+
315324
private boolean inflatePropertyChain0(String propertyChain){
316325
if(mInflater == null){
317326
mInflater = new PropertyChainInflater<T>(this);
318327
}
319328
return mInflater.inflatePropertyChain(propertyChain);
320329
}
321-
322330
}

data-mediator/src/main/java/com/heaven7/java/data/mediator/DataMediatorFactory.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,28 @@ public final class DataMediatorFactory {
3131
private static final String SUFFIX_PROXY = "_$Proxy";
3232

3333
/**
34-
* create group properties as 'Gps'.
34+
* create group properties recursively as 'Gps'.
3535
* @param clazz the data/interface class.
36-
* @return the 'Gps'
36+
* @return the 'Gps' or null if the target GPS not exist.
3737
* @since 1.4.5
3838
*/
3939
public static Gps createGps(Class<?> clazz){
40-
try {
41-
Class<?> class_gps = Class.forName(clazz.getName() + "_$GPS");
42-
return (Gps) class_gps.newInstance();
43-
} catch (Exception e) {
44-
throw new RuntimeException("create $Gps failed, caused by "+ clazz.getName() + "_$Gps doesn't exists !");
45-
}
40+
Class<?> target = clazz;
41+
do {
42+
try {
43+
Class<?> class_gps = Class.forName(target.getName() + "_$GPS");
44+
return (Gps) class_gps.newInstance();
45+
}catch (ClassNotFoundException e){
46+
target = target.getSuperclass();
47+
if(target == null || target.getName().startsWith("java.")
48+
|| target.getName().startsWith("android.")){
49+
//throw new RuntimeException("create $Gps failed, caused by "+ clazz.getName() + "_$Gps doesn't exists !");
50+
return null;
51+
}
52+
}catch (InstantiationException | IllegalAccessException e){
53+
throw new RuntimeException(e);
54+
}
55+
}while (true);
4656
}
4757

4858
/**

data-mediator/src/main/java/com/heaven7/java/data/mediator/GroupDataManager.java

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public final class GroupDataManager<T> {
2323

2424
// (key, value) = (type, GroupProperty)
2525
private final SparseArray<List<GroupProperty>> mPropMap = new SparseArray<>();
26-
private final State mState;
26+
private final State<T> mState;
27+
private boolean mAttached;
2728

2829
private final DataMediatorCallback<T> mCallback = new DataMediatorCallback<T>() {
2930
@Override
@@ -90,32 +91,98 @@ public static <T, D extends MediatorDelegate<T>> GroupDataManager<T> of(SparseAr
9091
return gdm;
9192
}
9293

93-
public void attach() {
94-
mState.attach();
94+
/**
95+
* attach internal data-mediator-callback to all data-proxy,
96+
*/
97+
public void attachAll() {
98+
if(!mAttached) {
99+
mAttached = true;
100+
mState.attach();
101+
}
102+
}
103+
/**
104+
* detach internal data-mediator-callback from all data-proxy,
105+
*/
106+
public void detachAll() {
107+
if(mAttached) {
108+
mState.detach();
109+
mAttached = false;
110+
}
95111
}
96112

97-
public void detach() {
98-
mState.detach();
113+
/**
114+
* attach internal data-mediator-callback to target delegate
115+
* @param delegate mediator delegate
116+
* @param strict true if attach strictly
117+
* @return true if attach success. always return true if strict is false.
118+
*/
119+
public boolean attach(MediatorDelegate<T> delegate, boolean strict){
120+
if(!strict){
121+
delegate.getDataMediator().addDataMediatorCallback(mCallback);
122+
return true;
123+
}else {
124+
if (mState.containsMediatorDelegate(delegate)) {
125+
delegate.getDataMediator().addDataMediatorCallback(mCallback);
126+
return true;
127+
}
128+
}
129+
return false;
130+
}
131+
/**
132+
* detach internal data-mediator-callback to target delegate
133+
* @param delegate mediator delegate
134+
* @param strict true if detach strictly
135+
* @return true if detach success. always return true if strict is false.
136+
*/
137+
public boolean detach(MediatorDelegate<T> delegate, boolean strict){
138+
if(!strict){
139+
delegate.getDataMediator().removeDataMediatorCallback(mCallback);
140+
return true;
141+
}else {
142+
if (mState.containsMediatorDelegate(delegate)) {
143+
delegate.getDataMediator().removeDataMediatorCallback(mCallback);
144+
return true;
145+
}
146+
}
147+
return false;
99148
}
100149

101-
@SuppressWarnings("unchecked")
150+
/**
151+
* get datas by target property and value.
152+
* @param prop the property
153+
* @param val the value
154+
* @return the datas
155+
*/
102156
public List<T> getDatas(Property prop, Object val) {
103-
return (List<T>) mState.getItems(prop, val, false);
157+
return mState.getItems(prop, val, false);
104158
}
105159

106-
@SuppressWarnings("unchecked")
160+
/**
161+
* get all data-proxy by target property and value.
162+
* @param prop the property
163+
* @param val the value
164+
* @return all data-proxy as list
165+
*/
107166
public List<T> getDataProxies(Property prop, Object val) {
108-
return (List<T>) mState.getItems(prop, val, true);
167+
return mState.getItems(prop, val, true);
109168
}
110-
111-
@SuppressWarnings("unchecked")
169+
/**
170+
* get data by target property and value.
171+
* @param prop the property
172+
* @param val the value
173+
* @return the data
174+
*/
112175
public T getData(Property prop, Object val) {
113-
return (T) mState.getItem(prop, val, false);
176+
return mState.getItem(prop, val, false);
114177
}
115-
116-
@SuppressWarnings("unchecked")
178+
/**
179+
* get data-proxy by target property and value.
180+
* @param prop the property
181+
* @param val the value
182+
* @return the data-proxy
183+
*/
117184
public T getDataProxy(Property prop, Object val) {
118-
return (T) mState.getItem(prop, val, true);
185+
return mState.getItem(prop, val, true);
119186
}
120187

121188
private void prepare(List<GroupProperty> gps) {
@@ -283,19 +350,21 @@ public interface MediatorDelegate<T> {
283350
DataMediator<T> getDataMediator();
284351
}
285352

286-
private interface State {
353+
private interface State<T>{
287354
void attach();
288355

289356
void detach();
290357

291-
void doMutex(GroupProperty target, Object data);
358+
void doMutex(GroupProperty target, T data);
292359

293-
List<?> getItems(Property prop, Object val, boolean asProxy);
360+
List<T> getItems(Property prop, Object val, boolean asProxy);
294361

295-
Object getItem(Property prop, Object val, boolean asProxy);
362+
T getItem(Property prop, Object val, boolean asProxy);
363+
364+
boolean containsMediatorDelegate(MediatorDelegate<T> delegate);
296365
}
297366

298-
private class ArrayState implements State {
367+
private class ArrayState implements State<T> {
299368

300369
private final MediatorDelegate<T>[] array;
301370

@@ -318,7 +387,7 @@ public void detach() {
318387
}
319388

320389
@Override
321-
public void doMutex(GroupProperty target, Object data) {
390+
public void doMutex(GroupProperty target, T data) {
322391
for (MediatorDelegate<T> md : array) {
323392
if (md.getDataMediator().getData() == data) {
324393
continue;
@@ -328,7 +397,7 @@ public void doMutex(GroupProperty target, Object data) {
328397
}
329398

330399
@Override
331-
public List<?> getItems(Property prop, Object val, boolean asProxy) {
400+
public List<T> getItems(Property prop, Object val, boolean asProxy) {
332401
List<T> results = new ArrayList<>();
333402
for (MediatorDelegate<T> md : array) {
334403
T item = getItem0(prop, val, asProxy, md);
@@ -340,7 +409,7 @@ public List<?> getItems(Property prop, Object val, boolean asProxy) {
340409
}
341410

342411
@Override
343-
public Object getItem(Property prop, Object val, boolean asProxy) {
412+
public T getItem(Property prop, Object val, boolean asProxy) {
344413
for (MediatorDelegate<T> md : array) {
345414
T item = getItem0(prop, val, asProxy, md);
346415
if(item != null){
@@ -349,9 +418,13 @@ public Object getItem(Property prop, Object val, boolean asProxy) {
349418
}
350419
return null;
351420
}
421+
@Override
422+
public boolean containsMediatorDelegate(MediatorDelegate<T> delegate) {
423+
return Arrays.asList(array).contains(delegate);
424+
}
352425
}
353426

354-
private class ListState implements State {
427+
private class ListState implements State<T> {
355428
private final List<? extends MediatorDelegate<T>> mList;
356429

357430
ListState(List<? extends MediatorDelegate<T>> mList) {
@@ -383,7 +456,7 @@ public void doMutex(GroupProperty target, Object data) {
383456
}
384457

385458
@Override
386-
public List<?> getItems(Property prop, Object val, boolean asProxy) {
459+
public List<T> getItems(Property prop, Object val, boolean asProxy) {
387460
List<T> items = new ArrayList<>();
388461
for (MediatorDelegate<T> md : mList) {
389462
T item = getItem0(prop, val, asProxy, md);
@@ -395,7 +468,7 @@ public List<?> getItems(Property prop, Object val, boolean asProxy) {
395468
}
396469

397470
@Override
398-
public Object getItem(Property prop, Object val, boolean asProxy) {
471+
public T getItem(Property prop, Object val, boolean asProxy) {
399472
for (MediatorDelegate<T> md : mList) {
400473
T item = getItem0(prop, val, asProxy, md);
401474
if(item != null){
@@ -404,9 +477,13 @@ public Object getItem(Property prop, Object val, boolean asProxy) {
404477
}
405478
return null;
406479
}
480+
@Override
481+
public boolean containsMediatorDelegate(MediatorDelegate<T> delegate) {
482+
return mList.contains(delegate);
483+
}
407484
}
408485

409-
private class SparseArrayState implements State {
486+
private class SparseArrayState implements State<T> {
410487

411488
private final SparseArray<? extends MediatorDelegate<T>> mSa;
412489

@@ -451,7 +528,7 @@ public void doMutex(GroupProperty target, Object data) {
451528
}
452529

453530
@Override
454-
public List<?> getItems(Property prop, Object val, boolean asProxy) {
531+
public List<T> getItems(Property prop, Object val, boolean asProxy) {
455532
List<T> results = new ArrayList<>();
456533
int size = mSa.size();
457534
for (int i = size - 1; i >= 0; i--) {
@@ -467,7 +544,7 @@ public List<?> getItems(Property prop, Object val, boolean asProxy) {
467544
}
468545

469546
@Override
470-
public Object getItem(Property prop, Object val, boolean asProxy) {
547+
public T getItem(Property prop, Object val, boolean asProxy) {
471548
int size = mSa.size();
472549
for (int i = size - 1; i >= 0; i--) {
473550
MediatorDelegate<T> delegate = mSa.get(mSa.keyAt(i));
@@ -480,5 +557,11 @@ public Object getItem(Property prop, Object val, boolean asProxy) {
480557
}
481558
return null;
482559
}
560+
@SuppressWarnings("unchecked")
561+
@Override
562+
public boolean containsMediatorDelegate(MediatorDelegate<T> delegate) {
563+
SparseArray<MediatorDelegate<T>> mSa = (SparseArray<MediatorDelegate<T>>) this.mSa;
564+
return mSa.indexOfValue(delegate) >= 0;
565+
}
483566
}
484567
}

data-mediator/src/test/java/com/heaven7/java/data/mediator/test/gdm/GroupDataManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public DataMediator<TestState> getDataMediator() {
3636
gp.setProperty(TestState.PROP_state);
3737
gp.setValue(FOCUS_VALUE);
3838
gp.setOppositeValue(OPPISITE_VALUE);
39-
GroupDataManager.of(list, Arrays.asList(gp)).attach();
39+
GroupDataManager.of(list, Arrays.asList(gp)).attachAll();
4040
}
4141

4242
private List<TestState> createDatas() {

0 commit comments

Comments
 (0)