Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2013 AndroidPlot.com
      3  *
      4  *    Licensed under the Apache License, Version 2.0 (the "License");
      5  *    you may not use this file except in compliance with the License.
      6  *    You may obtain a copy of the License at
      7  *
      8  *        http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  *    Unless required by applicable law or agreed to in writing, software
     11  *    distributed under the License is distributed on an "AS IS" BASIS,
     12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  *    See the License for the specific language governing permissions and
     14  *    limitations under the License.
     15  */
     16 
     17 package com.androidplot.util;
     18 import java.util.List;
     19 import java.util.Set;
     20 
     21 /**
     22  * Utility class for obtaining synch lock across multiple objects.
     23  */
     24 public abstract class MultiSynch {
     25 
     26     /**
     27      * Callback class for doing work from within a MultiSynch.
     28      */
     29     public interface Action {
     30 
     31         /**
     32          * Invoked by MultiSynch.run(...)
     33          * @param params
     34          */
     35         public void run(Object[] params);
     36     }
     37 
     38 
     39     /**
     40      *
     41      * @param params
     42      * @param synchSet Set of objects to be synchronized upon
     43      * @param action Action to be invoked once  full synchronization has been obtained.
     44      */
     45     public static void run(Object[] params, Set synchSet,  Action action) {
     46         run(params, synchSet.toArray(), action, 0);
     47     }
     48 
     49     /**
     50      * @param params
     51      * @param synchList List of objects to be synchronized upon
     52      * @param action   Action to be invoked once  full synchronization has been obtained.
     53      */
     54     public static void run(Object[] params, List synchList, Action action) {
     55         run(params, synchList.toArray(), action, 0);
     56     }
     57 
     58     /**
     59      * @param params
     60      * @param synchArr Array of objects to be synchronized upon
     61      * @param action   Action to be invoked once  full synchronization has been obtained.
     62      */
     63     public static void run(Object[] params, Object[] synchArr, Action action) {
     64         run(params, synchArr, action, 0);
     65     }
     66 
     67     /**
     68      * Recursively synchs on each item in SynchList
     69      * @param params
     70      * @param synchArr
     71      * @param action
     72      * @param depth
     73      */
     74     private static void run(Object[] params, Object[] synchArr, Action action, int depth) {
     75         if (synchArr != null) {
     76             synchronized (synchArr[depth]) {
     77                 if (depth < synchArr.length - 1) {
     78                     run(params, synchArr, action, ++depth);
     79                 } else {
     80                     action.run(params);
     81                 }
     82             }
     83         }
     84         action.run(params);
     85     }
     86 }
     87