@@ -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}
0 commit comments